我有下面的语句来找到自己的数据明确的名称(约1万个条目):为什么SQL成本会用简单的“或”爆炸?
select Prename, Surname from person p1
where Prename is not null and Surname is not null
and not exists (
select * from person p2 where (p1.Surname = p2.Surname OR p1.Surname = p2.Altname)
and p2.Prename LIKE CONCAT(CONCAT('%', p1.Prename), '%') and p2.id <> p1.id
) and inv_date IS NULL
甲骨文表示1477315000巨大的成本,并在5分钟后执行并没有结束。简单地拆分或成自己的存在款性能提升至0.5秒和成本45000:
select Prename, Surname from person p1
where Prename is not null and Surname is not null
and not exists (
select * from person p2 where p1.Surname = p2.Surname and
p2.Prename LIKE CONCAT(CONCAT('%', p1.Prename), '%') and p2.id <> p1.id
) and not exists (
select * from person p2 where p1.Surname = p2.Altname and
p2.Prename LIKE CONCAT(CONCAT('%', p1.Prename), '%') and p2.id <> p1.id
) and inv_date IS NULL
这不是我的问题来调整这是最好的,因为它只是一个罕执行查询,我知道CONTACT超过任何指数,但我只是想知道这种高成本来自哪里。这两个查询在语义上都与我相同。
+1 - 详细说明,'EXISTS'短路和'OR'没有(至少在SQL Server中,我假设Oracle是相似的)。通过在'EXISTS'子文件中包含'OR',它每次都检查两个选项。分离意味着它只检查第二个是否第一个是假的。 – JNK 2011-05-23 14:41:28
+1 - 执行计划1:筛选器不存在(...)1477315000 |表索引人员索引ROWID 13863 |按索引表访问人员ROWID 4019;计划2是巨大的,并使用两个散列连接 – stracktracer 2011-05-23 14:47:08
接受asnwer。看起来我高估了Oracle的查询分析器 – stracktracer 2011-05-24 07:53:16