这篇文章主要介绍SQL优化之如何使用索引,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!下面sql30秒执行出结果,查看sql谓词中有like,我们知道谓词中有这样的语句是不走索引的(为了保护客户的隐私,表名和部分列已经重命名)。1234567891011121314SELECT
/*+1*/
CHECKNUM
AS
PINGZBSM,
CHECKDATE,
XXXMODE,
XXXRESULT,
(
SELECT
RESULT
FROM
(
select
ID,to_char(WMSYS.WM_CONCAT(xxxnum||xxxtype||xxxmode||xxxresult))RESULT
from
OOOO_XXXCHECKLOG
WHERE
CHECKDATE
BETWEEN
DATE
'2018-05-04'
AND
DATE
'2018-05-04'
and
xxxtype
like
'%PAR'
GROUP
BY
ID
)b
where
b.id=a.id
)RESULT,
CLERKNUM
AS
CHECKNUM
FROM
OOO_XXXECHECKLOGA;
逻辑读600多万。查看索引情况如下表过滤返回数据量如下:123456SQL>
select
count
(*)
from
OOOO_XXXCHECKLOG;
2799616
select
count
(*)
from
OOOO_XXXCHECKLOG
WHERE
CHECKDATE
BETWEEN
DATE
'2018-05-04'
AND
DATE
'2018-05-04'
and
xxxtype
like
'%PAR'
;
12856
select
count
(*)
from
OOOO_XXXCHECKLOG
WHERE
CHECKDATE
BETWEEN
DATE
'2018-05-04'
AND
DATE
'2018-05-04'
;
197984
通过查询上面返回数据可知,因为xxxtype不走索引,所以通过索引要回表197984次,如果走了索引只回表12856次。下面我们建立REVERSE索引IDX_ID_TYPE_RE1234567891011121314SELECT
/*+OOOO_XXXCHECKLOG
index
(IDX_ID_TYPE_RE)2*/
CHECKNUM
AS
PINGZBSM,
CHECKDATE,
XXXMODE,
XXXRESULT,
(
SELECT
RESULT
FROM
(
select
ID,to_char(WMSYS.WM_CONCAT(xxxnum||xxxtype||xxxmode||xxxresult))RESULT
from
OOOO_XXXCHECKLOG
WHERE
CHECKDATE
BETWEEN
DATE
'2018-05-04'
AND
DATE
'2018-05-04'
and
REVERSE(xxxtype)
like
'RAP%'
GROUP
BY
ID
)b
where
b.id=a.id
)RESULT,
CLERKNUM
AS
CHECKNUM
FROM
OOO_XXXECHECKLOGA;
查看执行计划如下,逻辑读将为300万,但是时间还是维持在18秒,根本原因在于这个索引因为标量子查询的问题被访问700万次导致。下面我们改写sql如下12345678910111213SELECT
/*+
index
(OOOO_XXXCHECKLOGIDX_ID_TYPE_RE)3*/
CHECKNUM
AS
PINGZBSM,
CHECKDATE,
XXXMODE,
XXXRESULT,
B.RESULT,
CLERKNUM
AS
CHECKNUM
FROM
OOO_XXXECHECKLOGA
left
join
(
select
ID,to_char(WMSYS.WM_CONCAT(xxxnum||xxxtype||xxxmode||xxxresult))RESULT
from
OOOO_XXXCHECKLOG
WHERE
CHECKDATE
BETWEEN
DATE
'2018-05-04'
AND
DATE
'2018-05-04'
and
REVERSE(xxxtype)
like
'RAP%'
GROUP
BY
ID
)b
on
b.id=a.id;
执行计划中出现index_skip_scan免费云主机域名。下面我们创建如下索引:1create
index
idx_date_seal_re
on
OOOO_XXXCHECKLOG(CHECKDATE,REVERSE(xxxtype));
可以看到,逻辑读降到64424,50个物理读是因为刚刚创建索引的原因,sql也秒出。以上是“SQL优化之如何使用索引”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注百云行业资讯频道!
相关推荐: Oracle Database Server ‘TNS Listener’远程数据投毒漏洞(CVE-2012-1675)解决
环境:Windows 2008 R2 +Oracle10.2.0.3 应用最新bundle patch后,扫描依然报出漏洞 Oracle Database Server ‘TNS Listener’远程数据投毒漏洞(CVE-2012-1675) 安全厂家给出的…
免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。