MySQL中对于not in和minus使用的优化
数据库  /  管理员 发布于 6年前   282
优化前:
select count(t.id) from test t where t.status = 1 and t.id not in (select distinct a.app_id from test2 a where a.type = 1and a.rule_id in (152, 153, 154)) 17:20:57 laojiu>@planPLAN_TABLE_OUTPUT――――――――――――――――――――――――――――――――――――――――-Plan hash value: 684502086―――――――――――――――――――――――――――――-| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |―――――――――――――――――――――――――――――-| 0 | SELECT STATEMENT | | 1 | 18 | 176K (2)| 00:35:23 || 1 | SORT AGGREGATE | | 1 | 18 | | ||* 2 | FILTER | | | | | ||* 3 | TABLE ACCESS FULL| test | 1141 | 20538 | 845 (2)| 00:00:11 ||* 4 | TABLE ACCESS FULL| test2 | 1 | 12 | 309 (2)| 00:00:04 |―――――――――――――――――――――――――――――-Predicate Information (identified by operation id):――――――――――――――――― 2 C filter( NOT EXISTS (SELECT /*+ */ 0 FROM “test2″ “A” WHERE “A”.”type”=1 AND (“A”.”RULE_ID”=152 OR “A”.”RULE_ID”=153 OR “A”.”RULE_ID”=154) AND LNNVL(“A”.”APP_ID”<>:B1))) 3 C filter(“T”.”status”=1) 4 C filter(“A”.”type”=1 AND (“A”.”RULE_ID”=152 OR “A”.”RULE_ID”=153 OR “A”.”RULE_ID”=154) AND LNNVL(“A”.”APP_ID”<>:B1))Statistics―――――――――――――――――――- 0 recursive calls 0 db block gets 1762169 consistent gets 0 physical reads 0 redo size 519 bytes sent via SQL*Net to client 492 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed21 rows selected.
优化后:
select count(*) from( select t.id from test t where t.status = 1 minus select distinct a.app_id from test2 a where a.type = 1 and a.rule_id in (152, 153, 154))17:23:33 laojiu>@planPLAN_TABLE_OUTPUT――――――――――――――――――――――――――――――――――――――――-Plan hash value: 631655686――――――――――――――――――――――――――――――――C| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |――――――――――――――――――――――――――――――――C| 0 | SELECT STATEMENT | | 1 | | | 1501 (2)| 00:00:19 || 1 | SORT AGGREGATE | | 1 | | | | || 2 | VIEW | | 1141 | | | 1501 (2)| 00:00:19 || 3 | MINUS | | | | | | || 4 | SORT UNIQUE | | 1141 | 20538 | | 846 (2)| 00:00:11 ||* 5 | TABLE ACCESS FULL| test | 1141 | 20538 | | 845 (2)| 00:00:11 || 6 | SORT UNIQUE | | 69527 | 814K| 3632K| 654 (2)| 00:00:08 ||* 7 | TABLE ACCESS FULL| test2 | 84140 | 986K| | 308 (2)| 00:00:04 |――――――――――――――――――――――――――――――――CPredicate Information (identified by operation id):――――――――――――――――― 5 C filter(“T”.”status”=1) 7 C filter(“A”.”type”=1 AND (“A”.”RULE_ID”=152 OR “A”.”RULE_ID”=153 OR “A”.”RULE_ID”=154))21 rows selected.Statistics―――――――――――――――――――- 1 recursive calls 0 db block gets 2240 consistent gets 0 physical reads 0 redo size 516 bytes sent via SQL*Net to client 492 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 2 sorts (memory) 0 sorts (disk) 1 rows processed
在优化sql的时候,我们需要转变一下思路,等价的改写sql;
改写后的sql由于逻辑读得到了天翻地覆的改变,很快得到结果。
第一条sql执行计划中有一个函数,LNNVL(“A”.”APP_ID”<>:B1),lnnvl(exp)
如果exp的结果是false或者是unknown,那么lnnvl返回true;
如果exp的结果是true,返回false.
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号