Sql optimization tips

When the big table when you can design a redundant field, to avoid a large table query table even cause poor performance

For example, log tables and user tables, the log table is usually late to be quite large redundancy field can do a username and avoid the time to view the username and the user table associated

When optimization strategies when paging is too large

Examples of the graph is used as a non-indexed where to retrieve the column, the result is the use of full-text search mode

By adding the index column order by drawing on the type of query optimization to index

BTree use the Index Tuning inquiry found that type into a ref

BTree Index

Query classType in the figure below have normal index, the index btree way this time type is already fast the ref

When the query ID card identity card but it is not pure digital x, only with vachar, this time you can add a unique index, the index manner btree, this query type will become const speed equivalent block

The following figure is a unique index user_sys2 username, and indexing is Btree, so this time type becomes const

count (*) in MyIsam very fast, because MyIsam engines have saved a few lines, but needs its own calculation Innodb

count (*) counts all rows

Entry count (column) statistical data is not NULL

When the order by two columns need to be created to sort of joint index, below hire_date and ids all have separate index, but when the query will use the All, the overhead is spent creates a sorted index above, see Figure 2 below, It can be found creating sort index consumption accounted for 99.841%

This time if we hire_date, ids plus joint index, would greatly improve query speed, type the following figure at this time has become range

image-20191206093818430

Not just to create a joint index, the data often requires only a union query column only need to add joint index, because the index increase will affect the speed of insertion and modification, because the next index inndb leaf nodes are placed directly on the data, so the query block, ScanDisk to do, but when you insert containing copies of data and create an index, it will result in the insertion and modification slow

limit Optimization

Where there is an index on condition query username but if we do not limit using all

加上了limit之后就会变成range

limit优化用错排序字段遇到的坑,因为是联表,因为用到的是b表的查询条件,但是排序用的是a表的,所以虽然type是range但是extra有using temporary,using filesort此时我们是需要优化的

排序和查询都用一张表的索引字段,这样虽然type变成了index 但是extra只有一个where 速度就快了好多

分组查询倒排序速度很慢,因为排序过程中的s并不是索引字段,而是max(salary)

使用下面的方式已经优化了一些但是依然不够,可以用第三方来完成我们的sql优化,可以吧分组查询结果单独建立一张表,当有数据产生时需要去修改这张表





Guess you like

Origin www.cnblogs.com/hualou/p/12071099.html