国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

簡單解析MySQL中的cardinality異常

2024-07-24 12:46:12
字體:
來源:轉載
供稿:網(wǎng)友

前段時間,一大早上,就收到報警,警告php-fpm進程的數(shù)量超過閾值。最終發(fā)現(xiàn)是一條sql沒用到索引,導致執(zhí)行數(shù)據(jù)庫查詢慢了,最終導致php-fpm進程數(shù)增加。最終通過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查看時,發(fā)現(xiàn)sql用的是主鍵PRIMARY,而不是obj_type索引。通過show index 查看索引的Cardinality值,發(fā)現(xiàn)這個值是實際數(shù)據(jù)的兩倍。感覺這個Cardinality值已經(jīng)不正常,因此通過analyzea table命令對這個值從新進行了計算。命令執(zhí)行完畢后,就可用使用索引了。

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、它代表的是索引中唯一值的數(shù)目的估計值。如果是myisam引擎,這個值是一個準確的值。如果是innodb引擎,這個值是一個估算的值,每次執(zhí)行show index 時,可能會不一樣
2、創(chuàng)建Index時(primary key除外),MyISAM的表Cardinality的值為null,InnoDB的表Cardinality的值大概為行數(shù);
3、值的大小會影響到索引的選擇
4、創(chuàng)建Index時,MyISAM的表Cardinality的值為null,InnoDB的表Cardinality的值大概為行數(shù)。
5、可以通過Analyze table來更新一張表或者mysqlcheck -Aa來進行更新整個數(shù)據(jù)庫
6、可以通過 show index 查看其值

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 蒙自县| 林芝县| 镇江市| 枞阳县| 阿瓦提县| 怀远县| 双牌县| 如皋市| 紫阳县| 新津县| 石棉县| 武隆县| 芜湖市| 葵青区| 宁安市| 邮箱| 平凉市| 水富县| 巴东县| 保德县| 秦安县| 中牟县| 彰化县| 涪陵区| 秦皇岛市| 临武县| 克拉玛依市| 淄博市| 定襄县| 富川| 徐闻县| 自贡市| 孝感市| 绩溪县| 彰化市| 电白县| 芒康县| 长宁区| 百色市| 舟山市| 枝江市|