• 11g 搜集直方图导致不走索引


    SELECT messagein0_.proccnt AS col_0_0_ FROM mbfe.messagein messagein0_ 
    WHERE messagein0_.msgID='ID:414d5120514d453333313020202020204c224a3c200c49cb';
    
    1条记录
    
    SQL_ID  c1pvjs5wx132x, CHILD NUMBER 0
    -------------------------------------
    SELECT messagein0_.proccnt AS col_0_0_ FROM mbfe.messagein messagein0_ 
    WHERE messagein0_.msgID=:1
     
    PLAN HASH VALUE: 3093528534
     
    -------------------------------------------------------------------------------
    | ID  | Operation         | NAME      | ROWS  | Bytes | COST (%CPU)| TIME     |
    -------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |           |       |       | 71102 (100)|          |
    |*  1 |  TABLE ACCESS FULL| MESSAGEIN |   910K|    47M| 71102   (1)| 00:14:14 |
    -------------------------------------------------------------------------------
     
    Predicate Information (IDENTIFIED BY operation ID):
    ---------------------------------------------------
     
       1 - FILTER("MESSAGEIN0_"."MSGID"=:1)
       
       
    
    SQL> set linesize 200
    SQL> select a.column_name,
      2             b.num_rows,
      3             a.num_distinct Cardinality,
      4              round(a.num_distinct / b.num_rows * 100, 2) selectivity,
      5             a.histogram,
      6            a.num_buckets
      7        from dba_tab_col_statistics a, dba_tables b
      8       where a.owner = b.owner
      9         and a.table_name = b.table_name
     10        and a.owner = 'MBFE'
     11       and a.table_name = upper('messagein'); 
    
    COLUMN_NAME                      NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM       NUM_BUCKETS
    ------------------------------ ---------- ----------- ----------- --------------- -----------
    ID                                1650567     1650567         100 NONE                      1
    MSGID                             1650567     1650567         100 FREQUENCY                 2
    STATUS                            1650567           1           0 NONE                      1
    PROCCNT                           1650567           2           0 NONE                      1
    ACCEPTTIME                        1650567     1271808       77.05 HEIGHT BALANCED         254
    MSGBODY                           1650567           0           0 NONE                      0
    QUEUE                             1650567           4           0 FREQUENCY                 4
    
    7 rows selected.
    
    SELECT /*+ index (messagein0_ msgID_idx1)*/ messagein0_.proccnt AS col_0_0_ FROM mbfe.messagein messagein0_ 
    WHERE messagein0_.msgID='ID:414d5120514d453333313020202020204c224a3c200c49cb';
    
    SQL> select count(distinct(MSGID)) from mbfe.messagein;
    
    COUNT(DISTINCT(MSGID))
    ----------------------
                   1721321
    
    SQL> select count(*) from mbfe.messagein;
    
      COUNT(*)
    ----------
       1721321
    
    
    BEGIN
          DBMS_STATS.GATHER_TABLE_STATS(ownname          => 'MBFE',
                                        tabname          => 'MESSAGEIN',
                                       estimate_percent => 100,
                                        method_opt       => 'for columns MSGID size skewonly',
                                       no_invalidate    => FALSE,
                                       degree           => 16,
                                       cascade          => TRUE);
       END;
    
    
    
    只对end_dt搜集, repeat表示以前收集过直方图,现在收集统计信息的时候就收集直方图,如果以前没收集过直方图,现在收集统计信息的时候就不收集。
    
    
    SQL> set linesize 200
    SQL>  select a.column_name,
      2                 b.num_rows,
      3                 a.num_distinct Cardinality,
      4                  round(a.num_distinct / b.num_rows * 100, 2) selectivity,
      5                 a.histogram,
      6                a.num_buckets
      7            from dba_tab_col_statistics a, dba_tables b
      8           where a.owner = b.owner
      9             and a.table_name = b.table_name
     10           and a.owner = 'MBFE'
     11          and a.table_name = upper('messagein');
    
    COLUMN_NAME                      NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM       NUM_BUCKETS
    ------------------------------ ---------- ----------- ----------- --------------- -----------
    ID                                1723263     1650567       95.78 NONE                      1
    MSGID                             1723263     1723263         100 FREQUENCY                 2
    STATUS                            1723263           1           0 NONE                      1
    PROCCNT                           1723263           2           0 NONE                      1
    ACCEPTTIME                        1723263     1271808        73.8 HEIGHT BALANCED         254
    MSGBODY                           1723263           0           0 NONE                      0
    QUEUE                             1723263           4           0 FREQUENCY                 4
    
    7 rows selected.
    
    SQL> 
    
    SQL> set linesize 200
    SQL> set pagesize 200
    SQL> explain plan for SELECT messagein0_.proccnt AS col_0_0_ FROM mbfe.messagein messagein0_ 
      2  WHERE messagein0_.msgID='ID:414d5120514d453333313020202020204c224a3c200c49cb';
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display());
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 3093528534
    
    -------------------------------------------------------------------------------
    | Id  | Operation         | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
    -------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |           |   928K|    48M| 73327   (1)| 00:14:40 |
    |*  1 |  TABLE ACCESS FULL| MESSAGEIN |   928K|    48M| 73327   (1)| 00:14:40 |
    -------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("MESSAGEIN0_"."MSGID"='ID:414d5120514d45333331302020202020
                  4c224a3c200c49cb')
    
    14 rows selected.
    
    
    
    
    
    SQL> BEGIN
      2        DBMS_STATS.GATHER_TABLE_STATS(ownname          => 'MBFE',
      3                                      tabname          => 'MESSAGEIN',
      4                                     estimate_percent => 100,
      5                                      method_opt       => 'for columns MSGID size skewonly',
      6                                     no_invalidate    => FALSE,
      7                                     degree           => 16,
      8                                     cascade          => TRUE);
      9     END;
     10  /
    
    PL/SQL procedure successfully completed.
    
    SQL>  select a.column_name,
      2                 b.num_rows,
      3                 a.num_distinct Cardinality,
      4                  round(a.num_distinct / b.num_rows * 100, 2) selectivity,
      5                 a.histogram,
      6                a.num_buckets
      7            from dba_tab_col_statistics a, dba_tables b
      8           where a.owner = b.owner
      9             and a.table_name = b.table_name
     10           and a.owner = 'MBFE'
     11          and a.table_name = upper('messagein');
    
    COLUMN_NAME                      NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM       NUM_BUCKETS
    ------------------------------ ---------- ----------- ----------- --------------- -----------
    ID                                1721323     1650567       95.89 NONE                      1
    MSGID                             1721323     1721323         100 FREQUENCY                 2
    STATUS                            1721323           1           0 NONE                      1
    PROCCNT                           1721323           2           0 NONE                      1
    ACCEPTTIME                        1721323     1271808       73.89 HEIGHT BALANCED         254
    MSGBODY                           1721323           0           0 NONE                      0
    QUEUE                             1721323           4           0 FREQUENCY                 4
    
    
    
    SQL> select msgID from mbfe.messagein messagein0_   where rownum<10;
    
    MSGID
    ----------------------------------------------------------------------------------------------------
    ID:414d5120514d454d42464520202020204c24b44120263403
    ID:414d5120514d453333313020202020204c224a3c200c1de7
    ID:414d5120514d454d42464520202020204c24b44120263404
    ID:414d5120514d453333313020202020204c224a3c200c49cb
    ID:414d5120514d454d42464520202020204c24b44120263703
    ID:414d5120514d453333313020202020204c224a3c200fa81d
    ID:414d5120514d453333313020202020204c224a3c200e15d0
    ID:414d5120514d453333313020202020204c224a3c200ff871
    ID:414d5120514d453333313020202020204c224a3c200cd011
    
    
    SQL> explain plan for  SELECT messagein0_.proccnt AS col_0_0_ FROM mbfe.messagein messagein0_ where msgID='ID:414d5120514d454d42464520202020204c24b44120263403';
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display());
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 4204216909
    
    --------------------------------------------------------------------------------------------
    | Id  | Operation		    | Name	   | Rows  | Bytes | Cost (%CPU)| Time	   |
    --------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT	    |		   |	 1 |	55 |	 4   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| MESSAGEIN    |	 1 |	55 |	 4   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN	    | INDEXMSGIN_1 |	 1 |	   |	 3   (0)| 00:00:01 |
    --------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("MSGID"='ID:414d5120514d454d42464520202020204c24b44120263403')
    
    14 rows selected.
    
    
    SQL>  select a.column_name,
                       b.num_rows,
                      a.num_distinct Cardinality,
                        round(a.num_distinct / b.num_rows * 100, 2) selectivity,
                     a.histogram,
                      a.num_buckets
                 from dba_tab_col_statistics a, dba_tables b
                 where a.owner = b.owner
                  and a.table_name = b.table_name
               and a.owner = 'MBFE'
           and a.table_name = upper('messagein');
      2    3    4    5    6    7    8    9   10   11  
    COLUMN_NAME			 NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM	  NUM_BUCKETS
    ------------------------------ ---------- ----------- ----------- --------------- -----------
    ID				   978619      978619	      100 NONE			    1
    MSGID				   978619      978619	      100 NONE			    1
    STATUS				   978619	    1		       0 NONE			    1
    PROCCNT 			   978619	    2		       0 NONE			    1
    ACCEPTTIME			   978619      653888	    66.82 NONE			    1
    MSGBODY 			   978619	    0		      0 NONE			    0
    QUEUE				   978619	    4		     0 NONE			    1
    
    7 rows selected.
    
    
    
    收集直方图后:
    
     BEGIN
              DBMS_STATS.GATHER_TABLE_STATS(ownname          => 'MBFE',
                                           tabname          => 'MESSAGEIN',
                                          estimate_percent => 100,
                                            method_opt       => 'for columns MSGID size skewonly',
                                          no_invalidate    => FALSE,
                                           degree           => 16,
                                           cascade          => TRUE);
           END;
    
    
    SQL> /
    
    COLUMN_NAME			 NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM	  NUM_BUCKETS
    ------------------------------ ---------- ----------- ----------- --------------- -----------
    ID				   978619      978619	      100 NONE			    1
    MSGID				   978619      978619	      100 FREQUENCY		    2
    STATUS				   978619	    1		0 NONE			    1
    PROCCNT 			   978619	    2		0 NONE			    1
    ACCEPTTIME			   978619      653888	    66.82 NONE			    1
    MSGBODY 			   978619	    0		0 NONE			    0
    QUEUE				   978619	    4		0 NONE			    1
    
    7 rows selected.
    
    SQL> explain plan for  SELECT messagein0_.proccnt AS col_0_0_ FROM mbfe.messagein messagein0_ where msgID='ID:414d5120514d454d42464520202020204c24b44120263403';
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display());
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 2678161237
    
    -------------------------------------------------------------------------------
    | Id  | Operation	  | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
    -------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |	      |   454K|    23M| 43978	(1)| 00:08:48 |
    |*  1 |  TABLE ACCESS FULL| MESSAGEIN |   454K|    23M| 43978	(1)| 00:08:48 |
    -------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("MSGID"='ID:414d5120514d454d42464520202020204c24b441202634
    	      03')
    
    14 rows selected.
    
    repeat表示以前收集过直方图,现在收集统计信息的时候就收集直方图,如果以前没收集过直方图,现在收集统计信息的时候就不收集。
    
    skewonly 对所有的列搜集统计信息
    
    干掉直方图信息:
    
     BEGIN
              DBMS_STATS.GATHER_TABLE_STATS(ownname          => 'MBFE',
                                           tabname          => 'MESSAGEIN',
                                          estimate_percent => 100,
                                            method_opt       => 'for columns MSGID size 1',
                                          no_invalidate    => FALSE,
                                           degree           => 16,
                                           cascade          => TRUE);
           END;
    
    SQL> select a.column_name,
                       b.num_rows,
                      a.num_distinct Cardinality,
                        round(a.num_distinct / b.num_rows * 100, 2) selectivity,
                     a.histogram,
                      a.num_buckets
                 from dba_tab_col_statistics a, dba_tables b
                 where a.owner = b.owner
                  and a.table_name = b.table_name
               and a.owner = 'MBFE'
           and a.table_name = upper('messagein');  2    3    4    5    6    7    8    9   10   11  
    
    COLUMN_NAME			 NUM_ROWS CARDINALITY SELECTIVITY HISTOGRAM	  NUM_BUCKETS
    ------------------------------ ---------- ----------- ----------- --------------- -----------
    ID				   978619      978619	      100 NONE			    1
    MSGID				   978619      978619	      100 NONE			    1
    STATUS				   978619	    1		0 NONE			    1
    PROCCNT 			   978619	    2		0 NONE			    1
    ACCEPTTIME			   978619      653888	    66.82 NONE			    1
    MSGBODY 			   978619	    0		0 NONE			    0
    QUEUE				   978619	    4		0 NONE			    1
    
    7 rows selected.
    
    
    此时又可以走索引:
    SQL> explain plan for SELECT messagein0_.proccnt AS col_0_0_ FROM mbfe.messagein messagein0_ where msgID='ID:414d5120514d454d42464520202020204c24b44120263403';
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display());
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 4204216909
    
    --------------------------------------------------------------------------------------------
    | Id  | Operation		    | Name	   | Rows  | Bytes | Cost (%CPU)| Time	   |
    --------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT	    |		   |	 1 |	55 |	 4   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| MESSAGEIN    |	 1 |	55 |	 4   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN	    | INDEXMSGIN_1 |	 1 |	   |	 3   (0)| 00:00:01 |
    --------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("MSGID"='ID:414d5120514d454d42464520202020204c24b44120263403')
    
    14 rows selected.
    
    
    结论:主键列 唯一列 不需要搜集直方图信息
    
    只有列分布不均匀的时候才需要搜集
    

  • 相关阅读:
    mysql存储过程分库分表
    上取整与下取整的转换
    《windows内核安全与驱动开发》ctrl2cap中的ObReferenceObjectByName疑问
    JavaScript基础入门教程(六)
    Unicode中的BOM
    JavaScript基础入门教程(五)
    JavaScript基础入门教程(四)
    JavaScript基础入门教程(三)
    JavaScript基础入门教程(一)
    web及网络基础
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351875.html
Copyright © 2020-2023  润新知