MySQL中对于not in和minus使用的优化
作者:罗龙九 时间:2024-01-17 04:17:20
优化前:
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 = 1
and a.rule_id in (152, 153, 154))
17:20:57 laojiu>@plan
PLAN_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 – 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 – filter(“T”.”status”=1)
4 – 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 processed
21 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>@plan
PLAN_TABLE_OUTPUT
————————————————————————————————————————-
Plan hash value: 631655686
————————————————————————————————–
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
————————————————————————————————–
| 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 |
————————————————————————————————–
Predicate Information (identified by operation id):
—————————————————
5 – filter(“T”.”status”=1)
7 – 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.
标签:MySQL
0
投稿
猜你喜欢
基于pygame实现童年掌机打砖块游戏
2023-09-18 20:41:28
Python入门教程之运算符重载详解
2021-10-12 20:15:28
详解python读取matlab数据(.mat文件)
2021-03-04 19:29:29
利用python3如何给数据添加高斯噪声
2023-06-11 20:52:17
JavaScript 各种动画渐变效果
2008-09-02 10:38:00
Django DRF认证组件流程实现原理详解
2021-01-03 08:48:49
详解Python给照片换底色(蓝底换红底)
2023-02-03 02:07:45
纯js封装的ajax功能函数与用法示例
2024-05-11 09:09:20
为什么str(float)在Python 3中比Python 2返回更多的数字
2022-11-09 22:55:56
Python使用BeautifulSoup库解析HTML基本使用教程
2021-12-22 17:48:05
python求众数问题实例
2022-02-06 22:25:40
当达到输入长度时表单自动切换焦点
2024-05-02 17:29:33
windows+apache+mod_python配置django运行环境
2021-02-01 04:06:54
在PB中如何让用户只能修改新增的数据
2023-11-27 15:59:52
Oracle AS关键字 提示错误
2011-04-18 12:42:00
Python编程pydantic触发及访问错误处理
2021-05-19 20:49:07
python画立方体--魔方
2022-04-22 10:20:43
python办公自动化(Excel)的实例教程
2022-02-27 07:37:16
jsp include文件时的一个乱码解决方法
2024-03-27 19:34:28
python 偷懒技巧——使用 keyboard 录制键盘事件
2023-02-08 18:00:03