sql - SQL 性能优化
问题描述
我得到了两个返回相同结果的 SQL,我原以为 SQL2 会比 SQL1 快,但事实证明 SQL1 比 SQL2 快 5 倍。有人可以向我解释一下吗?提前致谢。
SQL1: 2 seconds
select count(*) from (
select * from (
SELECT distinct job_id, REGEXP_SUBSTR(JOB_Description, '(ABC|CDE)([[:digit:]]){5}', 1, LEVEL) AS substr
FROM (
select job_id, JOB_DESCRIPTION from smms.job where TO_NUMBER(to_char(CREATE_DATE,'YYYY'))=2017
)
CONNECT BY LEVEL <= REGEXP_COUNT(JOB_Description, '(ABC|CDE)([[:digit:]]){5}')
and prior job_description = job_description
AND PRIOR DBMS_RANDOM.VALUE IS NOT null
order by job_id
) **where substr is not null**);
SQL2: 11 seconds
select count(*) from (
SELECT distinct job_id, REGEXP_SUBSTR(JOB_Description, '(ABC|CDE)([[:digit:]]){5}', 1, LEVEL) AS substr
FROM (
select job_id, JOB_DESCRIPTION from smms.job where TO_NUMBER(to_char(CREATE_DATE,'YYYY'))=2017 **and REGEXP_COUNT(JOB_Description, '(ABC|CDE)([[:digit:]]){5}')>0**
)
CONNECT BY LEVEL <= REGEXP_COUNT(JOB_Description, '(ABC|CDE)([[:digit:]]){5}')
and prior job_description = job_description
AND PRIOR DBMS_RANDOM.VALUE IS NOT null
order by job_id
);
解决方案
推荐阅读
- cobol - 在 COBOL 中取消字符串
- python - Django 1.11 - CreateView - 无效表单引发不存在对象属性的错误
- r - R&dplyr - 使用基于另一列的键的bin变量
- php - 我无法通过 webscraper 获取整个源代码 - SIMPLE HTML DOM
- python - 通过参考图像测量照片中物体的大小
- r - 在 Shiny App 的每次迭代中单击多个绘图
- google-cloud-datastore - 使用 Google Cloud Datastore 创建和复制多级嵌入式实体
- javascript - 如何把它变成一个承诺?
- https - AWS CloudFormation Application Load Balancer - 如何将 HTTP 侦听器重定向到 HTTPS 侦听器?
- python-3.x - 如何在没有语法形式的情况下获取 nltk 树的节点?