SELECT DISTINCT items.itemid
FROM billingdata
LEFT OUTER JOIN items
ON items.itemid = billingdata.itemid
WHERE billingdata.encounterid IN (SELECT encounterid
FROM enc
WHERE patientid = 301487
AND date < '2014-08-21'
AND enctype <> 8
AND deleteflag = 0)
ORDER BY billingdata.displayindex;
在此查询中,在itemid,billingdata.encounterid,patientid中有索引。
此查询的执行计划是这样的: 它没有在billingdata表上显示任何索引。此表中的大部分行都受到影响 相反,enc表的遭遇在索引中显示。
它第一次服用18秒,然后服用1.8秒。但它也很慢。它可能是什么解决方案?
答案 0 :(得分:1)
我的理解是,在寻找IN的纯粹存在时,RDBMS中运行速度最快,速度有点慢。
所以这可能会更有效率,但enc和billingdata中的encounterID索引会产生很大的不同。
SELECT DISTINCT items.itemid
FROM billingdata
LEFT OUTER JOIN items
ON items.itemid = billingdata.itemid
WHERE exists (SELECT 1
FROM enc
WHERE patientid = 301487
AND date < '2014-08-21'
AND enctype <> 8
AND deleteflag = 0
AND enc.encounterID = billingdata.encounterid)
ORDER BY billingdata.displayindex;
甚至连接都可以更快,具体取决于索引..
SELECT DISTINCT items.itemid
FROM billingdata
INNER JOIN enc
ON ENC.EncounterID = BillingData.EncounterID
AND patientid = 301487
AND date < '2014-08-21'
AND enctype <> 8
AND deleteflag = 0
LEFT OUTER JOIN items
ON items.itemid = billingdata.itemid
ORDER BY billingdata.displayindex;