sql - 向oracle查询添加 'distinct'关键字会无故抹杀查询性能

标签 sql performance oracle optimization cbo

我对在 Oracle 10 数据库中看到的一些东西感到很困惑。

我有以下查询。

select
t2.duplicate_num
from table1 t1, table2 t2,

( 
   select joincriteria_0 from intable1 it1, intable2 it2 
   where it2.identifier in (4496486,5911382) 
   and it1.joincriteria_0 = it2.joincriteria_0 
   and it1.filter_0 = 1 
) tt

where t1.joincriteria_0 = tt.joincriteria_0
and t2.joincriteria_1 = t1.joincriteria_1
and t2.filter_0 = 3
and t2.filter_1 = 1
and t2.filter_2 not in (48020)

这对我来说似乎没什么特别的,以下是来自 autotrace 的基准性能数据:

CR_GETS:318

中央处理器:3

行数:33173

现在,如果我将“DISTINCT”关键字添加到查询中(例如,“select distinct t2.duplicate_num...”),就会发生这种情况

CR_GETS:152921

中央处理器:205

行数:305

查询计划没有改变,但逻辑 IO 增长了 500 倍。我原以为 CPU 只会上升而逻辑 IO 基本没有变化。

最终结果是使用 distinct 关键字的查询运行速度降低了 10-100 倍。我可以将代码放入应用程序中,这将使结果集在很短的时间内截然不同。这有什么意义?特别是在查询计划没有改变的情况下?

最佳答案

这表示某处缺少索引。这也意味着,您没有优化没有 distinct 子句的原始查询。使用“distinct”也无法对其进行优化,因此查询计划保持不变。由于全表扫描,未优化的查询在性能上差异很大。

关于sql - 向oracle查询添加 'distinct'关键字会无故抹杀查询性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12681123/

相关文章:

php - 在 php 中使用自动加载并搜索类文件会影响性能吗?

oracle - 我可以用来防止行被插入到 Oracle 表中的侵入性最小的锁定机制是什么?

sql - 从字符串(VARCHAR2)oracle sql中一一打印字符,而不使用plsql,也不使用dual

sql - Rails - 加入后不同

sql - 为列设置别名的不同方法

mysql - 如何自动设置一个mysql列等于另一个操作?

c# - 编译 32 位和 64 位时的巨大性能差异(快 26 倍)

performance - 使用 Vnode 重新平衡 Cassandra 环

sql - oracle数据库中的分布式事务锁

mysql - SQL防止重复