Advertisement
ExaGridDba

Unique index card 10

Jun 24th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. SQL> @ indexcard.sql
  2. SQL> whenever sqlerror exit 1
  3. SQL> set linesize 100
  4. SQL> set trimspool on
  5. SQL> set echo on
  6. SQL> variable x number
  7. SQL> exec :x := 0
  8.  
  9. PL/SQL procedure successfully completed.
  10.  
  11. SQL> drop table puzzle;
  12.  
  13. Table dropped.
  14.  
  15. SQL>
  16. SQL> create table puzzle (
  17. 2 k number,
  18. 3 l number
  19. 4 );
  20.  
  21. Table created.
  22.  
  23. SQL> create unique index puzzle_pk on puzzle ( k );
  24.  
  25. Index created.
  26.  
  27. SQL> insert into puzzle ( k, l ) select level, 0 from dual connect by level <= 10000;
  28.  
  29. 10000 rows created.
  30.  
  31. SQL> commit;
  32.  
  33. Commit complete.
  34.  
  35. SQL> exec dbms_stats.gather_table_stats( ownname=>user, tabname=>'PUZZLE',cascade=>true )
  36.  
  37. PL/SQL procedure successfully completed.
  38.  
  39. SQL> delete from puzzle;
  40.  
  41. 10000 rows deleted.
  42.  
  43. SQL> commit;
  44.  
  45. Commit complete.
  46.  
  47. SQL> insert into puzzle ( k, l ) select level, 0 from dual connect by level <= 1000;
  48.  
  49. 1000 rows created.
  50.  
  51. SQL> commit;
  52.  
  53. Commit complete.
  54.  
  55. SQL> exec dbms_stats.gather_table_stats( ownname=>user, tabname=>'PUZZLE',cascade=>false )
  56.  
  57. PL/SQL procedure successfully completed.
  58.  
  59. SQL> select num_rows from user_tables where table_name = 'PUZZLE';
  60.  
  61. NUM_ROWS
  62. ----------
  63. 1000
  64.  
  65. SQL> select num_rows from user_indexes where index_name = 'PUZZLE_PK';
  66.  
  67. NUM_ROWS
  68. ----------
  69. 10000
  70.  
  71. SQL>
  72. SQL> explain plan for select * from puzzle where k = :x;
  73.  
  74. Explained.
  75.  
  76. SQL> select * from table ( dbms_xplan.display );
  77.  
  78. PLAN_TABLE_OUTPUT
  79. ----------------------------------------------------------------------------------------------------
  80. Plan hash value: 1561462423
  81.  
  82. -----------------------------------------------------------------------------------------
  83. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  84. -----------------------------------------------------------------------------------------
  85. | 0 | SELECT STATEMENT | | 1 | 6 | 2 (0)| 00:00:01 |
  86. | 1 | TABLE ACCESS BY INDEX ROWID| PUZZLE | 1 | 6 | 2 (0)| 00:00:01 |
  87. |* 2 | INDEX UNIQUE SCAN | PUZZLE_PK | 10 | | 1 (0)| 00:00:01 |
  88. -----------------------------------------------------------------------------------------
  89.  
  90. Predicate Information (identified by operation id):
  91. ---------------------------------------------------
  92.  
  93. 2 - access("K"=TO_NUMBER(:X))
  94.  
  95. 14 rows selected.
  96.  
  97. SQL> explain plan for select * from puzzle where k in ( select rownum from dual );
  98.  
  99. Explained.
  100.  
  101. SQL> select * from table ( dbms_xplan.display );
  102.  
  103. PLAN_TABLE_OUTPUT
  104. ----------------------------------------------------------------------------------------------------
  105. Plan hash value: 4207056078
  106.  
  107. ------------------------------------------------------------------------------------------
  108. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  109. ------------------------------------------------------------------------------------------
  110. | 0 | SELECT STATEMENT | | 1 | 19 | 4 (25)| 00:00:01 |
  111. | 1 | NESTED LOOPS | | 1 | 19 | 4 (25)| 00:00:01 |
  112. | 2 | VIEW | VW_NSO_1 | 1 | 13 | 3 (34)| 00:00:01 |
  113. | 3 | COUNT | | | | | |
  114. | 4 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
  115. | 5 | TABLE ACCESS BY INDEX ROWID| PUZZLE | 1 | 6 | 1 (0)| 00:00:01 |
  116. |* 6 | INDEX UNIQUE SCAN | PUZZLE_PK | 10 | | 0 (0)| 00:00:01 |
  117. ------------------------------------------------------------------------------------------
  118.  
  119. Predicate Information (identified by operation id):
  120. ---------------------------------------------------
  121.  
  122. 6 - access("K"="ROWNUM")
  123.  
  124. Note
  125. -----
  126. - this is an adaptive plan
  127.  
  128. 22 rows selected.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement