MYSQL index optimization

When a table has a large amount of data, we need to optimize the table. In addition to sub-table and sub-library, the most common is index optimization. What is the principle of index optimization?

When sorting and grouping are not considered, that is, when there is only where in the SQL statement, multi-column and query such as

select * from payment where staff_id=? and customer_id=?

According to the index principle, who has the most number, who is the leftmost index, and the position of the leftmost index in MySQL's B+ tree structure is very important.

select count(distinct staff_id)/count(*) staff_id_selectivity,count(disctinct customer_id)/count(*) customer_id_selectivity,count(*) from payment\G

The result of join operation is staff_id_selectivity: 0.0001

                  customer_id_selectivity:0.0373

                                        count(*):16049

It is obvious that customer_id accounts for a large proportion, and the result is

alter table payment add key(customer_id,staff_id)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344136&siteId=291194637