成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

事務處理對持久統計信息自動收集的影響

存儲 數據管理
系統變量innodb_stats_auto_recalc(默認ON)控制表行更改超過10%時,是否自動計算統計信息。也可以通過創建或更改表時指定stats_auto_recalc子句為單個表配置自動統計重新計算。

1. 持久化統計信息

持久統計信息將統計信息存儲到磁盤,使它們在服務器重啟后保持不變,優化器更有可能在查詢時做出一致的選擇,從而提高執行計劃的穩定性。

在innodb_stats_persistent=ON(默認值)或表定義使用 stats_persistent=1時,優化器統計信息會持久化保存。

持久統計信息存儲在 mysql.innodb_table_stats 和 mysql.innodb_index_stats 表中,last_update 列可以看到上次更新統計信息的時間。

系統變量innodb_stats_auto_recalc(默認ON)控制表行更改超過10%時,是否自動計算統計信息。也可以通過創建或更改表時指定stats_auto_recalc子句為單個表配置自動統計重新計算。

由于自動統計信息收集是一個后臺線程,其處理過程與DML操作是異步的,在DML操作超過 10% 的表后,可能不會立即重新計算統計信息。在某些情況下,統計數據重新計算可能會延遲幾秒鐘。如果立即需要最新的統計信息,執行 ANALYZE TABLE 以啟動統計信息的同步計算。

事務的 commit 和 rollback 會影響統計信息的自動收集么?通過下面測試,可以回答這問題。

2. 測試commit和rollback對持久統計信息收集的影響

測試環境的系統變量值:

greatsql> SHOW GLOBALVARIABLESLIKE'innodb_stats%';
+--------------------------------------+-------------+
| Variable_name                        | Value       |
+--------------------------------------+-------------+
| innodb_stats_auto_recalc             | ON          |
| innodb_stats_include_delete_marked   | OFF         |
| innodb_stats_method                  | nulls_equal |
| innodb_stats_on_metadata             | OFF         |
| innodb_stats_persistent              | ON          |
| innodb_stats_persistent_sample_pages | 20          |
| innodb_stats_transient_sample_pages  | 8           |
+--------------------------------------+-------------+

2.1 INSERT 操作

greatsql> TRUNCATE TABLE test1;
Query OK, 0 rows affected (0.05 sec)

-- 開啟事務,在空表test1中插入10萬行數據
greatsql> BEGIN;
Query OK, 0 rows affected (0.00 sec)

greatsql> SELECTnow();INSERTINTO test1 SELECT * FROMLIMIT100000;SELECTnow();
+---------------------+
| now()               |
+---------------------+
| 2025-01-07 09:59:19 |
+---------------------+
1 row in set (0.00 sec)

Query OK, 100000rows affected (2.73 sec)
Records: 100000  Duplicates: 0Warnings: 0

+---------------------+
| now()               |
+---------------------+
| 2025-01-0709:59:21 |
+---------------------+
1rowinset (0.00 sec)


-- 事務沒有提交,但統計信息已收集
greatsql> SELECT * FROM mysql.innodb_index_stats WHERE database_name='testdb1'AND table_name='test1';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:59:19 | n_diff_pfx01 |         11 |           1 | id                                |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:59:19 | n_leaf_pages |          1 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:59:19 | size         |          1 |        NULL | Number of pages in the index      |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:19 | n_diff_pfx01 |         11 |           1 | k                                 |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:19 | n_diff_pfx02 |         11 |           1 | k,id                              |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:19 | n_leaf_pages |          1 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:19 | size         |          1 |        NULL | Number of pages in the index      |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
7 rows in set (0.00 sec)

greatsql> SELECT * FROM mysql.innodb_index_stats WHERE database_name='testdb1'AND table_name='test1';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:59:29 | n_diff_pfx01 |      98712 |          20 | id                                |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:59:29 | n_leaf_pages |       1371 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:59:29 | size         |       1379 |        NULL | Number of pages in the index      |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:29 | n_diff_pfx01 |      30169 |          20 | k                                 |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:29 | n_diff_pfx02 |     100268 |          20 | k,id                              |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:29 | n_leaf_pages |        128 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:29 | size         |        161 |        NULL | Number of pages in the index      |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
7 rows in set (0.00 sec)


-- 回滾事務
greatsql> ROLLBACK;
Query OK, 0 rows affected (2.64 sec)

