sql - Why isn't index used for this query? -
I had a question where an index was not used when I thought it might be, so I asked her Republished:
Create a test_table
with 1.000.000 rows (10 specific value in col
, some_data
500 bytes of data). LAPAD ('X', 500, 'X') from D-connection to ROWNUM from couple of-DTA & lt; = 1000000); Create test_table as
create table (select MODE (ROWNUM, 10)
Create an index and collect table statistics:
Create an index test_index on test_table (col); EXEC dbms_stats.gather_table_stats ('MY_SCHEMA', 'TEST_TABLE'); col
and COUNT
:
call expansion plan for selection, call by COUNT (*) by test_table group; -------- ---------------------------------------------------- ----------------------- | ID | Operation | Name | Lines | Byte | Cost ( % CPU) | Pahr --------------------------------------------- | - | -------------------------------- | 0 | Select Command | 10 | 30 | 15816 (1 ) | 00:03:10 | 1 | by Hash Group | | 10 | 30 | 15816 (1) | 2 | Table Access Complete | TEST_TABLE | 994K | 2914K | 15755 (1) | 00:03 : 10 --------------------------------------------- --- -----------------------------
Index is not used It does not change by providing the signal.
I think the index can not be used in this case, but why?
I ran the original material of Peter and reprinted its results. I then implemented DCP suggestion ...
SQL & gt; Make changes to the table; Do not empty the table; Table changed to SQL & gt; EXEC dbms_stats.gather_table_stats (user, 'TEST_TABLE', Cascade = & gt; true) The PL / SQL process was successfully completed. SQL & gt; 2 Expansion plan for selection colon, call by group (*) 3 to test_table 4; Explained. SQL & gt; Select * from the table (dbms_xplan.display) 2 / PLAN_TABLE_OUTPUT -------------------------------------- ---------------------------------------------- Plan hash Price: 2099921 975 ------------------------------------------------ - ----------------------------------- | ID | Operation | Name | Rows | Byte | Cost (% CPU) | Time | -------------------------------------------------- ---------------------------------- | 0 | Select Command | | 10 | 30 | 574 (9) || 00:00:07 | | 1 | By Hush Group | 10 | 30 | 574 (9) || 00:00:07 | | 2 | Index Fast Full Scan | TEST_INDEX | 1000K | 2929K | 532 (2) || 00:00:07 | -------------------------------------------------- ---------------------------------- 9 rows selected sql & gt; The reason for this is because zero values are not included in the normal B-TEE index, but Group B has included the group as "value" in your query. By telling the optimizer that there is no cord in col
, it is free to use a more efficient index (I was ending with a FTS of about 3.55 seconds). This is an excellent example of how Metadata can affect adapter. By coincidence, this is clearly a 10g or 11g database, because it uses hash group by algorithms instead of the old sort (group bye) algorithm.
Comments
Post a Comment