下面是我的SQL查询,其中包含五个内部联接。执行需要10分钟。我们如何优化此查询以更快地运行?
SELECT DISTINCT a.assesmt_no,
a.parcel_no,
vi.struct_no,
om.own_first AS TaxpayerName
FROM assessments a
INNER JOIN owner om ON om.id = a.owner_id
INNER JOIN valueimp vi ON vi.assesmt_no = a.assesmt_no
INNER JOIN imp_details imp ON imp.improvementId = vi.id
INNER JOIN imp_components_details impdt ON impdt.quality_id = imp.quality_id
INNER JOIN category_items ci ON ci.category_id = impdt.category_id
WHERE ci.category_id <> 3
答案 0 :(得分:0)
所有乘法行的连接可以转换为exists
,之后您就可以放弃distinct
。 category_id <> 3
使其无法执行除scan
以外的其他操作,除非大多数行都有category_id=3
,其中只有少数行<>
。
所以,如果没有DDL和其他信息,我的猜测是:
select
a.assesmt_no, a.parcel_no, vi.struct_no, om.own_first as TaxpayerName
from assessments a
inner join owner om on om.id = a.owner_id
inner join valueimp vi on vi.assesmt_no = a.assesmt_no
where exists(
select 1
from imp_details imp
inner join imp_components_details impdt on impdt.quality_id = imp.quality_id
inner join category_items ci on ci.category_id = impdt.category_id
where imp.improvementId = vi.id
and ci.category_id <> 3
)
我担心别无其他事情可做。
答案 1 :(得分:0)
请在查询末尾添加option (hash join,hash group)
P.S。
imp_details
和imp_components_details
加入?
是用于过滤目的吗?distinct
?