sql - 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 子句或如何重新编写查询以获得相同的输出但提高性能。
解决方案
怎么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
推荐阅读
- tableau-api - 使用日期参数将 Tableau 仪表板默认设置为昨天
- r - 修复使用 mutate 函数和 ggplot 时 X 标签的顺序
- c++ - C++ 基本函数和参数传递错误:CS3861
- reactjs - 部署到 IBM Cloud 的 React 应用程序中的“无效主机标头”
- security - Cobol 中的错误哈希函数
- error-handling - 如何通过在 Apache-Camel 错误处理程序中重新交付来记录恢复?
- apache-spark - 火花数据框无法替换 NULL 值
- wpf - WPF 在运行时更改按钮样式
- javascript - 添加字符串文件的字符串后缀
- c# - 如何提高 Entity Framework 6 中的更新操作性能