作者:半邪书生66_516 | 来源:互联网 | 2023-10-10 13:58
ImtryingtooptimizeaslowqueryandIvecomeacrossthefollowing(runningconsistentlyover8
I'm trying to optimize a slow query and I've come across the following (running consistently over 8 seconds).
我正在尝试优化慢速查询,我遇到了以下情况(持续运行超过8秒)。
SELECT entryID, entryID AS iE, 0 AS eE, 'clarus1' AS locationID, dateStamp, amount AS income, NULL AS expense, reconciled, leaseID AS vendorID, incomeID AS expenseID
FROM structu_income.iLedger
WHERE (dateStamp BETWEEN '2009-04-16' AND '2012-02-29') AND incomeID IS NOT NULL
AND (
leaseID IN (
SELECT lease.leaseID FROM structu_assets.lease WHERE lease.unitID IN (
SELECT unit.unitID FROM structu_assets.unit WHERE unit.locatiOnID= 'clarus1'
)
)
OR locationID IN (SELECT locationID FROM structu_assets.deed WHERE ownerID = 'clarus')
)
Here's the EXPLAIN
:
这是EXPLAIN:
My thought was to refactor the subqueries to use JOIN
. But keeping the logical OR
is throwing me off.
我的想法是重构子查询以使用JOIN。但保持合乎逻辑的OR会让我失望。
In addition, the nested subqueries seem inevitable. Unless I predetermined the unitID
in a separate query.
此外,嵌套的子查询似乎是不可避免的。除非我在单独的查询中预先确定unitID。
I'm not the original developer. But I'm charged with making it more performant without modifying the existing codebase or schema. So I'm attempting the pick off the slow queries.
我不是原来的开发者。但是我负责在不修改现有代码库或架构的情况下使其更高性能。所以我正在尝试挑选慢速查询。
As an aside, do cross database queries take a performance hit?
另外,交叉数据库查询是否会受到性能影响?
2 个解决方案