MySQL query optimization experience

       Recently, when dealing with a large amount of data of more than 10 million levels, MySQL has the disadvantage of taking a long time. Through collecting data, I have summarized the experience of optimizing SQL found as follows, I hope it will be helpful to you~


1. To optimize the query, full table scan should be avoided as much as possible. First, you should consider building indexes on the columns involved in where and order by.

2. 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 the default value of 0 on num to ensure that There is no null value in the num column in the table, and then query like this: select id from t where num=0

3. 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 .

4. Try to avoid using or to connect conditions 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=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. In and not in should also be used with caution, otherwise it will result in a full table scan, such as: select id from t where num in(1,2,3) For For consecutive values, you can use between instead of in: select id from t where num between 1 and 3

6. The following query will also result in a full table scan: select id from t where name like '%three'. To improve Efficiency, full-text search can be considered. But the following query uses an index: select id from t where name like 'zhang%'. 

7. If a parameter is used in the where clause, it will also cause a full table scan. Because SQL resolves local variables only at runtime, the optimizer cannot defer the choice of an access plan to runtime; it must choose it at compile time. However, if the access plan is built at compile time, the value of the variable is unknown and cannot be used as an input for index selection. For example, the following statement will perform a full table scan: select id from t where num=@num can be changed to force the query to use the index: select id from t with(index(index name)) where num=@num

8. Try to avoid where The expression operation on the field in the clause will cause the engine to give up the use of the index and perform a full table scan. For example: select id from t where num/2=100 should be changed to: select id from t where num=100*2

9. You should try to avoid performing functional operations on fields in the where clause, which will cause the engine to abandon the use of indexes instead of Do a full table scan. For example: select id from t where substring(name,1,3)='abc', the id whose name starts with abc should be changed to: select id from t where name like 'abc%'

10. Don't use it in the where clause Functions, arithmetic operations, or other expression operations are performed on the left side of the "=", otherwise the system may not use the index correctly.

11. 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.

12. Don't write some meaningless queries. If you need to generate an empty table structure: select col1,col2 into #t from t where 1=0 This kind of code will not return any result set, but it will consume system resources and should be changed Like this: create table #t(...)

13. Many times it is a good choice to replace in with exists: select num from a where num in(select num from b) replace with the following statement: select num from a where exists(select 1 from b where num=a.num )

14. Not all indexes are valid for queries. SQL optimizes the query based on the data in the table. When a large amount of data in the index column is repeated, the SQL query may not use the index. For example, there are fields sex, male in a table. , female are almost half and half, so even if an index is built on sex, it will not affect the query efficiency.

15. The more indexes the better, the index can certainly improve the efficiency of the corresponding select, but it also reduces the efficiency of insert and update, because the index may be rebuilt during insert or update, so how to build an index needs to be carefully considered. As the case may be. The number of indexes in a table should not exceed 6. If there are too many indexes, you should consider whether it is necessary to build indexes on some infrequently used columns.


16.应尽可能的避免更新 clustered 索引数据列,因为 clustered 索引数据列的顺序就是表记录的物理存储顺序,一旦该列值改变将导致整个表记录的顺序的调整,会耗费相当大的资源。若应用系统需要频繁更新 clustered 索引数据列,那么需要考虑是否应将该索引建为 clustered 索引。

17.尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能,并会增加存储开销。这是因为引擎在处理查询和连接时会逐个比较字符串中每一个字符,而对于数字型而言只需要比较一次就够了。

18.尽可能的使用 varchar/nvarchar 代替 char/nchar ,因为首先变长字段存储空间小,可以节省存储空间,其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些。

19.任何地方都不要使用 select * from t ,用具体的字段列表代替“*”,不要返回用不到的任何字段。

20.尽量使用表变量来代替临时表。如果表变量包含大量数据,请注意索引非常有限(只有主键索引)。

21.避免频繁创建和删除临时表,以减少系统表资源的消耗。

22.临时表并不是不可使用,适当地使用它们可以使某些例程更有效,例如,当需要重复引用大型表或常用表中的某个数据集时。但是,对于一次性事件,最好使用导出表。

23.在新建临时表时,如果一次性插入数据量很大,那么可以使用 select into 代替 create table,避免造成大量 log ,以提高速度;如果数据量不大,为了缓和系统表的资源,应先create table,然后insert。

24.如果使用到了临时表,在存储过程的最后务必将所有的临时表显式删除,先 truncate table ,然后 drop table ,这样可以避免系统表的较长时间锁定。

25.尽量避免使用游标,因为游标的效率较差,如果游标操作的数据超过1万行,那么就应该考虑改写。

26.使用基于游标的方法或临时表方法之前,应先寻找基于集的解决方案来解决问题,基于集的方法通常更有效。

27.与临时表一样,游标并不是不可使用。对小型数据集使用 FAST_FORWARD 游标通常要优于其他逐行处理方法,尤其是在必须引用几个表才能获得所需的数据时。在结果集中包括“合计”的例程通常要比使用游标执行的速度快。如果开发时 间允许,基于游标的方法和基于集的方法都可以尝试一下,看哪一种方法的效果更好。

28.在所有的存储过程和触发器的开始处设置 SET NOCOUNT ON ,在结束时设置 SET NOCOUNT OFF 。无需在执行存储过程和触发器的每个语句后向客户端发送DONE_IN_PROC 消息。

29.尽量避免大事务操作,提高系统并发能力。

30.尽量避免向客户端返回大数据量,若数据量过大,应该考虑相应需求是否合理。

Guess you like

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