簡略解析MySQL中的cardinality異常。本站提示廣大學習愛好者:(簡略解析MySQL中的cardinality異常)文章只能為提供參考,不一定能成為您想要的結果。以下是簡略解析MySQL中的cardinality異常正文
前段時光,一年夜早上,就收到報警,正告php-fpm過程的數目跨越阈值。終究發明是一條sql沒用到索引,招致履行數據庫查詢慢了,終究招致php-fpm過程數增長。終究經由過程analyze table feed_comment_info_id_0000 敕令更新了Cardinality ,能力再次用到索引。
排查進程以下:
sql語句:
select id from feed_comment_info_id_0000 where obj_id=101 and type=1;
索引信息:
show index from feed_comment_info_id_0000 +---------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +---------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | feed_comment_info_id_0000 | 0 | PRIMARY | 1 | id | A | 6216 | NULL | NULL | | BTREE | | | feed_comment_info_id_0000 | 1 | obj_type | 1 | obj_id | A | 6216 | NULL | NULL | | BTREE | | | feed_comment_info_id_0000 | 1 | obj_type | 2 | type | A | 6216 | NULL | NULL | YES | BTREE | | | feed_comment_info_id_0000 | 1 | user_id | 1 | user_id | A | 6216 | NULL | NULL | | BTREE | | +---------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 5 rows in set (0.00 sec)
經由過程explian檢查時,發明sql用的是主鍵PRIMARY,而不是obj_type索引。經由過程show index 檢查索引的Cardinality值,發明這個值是現實數據的兩倍。感到這個Cardinality值曾經不正常,是以經由過程analyzea table敕令對這個值重新停止了盤算。敕令履行終了後,便可用應用索引了。
Cardinality說明
官方文檔的說明:
An estimate of the number of unique values in the index. This is updated by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on statistics stored as integers, so the value is not necessarily exact even for small tables. The higher the cardinality, the greater the chance that MySQL uses the index when doing
總結一下:
1、它代表的是索引中獨一值的數量的估量值。假如是myisam引擎,這個值是一個精確的值。假如是innodb引擎,這個值是一個預算的值,每次履行show index 時,能夠會紛歧樣
2、創立Index時(primary key除外),MyISAM的表Cardinality的值為null,InnoDB的表Cardinality的值年夜概為行數;
3、值的年夜小會影響到索引的選擇
4、創立Index時,MyISAM的表Cardinality的值為null,InnoDB的表Cardinality的值年夜概為行數。
5、可以經由過程Analyze table來更新一張表或許mysqlcheck -Aa來停止更新全部數據庫
6、可以經由過程 show index 檢查其值