首页 > 解决方案 > Oracle - NVL(col1,col2) 按缓慢排序

问题描述

Select 子句 NVL(b.name, a.name) 中有一个列,我在 Order By 中使用此列,因此 Oracle 查询变慢了。

我尝试在 NAME 列上创建索引,但没有用。

SELECT
    *
FROM
    (
        SELECT
            nvl(b.name,a.name) AS b_a_name, -- Order by is using this column and hence the slowness. Index is present on NAME column but of no use
            b.name b_name,
            a.name a_name
        FROM
            employee a
            LEFT JOIN employee b ON a.parent_id = b.child_id
    )
ORDER BY b_a_name --- this Order By is taking time
;

我希望如何调整 Order By 子句或如何重新编写查询以获得相同的输出但提高性能。

标签: sqloracleoracle11gsql-tuning

解决方案


怎么NVL去掉ORDER BY

SELECT NVL(b.name, a.name) AS b_a_name,
       b.name a_name,
       a.name b_name
  FROM employee a
       LEFT JOIN employee b ON a.parent_id = b.child_id
  ORDER BY b.name, a.name;

无论如何,ORDER BY 都会减慢速度。无序集总是检索得更快。


顺便说一句,你为什么用列别名来做这件事?迷惑敌人?好吧,你把我弄糊涂

b.name a_name      --> shouldn't that be b_name
a.name b_name      -->                   a_name

推荐阅读