An error occurred while parsing cardinality in MySQL.

Source: Internet
Author: User

An error occurred while parsing cardinality in MySQL.

Some time ago, early in the morning, I received an alarm warning that the number of php-fpm processes has exceeded the threshold. Eventually, an SQL statement is useless to the index, leading to slow execution of database queries, resulting in an increase in the number of php-fpm processes. Finally, the Cardinality is updated through the analyze table feed_comment_info_id_0000 command before the index can be used again.
The Troubleshooting process is as follows:
SQL statement:

select id from feed_comment_info_id_0000 where obj_id=101 and type=1;

Index information:

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)

When you look at the explain statement, it is found that the SQL statement uses the PRIMARY key PRIMARY instead of the obj_type index. View the Cardinality value of the index through show index, and find that this value is twice the actual data. The Cardinality value is not normal. Therefore, the Cardinality value is calculated again using the analyzea table command. After the command is executed, indexes can be used.

Cardinality
Explanation of the official document:
An estimate of the number of unique values in the index. this is updated by running analyze table or myisamchk-. 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
Summary:
1. It represents the estimated number of unique values in the index. For the myisam engine, this value is an accurate value. For the innodb engine, this value is an estimated value. Each time you execute show index, it may be different.
2. When creating an Index (except for the primary key), the Cardinality value of the MyISAM table is null, And the Cardinality value of the InnoDB table is about the number of rows;
3. The value size affects the selection of indexes.
4. When creating an Index, the Cardinality value of the MyISAM table is null, And the Cardinality value of the InnoDB table is roughly the number of rows.
5. You can use Analyze table to update a table or mysqlcheck-Aa to update the entire database.
6. You can view the value through show index.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.