《Mysql入門MySQL中對(duì)于not in和minus使用的優(yōu)化》要點(diǎn):
本文介紹了Mysql入門MySQL中對(duì)于not in和minus使用的優(yōu)化,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
優(yōu)化前:
MYSQL數(shù)據(jù)庫
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 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 processed 21 rows selected.
優(yōu)化后:
MYSQL數(shù)據(jù)庫
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 ――――――――――――――――――――――――――――――――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 | ――――――――――――――――――――――――――――――――C Predicate 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
在優(yōu)化sql的時(shí)候,我們需要轉(zhuǎn)變一下思路,等價(jià)的改寫sql;MYSQL數(shù)據(jù)庫
改寫后的sql由于邏輯讀得到了天翻地覆的改變,很快得到結(jié)果.MYSQL數(shù)據(jù)庫
第一條sql執(zhí)行計(jì)劃中有一個(gè)函數(shù),LNNVL(“A”.”APP_ID”<>:B1),lnnvl(exp)MYSQL數(shù)據(jù)庫
如果exp的結(jié)果是false或者是unknown,那么lnnvl返回true;MYSQL數(shù)據(jù)庫
如果exp的結(jié)果是true,返回false.
MYSQL數(shù)據(jù)庫
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/2085.html