<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        MySQL5.6為什么關閉元數(shù)據(jù)統(tǒng)計信息自動更新&amp;統(tǒng)計信息收集源代碼

        來源:懂視網(wǎng) 責編:小采 時間:2020-11-09 16:15:08
        文檔

        MySQL5.6為什么關閉元數(shù)據(jù)統(tǒng)計信息自動更新&統(tǒng)計信息收集源代碼

        MySQL5.6為什么關閉元數(shù)據(jù)統(tǒng)計信息自動更新&統(tǒng)計信息收集源代碼:問題描述: MySQL 5.5.15 原sql如下: select constraint_schema,table_name,constraint_name,constraint_type from information_schema.table_constraints where table_schema not in ('informatio
        推薦度:
        導讀MySQL5.6為什么關閉元數(shù)據(jù)統(tǒng)計信息自動更新&統(tǒng)計信息收集源代碼:問題描述: MySQL 5.5.15 原sql如下: select constraint_schema,table_name,constraint_name,constraint_type from information_schema.table_constraints where table_schema not in ('informatio

        問題描述: MySQL 5.5.15 原sql如下: select constraint_schema,table_name,constraint_name,constraint_type from information_schema.table_constraints where table_schema not in ('information_schema', 'mysql', 'test',‘performance_schema’); 不

        問題描述:

        MySQL 5.5.15 原sql如下:

        select constraint_schema,table_name,constraint_name,constraint_type from information_schema.table_constraints where table_schema not in ('information_schema', 'mysql', 'test',‘performance_schema’);

        不只是上面提到的table_constraints,information_schema庫下的一下幾個表,訪問時候都會觸發(fā)這個“順手”操作。

        information_schema.TABLES

        information_schema.STATISTICS

        information_schema.PARTITIONS

        information_schema.KEY_COLUMN_USAGE

        information_schema.TABLE_CONSTRAINTS

        information_schema.REFERENTIAL_CONSTRAINTS

        show table status . .

        show index from ...

        當innodb_stats_on_metadata=on 都會觸發(fā)自動更新統(tǒng)計信息。

        問題:

        5.6 開始默認innodb_stats_on_metadata=off,why??? 答:為了防止自動更新統(tǒng)計信息在DB高峰時導致BP的swap;查詢性能大幅度抖動。

        沒有定期更新統(tǒng)計信息了么??答:有啊,而且可以是持久化的。


        我看到的MySQL 5.5.15 這個版本還是條件是====>

        counter > 2000000000 || ((ib_int64_t)counter > 16 + table->stat_n_rows / 16)

        下面做了對MySQL 收集統(tǒng)計信息做了擴展:


        一.下面針對MySQL community(5.5.15、5.5.36、5.6.16)源代碼分析:

        1.下面是對MySQL-5.5.15 的源代碼的分析:

        ./storage/innobase/row/row0mysql.c

        /*********************************************************************//**
        Updates the table modification counter and calculates new estimates
        for table and index statistics if necessary. */
        UNIV_INLINE
        void
        row_update_statistics_if_needed(
        /*============================*/
        	dict_table_t*	table)	/*!< in: table */
        {
        	ulint	counter;
        
        	counter = table->stat_modified_counter;
        
        	table->stat_modified_counter = counter + 1;
        
        	/* Calculate new statistics if 1 / 16 of table has been modified
        	since the last time a statistics batch was run, or if
        	stat_modified_counter > 2 000 000 000 (to avoid wrap-around).
        	We calculate statistics at most every 16th round, since we may have
        	a counter table which is very small and updated very often. */
        
        	if (counter > 2000000000
        	 || ((ib_int64_t)counter > 16 + table->stat_n_rows / 16)) {
        
        	dict_update_statistics(table, FALSE /* update even if stats
        	 are initialized */);
        	}
        }

        從上面可以看出更新統(tǒng)計信息的條件是:

        counter > 2000000000 || ((ib_int64_t)counter > 16 + table->stat_n_rows / 16)


        2.下面是對MySQL-5.5.36 的源代碼的分析:

        ---------------------------------------------------------------------------

        #通過更新統(tǒng)計信息stat_modified_counter,每個表都有這個表里來維護:

        ./storage/innobase/row/row0mysql.c

        /*********************************************************************//**
        Updates the table modification counter and calculates new estimates
        for table and index statistics if necessary. */
        UNIV_INLINE
        void
        row_update_statistics_if_needed(
        /*============================*/
         dict_table_t* table) /*!< in: table */
        {
         ulint counter;
        
         counter = table->stat_modified_counter;
        
         table->stat_modified_counter = counter + 1;
        
         if (DICT_TABLE_CHANGED_TOO_MUCH(table)) {
        
         dict_update_statistics(
         table,
         FALSE, /* update even if stats are initialized */
         TRUE /* only update if stats changed too much */);
         }
        }
        
        /*********************************************************************/

        規(guī)則:每一次DML操作導致1 行更新,stat_modified_counter加1,直到滿足更新統(tǒng)計信息的條件,stat_modified_counter的值自動重置為0。


        #更新統(tǒng)計信息的條件:(有超過1/16的row被更改過會更新表的條件信息)

        ./storage/innobase/include/dict0dict.h

        /** Calculate new statistics if 1 / 16 of table has been modified
        since the last time a statistics batch was run.
        We calculate statistics at most every 16th round, since we may have
        a counter table which is very small and updated very often.
        @param t table
        @return true if the table has changed too much and stats need to be
        recalculated
        */
        #define DICT_TABLE_CHANGED_TOO_MUCH(t) \
         ((ib_int64_t) (t)->stat_modified_counter > 16 + (t)->stat_n_rows / 16)
        
        /*********************************************************************/


        * 這樣有個性能問題,若有多個線程同時檢測到閾值,也即是并發(fā)調用會多次,,會導致dict_update_statistics函數(shù)多次的調用,浪費了系統(tǒng)資源。

        解決方法:在dict_update_statistics{}函數(shù)對stat_modified_counter加鎖,避免并發(fā)執(zhí)行。


        #統(tǒng)計新跟更新函數(shù):dict_update_statistics

        ./storage/innobase/dict/dict0dict.c

        /*********************************************************************//**
        Calculates new estimates for table and index statistics. The statistics
        are used in query optimization. */
        UNIV_INTERN
        void
        dict_update_statistics(
        /*===================*/
         dict_table_t* table, /*!< in/out: table */
         ibool only_calc_if_missing_stats,/*!< in: only
         update/recalc the stats if they have
         not been initialized yet, otherwise
         do nothing */
         ibool only_calc_if_changed_too_much)/*!< in: only
         update/recalc the stats if the table
         has been changed too much since the
         last stats update/recalc */
        {
         dict_index_t* index;
         ulint sum_of_index_sizes = 0;
        
         DBUG_EXECUTE_IF("skip_innodb_statistics", return;);
        -----------------------------------------------------------------------------

        可以優(yōu)化成:

        ---------------------------------------------------------------------------

        1) 加x鎖

        2) 索引統(tǒng)計

        3) stat_modified_counter 置0

        4) 解鎖

        ---------------------------------------------------------------------------

        3.下面我們來看下對MySQL 5.6.16 的源代碼的分析:

        MySQL版本:MySQL 5.6.16-log。

        ./storage/innobase/row/row0mysql.cc

        void
        row_update_statistics_if_needed(
        /*============================*/
         dict_table_t* table) /*!< in: table */
        {
         ib_uint64_t counter;
         ib_uint64_t n_rows;
        
         if (!table->stat_initialized) {
         DBUG_EXECUTE_IF(
         "test_upd_stats_if_needed_not_inited",
         fprintf(stderr, "test_upd_stats_if_needed_not_inited "
         "was executed\n");
         );
         return;
         }
        
         counter = table->stat_modified_counter++;
         n_rows = dict_table_get_n_rows(table);
        
         if (dict_stats_is_persistent_enabled(table)) {
         if (counter > n_rows / 10 /* 10% */
         && dict_stats_auto_recalc_is_enabled(table)) {
        
         dict_stats_recalc_pool_add(table);
         table->stat_modified_counter = 0;
         }
         return;
         }
        
         /* Calculate new statistics if 1 / 16 of table has been modified
         since the last time a statistics batch was run.
         We calculate statistics at most every 16th round, since we may have
         a counter table which is very small and updated very often. */
        
         if (counter > 16 + n_rows / 16 /* 6.25% */) {
        
         ut_ad(!mutex_own(&dict_sys->mutex));
         /* this will reset table->stat_modified_counter to 0 */
         dict_stats_update(table, DICT_STATS_RECALC_TRANSIENT);
         }
        }
        
        /*********************************************************************/

        從上面的代碼看可以看出:

        1.對InnoDB表統(tǒng)計信息持久化時,表的row發(fā)生變化大于10%(counter > n_rows / 10 /* 10%)并且innodb_stats_auto_recalc=on,統(tǒng)計信信息會更新(雖然innodb_stats_auto_recalc=on是自動重新計算,但是也是異步的,可能會延時,比如當瞬間的DML批量操作就可能有延時)

        2.統(tǒng)計信息非持久化還是和5.5 一致的(表的row發(fā)生變化大于1/16時更新統(tǒng)計信息)

        3.切記:不能完全依賴于MySQL本身的機制來更新統(tǒng)計信息,線上一些表不及時更新統(tǒng)計信息的我遇到過多次,針對這樣的表,我在夜間定期analyze table xxx;


        二.MySQL 5.6的改進:

        可以配置統(tǒng)計信息的持久化和非持久化(非持久化:5.6之前都是這種)

        相關參數(shù):

        持久化:

        innodb_stats_persistent:on(1)

        innodb_stats_persistent_sample_pages:20

        非持久化:

        innodb_stats_sample_pages:8

        相關表:

        mysql.innodb_index_stats

        mysql.innodb_table_stats

        From 5.6.6 開始,統(tǒng)計信息默認是持久化的(即innodb_stats_persistent=on),使用參數(shù)innodb_stats_persistent_sample_pages的值,來采樣,此時非持久化的參數(shù)innodb_stats_sample_pages就無效。

        From 5.6.6 開始,使用非持久化的統(tǒng)計信息:

        1.set innodb_stats_persistent=0;

        2.create|alter table stats_persistent=0;

        對單個表開啟:

        create|alter table...STATS_PERSISTENT [=] {DEFAULT|0|1}

        DEFAULT:table的統(tǒng)計信息是否持久化由參數(shù) innodb_stats_persistent 決定。\

        總結:From 5.6.6 開始,要么開啟統(tǒng)計信息持久化,要么是還用以前的非持久化,二者選一。


        參考相關參數(shù):

        innodb_stats_method: nulls_equal, nulls_unequal, and nulls_ignored
        myisam_stats_method:nulls_equal, nulls_unequal, and nulls_ignored

        --------------------------------------------------------------

        基數(shù)即value group=N/s (N:表行數(shù) S:average group size)
        基數(shù)(VG)|值組為不重復的值的個數(shù)

        nulls_equal:所有的NULL都相等,算作一個值組,這樣一旦null值很多的情況下,average group size偏大,導致基數(shù)偏小。

        nulls_unequal:每一個NULL都相等,算作一個值組,這樣一旦null值很多的情況下,如果non-null值組大,而null的值組過多,導致average group size偏小,導致基數(shù)偏大,可能導致誤走索引

        nulls_ignored:所有的null都忽略,不記錄索引。

        --------------------------------------------------------------

        參考:

        # http://dev.mysql.com/doc/refman/5.6/en/innodb-parameters.html#sysvar_innodb_stats_method

        聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        MySQL5.6為什么關閉元數(shù)據(jù)統(tǒng)計信息自動更新&統(tǒng)計信息收集源代碼

        MySQL5.6為什么關閉元數(shù)據(jù)統(tǒng)計信息自動更新&統(tǒng)計信息收集源代碼:問題描述: MySQL 5.5.15 原sql如下: select constraint_schema,table_name,constraint_name,constraint_type from information_schema.table_constraints where table_schema not in ('informatio
        推薦度:
        標簽: 關閉 信息 為什么
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 59pao成国产成视频永久免费| 国产黄片不卡免费| 最近中文字幕2019高清免费 | 日本一区免费电影| 日本亚洲色大成网站www久久| 91在线视频免费播放| 亚洲日本乱码卡2卡3卡新区| 好爽又高潮了毛片免费下载| 亚洲美国产亚洲AV| 免费va在线观看| 国产精品美女久久久免费 | 成年性生交大片免费看| 亚洲一线产区二线产区区| 免费观看毛片视频| 免费人成再在线观看网站| 中文字幕一精品亚洲无线一区| 中文无码日韩欧免费视频| 亚洲欧洲第一a在线观看| 精品久久8x国产免费观看| 亚洲伊人久久大香线蕉AV| 亚洲A丁香五香天堂网| a毛片免费全部在线播放**| 亚洲天堂男人天堂| 日韩一区二区三区免费体验| 一级免费黄色大片| 久久丫精品国产亚洲av不卡| 99在线视频免费观看视频 | 免费人成激情视频在线观看冫 | 在线观看www日本免费网站| 精品亚洲456在线播放| 免费中文字幕在线| 免费播放在线日本感人片| 亚洲人成网站日本片| 国产jizzjizz视频全部免费| 两个人看www免费视频| 精品久久亚洲中文无码| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲国产精品乱码一区二区| 性做久久久久久久免费看| 一级毛片一级毛片免费毛片 | 高清在线亚洲精品国产二区|