【发布时间】:2012-05-01 09:46:30
【问题描述】:
我将其用作子选择(在连接其他 2 个表的简单查询中),您可以想象它需要一段时间才能运行。到目前为止,持续了 6 小时以上。 这是唯一的方法吗?我知道做另一个 JOIN 而不是子选择可能会有所帮助。但主要瓶颈是所有这些 OR 和子字符串。
SELECT ex_array
FROM service_x
WHERE
( substr(ex_array,1,2) = 'FW' OR substr(ex_array,3,2) = 'FW' OR substr(ex_array,5,2) = 'FW' OR substr(ex_array,7,2) = 'FW' OR substr(ex_array,9,2) = 'FW' OR substr(ex_array,11,2) = 'FW' )
OR ( substr(ex_array,1,2) = 'IL' OR substr(ex_array,3,2) = 'IL' OR substr(ex_array,5,2) = 'IL' OR substr(ex_array,7,2) = 'IL' OR substr(ex_array,9,2) = 'IL' OR substr(ex_array,11,2) = 'IL' )
OR ( substr(ex_array,1,2) = 'IN' OR substr(ex_array,3,2) = 'IN' OR substr(ex_array,5,2) = 'IN' OR substr(ex_array,7,2) = 'IN' OR substr(ex_array,9,2) = 'IN' OR substr(ex_array,11,2) = 'IN' )
OR ( substr(ex_array,1,2) = 'IK' OR substr(ex_array,3,2) = 'IK' OR substr(ex_array,5,2) = 'IK' OR substr(ex_array,7,2) = 'IK' OR substr(ex_array,9,2) = 'IK' OR substr(ex_array,11,2) = 'IK' )
OR ( substr(ex_array,1,2) = 'IH' OR substr(ex_array,3,2) = 'IH' OR substr(ex_array,5,2) = 'IH' OR substr(ex_array,7,2) = 'IH' OR substr(ex_array,9,2) = 'IH' OR substr(ex_array,11,2) = 'IH' )
OR ( substr(ex_array,1,2) = 'KP' OR substr(ex_array,3,2) = 'KP' OR substr(ex_array,5,2) = 'KP' OR substr(ex_array,7,2) = 'KP' OR substr(ex_array,9,2) = 'KP' OR substr(ex_array,11,2) = 'KP' )
)
【问题讨论】:
-
您是否有查询的解释计划(查看 dbms_xplan 以获得此计划)?我希望查询可以通过全表扫描得到满足,因为您已经通过将函数应用于 ex_array 来使用任何标准索引停止数据库。 @dcp 建议的六个基于函数的索引可能会有所帮助。 service_x 有多少行,您希望查询返回多少行?您可能需要重新评估您的整体算法/设计以获得所需的性能。不过,关键可能是启用索引访问,具体取决于查询的选择性。
-
大约 1220 万行(在 service_x 中),我应该得到大约 175,000 个结果。
-
鉴于查询的高选择性 - 我建议如果您无法调整整个算法,您最好的选择是修改查询以通过对 ex_array 的索引访问来启用有效的行过滤器。我将在下面发布一个可能会有所帮助的答案。