How to improve the query speed of mysql

 In the actual project I participated in, it was found that when the amount of data in the mysql table reached millions, the efficiency of ordinary SQL queries plummeted, and if there were many query conditions in where, the query speed was simply unbearable. Therefore, how to improve the query efficiency of sql statement is very important. The following are the cumulative and more widely used 22 kinds of SQL query statement optimization methods: 
1. Try to avoid using the != or <> operator in the where clause, otherwise the engine will give up using the index and perform a full table scan. 
 
2. To optimize the query, full table scan should be avoided as much as possible. First, index should be considered on the columns involved in where and order by. 
 
3. Try to avoid the null value judgment of the field in the where clause, otherwise the engine will give up the use of the index and perform a full table scan, such as: 
     select id from t where num is null 
     You can set a default value of 0 on num, make sure there are no null values ​​in the num column in the table, and then query like this: 
     select id from t where num=0 
 
4. Try to avoid using or to join conditions in the where clause, otherwise it will cause the engine to give up using the index and perform a full table scan, such as: 
     select id from t where num=10 or num=20 
     You can query like this: 
     select id from t where num=10 
     union all 
     select id from t where num=20 
 
5. The following query will also result in a full table scan: (As the saying goes, after the wildcard, go to the index; before the wildcard, take the whole table. So you can't prepend the percent sign) 
     select id from t where name like ‘%c%’ 
    To improve efficiency, consider full-text search. 
 
6. Not in should be used with caution, otherwise it will result in a full table scan. PHP code, arrays, parameters, etc. have no mandatory requirements for the type of variables. Therefore, both strings and integers are included in the in condition, which will also cause the index to fail. 
 
7. Try to avoid expression operations on fields in the where clause, which will cause the engine to give up the use of indexes and perform full table scans. Such as: 
     select id from t where num/2=100 
     Should be changed to: 
     select id from t where num=100*2 
 
8. The function operation on the field in the where clause should be avoided as much as possible, which will cause the engine to give up the use of the index and perform a full table scan. Such as: 
     select id from t where substring(name,1,3)='abc' – id whose name starts with abc 
     select id from t where datediff(day,createdate,’2005-11-30′)=0–’2005-11-30′生成的id 
     Should be changed to: 
     select id from t where name like ‘abc%’ 
     select id from t where createdate>=’2005-11-30′ and createdate<’2005-12-1′ 
 
9. Do not perform functions, arithmetic operations or other expression operations on the left side of "=" in the where clause, otherwise the system may not be able to use the index correctly. 
 
10. When using an index field as a condition, if the index is a composite index, the first field in the index must be used as a condition to ensure that the system uses the index, otherwise the index will not be used and should be used. As much as possible, make the field order consistent with the index order. 
 
11. In many cases, it is a good choice to use exists instead of in: 
     select num from a where num in(select num from b) 
     用下面的语句替换: 
     select num from a where exists(select 1 from b where num=a.num) 
 
12、并不是所有索引对查询都有效,SQL是根据表中数据来进行查询优化的,当索引列有大量数据重复时,SQL查询可能不会去利用索引,如一表中有字段 sex,male、female几乎各一半,那么即使在sex上建了索引也对查询效率起不了作用。 
 
13、索引并不是越多越好,索引固然可以提高相应的 select 的效率,但同时也降低了 insert 及 update 的效率,因为 insert 或 update 时有可能会重建索引,所以怎样建索引需要慎重考虑,视具体情况而定。一个表的索引数最好不要超过6个,若太多则应考虑一些不常使用到的列上建的索引是否有 必要。 
 
14、尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能,并会增加存储开销。这是因为引擎在处理查询和连接时会 逐个比较字符串中每一个字符,而对于数字型而言只需要比较一次就够了。 
 
15、尽可能的使用 varchar/nvarchar 代替 char/nchar ,因为首先变长字段存储空间小,可以节省存储空间,其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些。 
 
16、任何地方都不要使用 select * from t ,用具体的字段列表代替“*”,不要返回用不到的任何字段。 
 
17、避免频繁创建和删除临时表,以减少系统表资源的消耗。 
 
18、尽量避免大事务操作,提高系统并发能力。
 
19、使用短索引
对串列进行索引,如果可能应该指定一个前缀长度。例如,如果有一个CHAR(255)的列,如果在前10个或20个字符内,多数值是惟一的,那么就不要对整个列进行索引。短索引不仅可以提高查询速度而且可以节省磁盘空间和I/O操作。
 
20、索引列排序
MySQL查询只使用一个索引,因此如果where子句中已经使用了索引的话,那么order by中的列是不会使用索引的。因此数据库默认排序可以符合要求的情况下不要使用排序操作;尽量不要包含多个列的排序,如果需要最好给这些列创建复合索引。
 
21、检查有无子查询,子查询会每次都扫描子查询的整表,尽量少使用子查询
 
22、检查关联每个表的每个条件都有无索引

Guess you like

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