About me:Oracle ACE pro,optimistic,passionate and harmonious. Focus on ORACLE,MySQL and other database programming,peformance tuning,db design, j2ee,Linux/AIX,Architecture tech,etc
全部博文(169)
分类: Oracle
2022-04-10 21:06:15
DROP TABLE a; DROP TABLE b; CREATE TABLE a AS SELECT * FROM hr.employees; CREATE TABLE b AS SELECT * FROM hr.employees; --反复插入,构造20万行+ INSERT INTO a SELECT * FROM a; INSERT INTO b SELECT * FROM a; COMMIT; BEGIN dbms_stats.gather_table_stats(ownname => USER,tabname => 'a',estimate_percent => 100,cascade => TRUE); dbms_stats.gather_table_stats(ownname => USER,tabname => 'b',estimate_percent => 100,cascade => TRUE); END; / |
SQL> set autotrace traceonly exp SQL> SELECT * FROM a 2 WHERE EXISTS( 3 SELECT 1 FROM b WHERE a.employee_id=b.employee_id); 执行计划 ---------------------------------------------------------- Plan hash value: 2317816356 --------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 217K| 15M| 1375 (2)| 00:00:17 | |* 1 | HASH JOIN | | 217K| 15M| 1375 (2)| 00:00:17 | | 2 | SORT UNIQUE | | 217K| 848K| 126 (1)| 00:00:02 | | 3 | INDEX FAST FULL SCAN| IDX2_B | 217K| 848K| 126 (1)| 00:00:02 | | 4 | TABLE ACCESS FULL | A | 217K| 14M| 620 (1)| 00:00:08 | --------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("A"."EMPLOYEE_ID"="B"."EMPLOYEE_ID") SQL> SELECT * FROM a 2 WHERE a.employee_id IN (SELECT b.employee_id FROM b); 执行计划 ---------------------------------------------------------- Plan hash value: 2317816356 --------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 217K| 15M| 1375 (2)| 00:00:17 | |* 1 | HASH JOIN | | 217K| 15M| 1375 (2)| 00:00:17 | | 2 | SORT UNIQUE | | 217K| 848K| 126 (1)| 00:00:02 | | 3 | INDEX FAST FULL SCAN| IDX2_B | 217K| 848K| 126 (1)| 00:00:02 | | 4 | TABLE ACCESS FULL | A | 217K| 14M| 620 (1)| 00:00:08 | --------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("A"."EMPLOYEE_ID"="B"."EMPLOYEE_ID") |
SQL> SELECT/*+optimizer_features_enable('8.1.7')*/ * FROM a 2 WHERE EXISTS( 3 SELECT 1 FROM b WHERE a.employee_id=b.employee_id); 已用时间: 00: 00: 00.00 执行计划 ---------------------------------------------------------- Plan hash value: 3422092984 ------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost | ------------------------------------------------------------- | 0 | SELECT STATEMENT | | 10854 | 731K| 344 | |* 1 | FILTER | | | | | | 2 | TABLE ACCESS FULL| A | 10854 | 731K| 344 | |* 3 | INDEX RANGE SCAN | IDX2_B | 2049 | 8196 | 5 | ------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter( EXISTS (SELECT 0 FROM "B" "B" WHERE "B"."EMPLOYEE_ID"=:B1)) 3 - access("B"."EMPLOYEE_ID"=:B1) Note ----- - cpu costing is off (consider enabling it) SQL> SELECT/*+optimizer_features_enable('8.1.7')*/ * FROM a 2 WHERE a.employee_id IN (SELECT b.employee_id FROM b); 已用时间: 00: 00: 00.00 执行计划 ---------------------------------------------------------- Plan hash value: 1679318093 ------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes |TempSpc| Cost | ------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 217K| 16M| | 1126 | |* 1 | HASH JOIN | | 217K| 16M| | 1126 | | 2 | VIEW | VW_NSO_1 | 106 | 1378 | | 779 | | 3 | SORT UNIQUE | | 106 | 424 | 2576K| 779 | | 4 | TABLE ACCESS FULL| B | 217K| 848K| | 344 | | 5 | TABLE ACCESS FULL | A | 217K| 14M| | 344 | ------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("A"."EMPLOYEE_ID"="EMPLOYEE_ID") Note ----- - cpu costing is off (consider enabling it) |
SELECT * FROM a WHERE EXISTS( SELECT 1 FROM b WHERE a.department_id=b.department_id GROUP BY b.department_id HAVING a.salary>=MAX(b.salary)); SQL> @display_cursor SQL_ID dgc8b80sxwct2, child number 1 ------------------------------------- SELECT * FROM a WHERE EXISTS( SELECT 1 FROM b WHERE a.department_id=b.department_id GROUP BY b.department_id HAVING a.salary>=MAX(b.salary)) Plan hash value: 705769378 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 22528 |00:09:13.61 | 32M| |* 1 | FILTER | | 1 | | 22528 |00:09:13.61 | 32M| | 2 | TABLE ACCESS FULL | A | 1 | 217K| 217K|00:00:00.20 | 3733 | |* 3 | FILTER | | 14403 | | 2058 |00:09:13.20 | 32M| | 4 | SORT GROUP BY NOSORT| | 14403 | 1 | 14403 |00:09:13.17 | 32M| |* 5 | TABLE ACCESS FULL | B | 14403 | 19745 | 973M|00:06:17.21 | 32M| ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter( IS NOT NULL) 3 - filter(MAX("B"."SALARY")<=:B1) 5 - filter("B"."DEPARTMENT_ID"=:B1) |
SQL> SELECT * FROM a,(SELECT department_id,MAX(b.salary) max_salary FROM b GROUP BY b.department_id) b1 2 WHERE a.department_id=b1.department_id AND a.salary>=b1.max_salary; 已选择22528行。 已用时间: 00: 00: 00.64 执行计划 ---------------------------------------------------------- Plan hash value: 774961296 ----------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 10854 | 1006K| 1250 (2)| 00:00:16 | |* 1 | HASH JOIN | | 10854 | 1006K| 1250 (2)| 00:00:16 | | 2 | VIEW | | 11 | 286 | 629 (3)| 00:00:08 | | 3 | HASH GROUP BY | | 11 | 88 | 629 (3)| 00:00:08 | | 4 | TABLE ACCESS FULL| B | 217K| 1696K| 620 (1)| 00:00:08 | | 5 | TABLE ACCESS FULL | A | 217K| 14M| 620 (1)| 00:00:08 | ----------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("A"."DEPARTMENT_ID"="B1"."DEPARTMENT_ID") filter("A"."SALARY">="B1"."MAX_SALARY") 统计信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 5986 consistent gets 0 physical reads 0 redo size 1851483 bytes sent via SQL*Net to client 16926 bytes received via SQL*Net from client 1503 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 22528 rows processed |
SELECT * FROM a WHERE EXISTS(SELECT 1 FROM b WHERE a.department_id=b.department_id AND a.employee_id=b.employee_id START WITH b.employee_id =202 CONNECT BY PRIOR b.employee_id=b.manager_id); SQL> set autotrace traceonly exp SQL> SELECT * FROM a WHERE EXISTS(SELECT 1 FROM b WHERE a.department_id=b.department_id AND a.employee_id=b.employee_id 2 START WITH b.employee_id =202 CONNECT BY PRIOR b.employee_id=b.manager_id); 已用时间: 00: 00: 00.00 执行计划 ---------------------------------------------------------- Plan hash value: 985844456 --------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 186 | 12834 | 723K (1)| 02:24:37 | |* 1 | FILTER | | | | | | | 2 | TABLE ACCESS FULL | A | 217K| 14M| 620 (1)| 00:00:08 | |* 3 | FILTER | | | | | | |* 4 | CONNECT BY NO FILTERING WITH SW (UNIQUE)| | | | | | | 5 | TABLE ACCESS FULL | B | 217K| 2545K| 620 (1)| 00:00:08 | --------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter( EXISTS (SELECT 0 FROM "B" "B" WHERE "B"."DEPARTMENT_ID"=:B1 AND "B"."EMPLOYEE_ID"=:B2 START WITH "B"."EMPLOYEE_ID"=202 CONNECT BY "B"."MANAGER_ID"=PRIOR "B"."EMPLOYEE_ID")) 3 - filter("B"."DEPARTMENT_ID"=:B1 AND "B"."EMPLOYEE_ID"=:B2) 4 - access("B"."MANAGER_ID"=PRIOR "B"."EMPLOYEE_ID") filter("B"."EMPLOYEE_ID"=202) |
SQL> SELECT * FROM a WHERE (a.department_id,a.employee_id) IN 2 (SELECT b.department_id,b.employee_id FROM b START WITH b.employee_id =202 CONNECT BY PRIOR b.employee_id=b.manager_id ); 已用时间: 00: 00: 00.13 执行计划 ---------------------------------------------------------- Plan hash value: 1584203533 -------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | -------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2048 | 190K| | 2466 (1)| 00:00:30 | |* 1 | HASH JOIN RIGHT SEMI | | 2048 | 190K| 8064K| 2466 (1)| 00:00:30 | | 2 | VIEW | VW_NSO_1 | 217K| 5514K| | 1617 (62)| 00:00:20 | |* 3 | CONNECT BY NO FILTERING WITH START-WITH| | | | | | | | 4 | TABLE ACCESS FULL | B | 217K| 2545K| | 620 (1)| 00:00:08 | | 5 | TABLE ACCESS FULL | A | 217K| 14M| | 620 (1)| 00:00:08 | -------------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("A"."DEPARTMENT_ID"="DEPARTMENT_ID" AND "A"."EMPLOYEE_ID"="EMPLOYEE_ID") 3 - access("B"."MANAGER_ID"=PRIOR "B"."EMPLOYEE_ID") filter("B"."EMPLOYEE_ID"=202) |