当前位置:网站首页>MySQL indexing and performance optimization

MySQL indexing and performance optimization

2022-06-09 04:34:00 User 9076598

Principles of using indexes 1. If there is no uniqueness requirement , You can select a normal index 2. If there is a uniqueness requirement on the column , You can select a unique index 3. If you need fuzzy search , It is recommended to select full-text index 4. If there are multiple conditions to query together , You can choose to combine indexes The following points should be paid attention to when using indexes : 1. Use indexes as needed 2. The larger the cardinality of the column where the index is located, the better , The effect of indexing such fields for men and women is not significant , The base number is very small 3. Pay attention to the leftmost principle in the composite index

We want to know our sql Is the sentence well written , How to judge ? Let's start with sql How statements are executed , for instance select u.name i.expression from user u left join userinfo i on u.id=i.uid where u.id in (1,3,4,55,67,76) order by u.id limit 10; This article sql sentence , Which block will be executed first ? What is the principle of execution ?

select u.name i.expression from user u left join userinfo i on u.id=i.uid where u.id in (1,3,4,55,67,76) order by u.id limit 10; sql The logic of statement execution is as follows First step : take user Table and userinfo surface Do Cartesian product 1.FROM Clause on the left table after it user And right table execution userinfo Line Cartesian product , Generate virtual tables VT1 2.ON Clause pair VT1 The data in is based on ON Conditions for filtration , Generate virtual tables VT2 problem : How to filter ? 3.JOIN Clause Add the data in the unqualified retention table to all VT2 in , formation VT3 4.WHERE Clause Yes VT3 The data in WHERE filter , formation VT4 5.GROUP BY Clause pair VT4 Group the data in , And then form VT5 6.CUBE | ROLLUP Clause to form VT6

7.HAVING Yes VT6 The data in HAVING filter , And then form VT7 8.SELECT from VT7 Select the field to get in the , And then form VT8 9.DISTINCT De duplication data , formation VT9 10.ORDER BY Yes VT9 After sorting the results of , formation VT10 11.LIMIT from VT10 To retrieve the specified data , formation VT11, Return to the user

We want to know our sql Is the sentence well written , How to judge ? Method 1 : Directly in database Up test , Look at the execution time Method 2 : explain select xxxx see

There are several parameters that need attention : type It's worth more than

const: The table has at most one matching row ,const For comparison primary key perhaps unique Indexes . eq_ref: It is used for joining all parts of an index and the index is UNIQUE or PRIMARY KEY".

eq_ref It can be used for = Compare indexed columns . ref For each row combination from the previous table , All rows with matching index values are read from this table . range Search in a given range , Use an index to check the rows . ref Column shows which column or constant to use with key Select rows from the table together . rows Show MYSQL Number of rows to execute the query , Simple and important , The larger the number, the worse , It means that the index is not used well

原网站

版权声明
本文为[User 9076598]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021700496400.html