-- 沒有重新收集統計信息,統計信息與表數據不匹配
greatsql> SELECT * FROM mysql.innodb_index_stats WHERE database_name='testdb1'AND table_name='test1';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:59:29 | n_diff_pfx01 |      98712 |          20 | id                                |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:59:29 | n_leaf_pages |       1371 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:59:29 | size         |       1379 |        NULL | Number of pages in the index      |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:29 | n_diff_pfx01 |      30169 |          20 | k                                 |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:29 | n_diff_pfx02 |     100268 |          20 | k,id                              |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:29 | n_leaf_pages |        128 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | k_1        | 2025-01-07 09:59:29 | size         |        161 |        NULL | Number of pages in the index      |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
7 rows in set (0.00 sec)

greatsql> SELECTCOUNT(*) FROM test1;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)


-- analyze重新收集統計信息,統計信息才和表數據一致
greatsql> ANALYZETABLE test1;
+---------------+---------+----------+----------+
| Table         | Op      | Msg_type | Msg_text |
+---------------+---------+----------+----------+
| testdb1.test1 | analyze | status   | OK       |
+---------------+---------+----------+----------+
1rowinset (0.06 sec)

greatsql> SELECT * FROM mysql.innodb_index_stats WHERE database_name='testdb1'AND table_name='test1';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| testdb1       | test1      | PRIMARY    | 2025-01-07 10:01:58 | n_diff_pfx01 |          0 |           1 | id                                |
| testdb1       | test1      | PRIMARY    | 2025-01-07 10:01:58 | n_leaf_pages |          1 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | PRIMARY    | 2025-01-07 10:01:58 | size         |          1 |        NULL | Number of pages in the index      |
| testdb1       | test1      | k_1        | 2025-01-07 10:01:58 | n_diff_pfx01 |          0 |           1 | k                                 |
| testdb1       | test1      | k_1        | 2025-01-07 10:01:58 | n_diff_pfx02 |          0 |           1 | k,id                              |
| testdb1       | test1      | k_1        | 2025-01-07 10:01:58 | n_leaf_pages |          1 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | k_1        | 2025-01-07 10:01:58 | size         |          1 |        NULL | Number of pages in the index      |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
7 rows in set (0.00 sec)

2.2 DELETE 操作

greatsql> SELECT COUNT(*) FROM test1;
+----------+
| count(*) |
+----------+
|   100000 |
+----------+
1 row in set (0.15 sec)

-- 開啟事務,執行delete操作
greatsql> BEGIN;
Query OK, 0 rows affected (0.00 sec)

greatsql> SELECTnow();DELETEFROM test1;SELECTnow();
+---------------------+
| now()               |
+---------------------+
| 2025-01-07 09:41:36 |
+---------------------+
1 row in set (0.00 sec)

Query OK, 100000rows affected (1.87 sec)

+---------------------+
| now()               |
+---------------------+
| 2025-01-0709:41:38 |
+---------------------+
1rowinset (0.00 sec)

-- 在delete開始時,進行了一次統計信息收集
greatsql> SELECT * FROM mysql.innodb_index_stats WHERE database_name='testdb1'AND table_name='test1';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:41:36 | n_diff_pfx01 |      98712 |          20 | id                                |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:41:36 | n_leaf_pages |       1371 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:41:36 | size         |       1379 |        NULL | Number of pages in the index      |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:36 | n_diff_pfx01 |      32313 |          20 | k                                 |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:36 | n_diff_pfx02 |      99244 |          20 | k,id                              |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:36 | n_leaf_pages |        128 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:36 | size         |        161 |        NULL | Number of pages in the index      |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
7 rows in set (0.00 sec)

-- delete完成一段時間后(約10秒)進行了第二次統計信息收集
greatsql> SELECT * FROM mysql.innodb_index_stats WHERE database_name='testdb1'AND table_name='test1';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:41:46 | n_diff_pfx01 |          0 |          20 | id                                |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:41:46 | n_leaf_pages |       1371 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:41:46 | size         |       1379 |        NULL | Number of pages in the index      |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:46 | n_diff_pfx01 |          0 |          20 | k                                 |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:46 | n_diff_pfx02 |          0 |          20 | k,id                              |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:46 | n_leaf_pages |        128 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:46 | size         |        161 |        NULL | Number of pages in the index      |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
7 rows in set (0.00 sec)

-- 回滾事務
greatsql> ROLLBACK;
Query OK, 0 rows affected (1.95 sec)

-- 事務回滾后,統計信息與表數據不匹配
greatsql> SELECT * FROM mysql.innodb_index_stats WHERE database_name='testdb1'AND table_name='test1';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:41:46 | n_diff_pfx01 |          0 |          20 | id                                |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:41:46 | n_leaf_pages |       1371 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:41:46 | size         |       1379 |        NULL | Number of pages in the index      |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:46 | n_diff_pfx01 |          0 |          20 | k                                 |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:46 | n_diff_pfx02 |          0 |          20 | k,id                              |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:46 | n_leaf_pages |        128 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | k_1        | 2025-01-07 09:41:46 | size         |        161 |        NULL | Number of pages in the index      |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
7 rows in set (0.00 sec)

greatsql> SELECTCOUNT(*) FROM test1;
+----------+
| count(*) |
+----------+
|   100000 |
+----------+
1 row in set (0.15 sec)

-- analyze重新收集統計信息,統計信息才和表數據一致
greatsql> ANALYZETABLE test1;
+---------------+---------+----------+----------+
| Table         | Op      | Msg_type | Msg_text |
+---------------+---------+----------+----------+
| testdb1.test1 | analyze | status   | OK       |
+---------------+---------+----------+----------+
1rowinset (0.08 sec)

greatsql> SELECT * FROM mysql.innodb_index_stats WHERE database_name='testdb1'AND table_name='test1';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:47:29 | n_diff_pfx01 |      98712 |          20 | id                                |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:47:29 | n_leaf_pages |       1371 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | PRIMARY    | 2025-01-07 09:47:29 | size         |       1379 |        NULL | Number of pages in the index      |
| testdb1       | test1      | k_1        | 2025-01-07 09:47:29 | n_diff_pfx01 |      32332 |          20 | k                                 |
| testdb1       | test1      | k_1        | 2025-01-07 09:47:29 | n_diff_pfx02 |     100051 |          20 | k,id                              |
| testdb1       | test1      | k_1        | 2025-01-07 09:47:29 | n_leaf_pages |        128 |        NULL | Number of leaf pages in the index |
| testdb1       | test1      | k_1        | 2025-01-07 09:47:29 | size         |        161 |        NULL | Number of pages in the index      |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
7 rows in set (0.01 sec)

3. 總結

  • 數據量變化大(超過10%)的DML操作會導致2次統計信息收集,一次是DML開始時,一次是DML完成約10秒后。
  • DML操作是否COMMIT提交,不影響統計信息收集。
  • DML操作的rollback回滾,可能造成統計信息與表數據不一致。當大數據DML操作回滾后,可以執行ANALYZE TABLE重新收集表的統計信息。
責任編輯:武曉燕 來源: GreatSQL
相關推薦

2009-07-15 17:41:55

iBATIS事務處理

2009-09-14 19:55:03

LINQ事務處理

2011-04-27 15:55:16

2010-04-13 15:44:00

Oracle與SqlS

2014-01-22 13:37:53

2009-07-09 18:15:42

JDBC事務處理

2009-04-15 17:03:15

OracleSQL Server事務處理

2010-05-17 14:59:05

MySQL事務處理

2009-11-13 17:01:07

ADO.NET事務處理

2011-04-27 16:09:48

SQL ServerSSIS

2010-01-04 13:06:50

ADO.NET事務

2014-02-11 09:07:31

2022-06-22 05:42:32

數據庫事務處理分析查詢

2022-06-13 10:42:21

分布式事務數據庫

2009-11-04 15:05:45

ADO.NET事務

2011-03-15 10:22:42

SQL Server 聯機事務處理

2017-04-10 13:30:47

Redis數據庫命令

2015-03-18 09:33:41

大數據分布式系統事務處理

2010-01-13 14:13:24

VB.NET事務處理

2009-07-15 15:57:28

AWT或Swing
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本一区二区不卡 | 久久久91精品国产一区二区三区 | 欧美国产视频 | 欧美aaaaaa| 国产美女精品视频 | 久久99精品视频 | 四虎网站在线观看 | 国产欧美日韩精品一区 | 国产91观看 | 亚洲一区二区三区免费视频 | 欧美综合久久 | 草草影院ccyy | 色欧美日韩 | 精品一区二区三区中文字幕 | 2019天天干夜夜操 | 久久成人综合 | 久久久久久久久99精品 | 欧美精品第一页 | 国产成人麻豆免费观看 | 久久久久亚洲精品国产 | 九九一级片| 久久久久久久av | 亚洲情侣视频 | 久久网一区二区 | 一区二区三区福利视频 | 欧美专区在线 | 国产jizz女人多喷水99 | 午夜在线电影网 | 91社区在线观看 | 在线视频91 | 日韩欧美精品在线 | 一区二区三区精品视频 | 日干夜干 | 成人午夜在线 | www.激情.com | av片网 | 黄网站免费在线 | 久草精品视频 | 国产精品视频不卡 | 国产美女视频黄 | 成人在线精品视频 |