MySQL optimization practice that you don't know is not allowed (3)



insert image description here

Don't say anything extra, just serve the dishes!


21. Try to replace union with union all

If there will be no duplicate records in the retrieval results, it is recommended to replace union with union all.

Counter example:

select * from user where userid=1 union  select * from user where age = 10

Positive example:

select * from user where userid=1 union all select * from user where age = 10

reason:

  • If union is used, no matter whether the search results are duplicated or not, they will try to merge and then sort before outputting the final result. If it is known that there are no duplicate records in the search results, use union all instead of union, which will improve efficiency.

22. There should not be too many indexes, generally within 5.

  • The more indexes, the better. Although indexes improve the efficiency of queries, they also reduce the efficiency of inserting and updating.
  • The index may be rebuilt during insert or update, so the index construction needs to be carefully considered, depending on the specific situation.
  • It is best not to have more than 5 indexes in a table. If there are too many, you need to consider whether some indexes are unnecessary.

23. Try to use numeric fields as much as possible. If the fields only contain numerical information, try not to design them as character types.

Counter example:

`king_id` varchar20NOT NULL COMMENT '守护者Id'

Positive example:

 `king_id` int(11) NOT NULL COMMENT '守护者Id'

reason:

  • Compared with numeric fields, character types will reduce the performance of queries and connections, and will increase storage overhead.

24. Indexes are not suitable to be built on fields with a large amount of repeated data, such as gender database fields.

Because the SQL optimizer performs query optimization based on the amount of data in the table, if there is a large amount of duplicate data in the index column, the Mysql query optimizer calculates that the cost of not using the index is lower, and it is likely to give up the index.


25. Try to avoid returning too much data to the client.

Suppose the business requirement is that the user requests to view the live broadcast data he has watched in the last year.

Counter example:

//一次性查询所有数据回来select * from LivingInfo where watchId =useId and watchTime >= Date_sub(now(),Interval 1 Y)

Positive example:

//分页查询select * from LivingInfo where watchId =useId and watchTime>= Date_sub(now(),Interval 1 Y) limit offset,pageSize
//如果是前端分页,可以先查询前两百条记录,因为一般用户应该也不会往下翻太多页,select * from LivingInfo where watchId =useId and watchTime>= Date_sub(now(),Interval 1 Y) limit 200 ;

26. When connecting multiple tables in an SQL statement, please use the alias of the table and prefix each column with the alias, so that the semantics are clearer.

Counter example:

select  * from A innerjoin B on A.deptId = B.deptId;

Positive example:

select  memeber.name,deptment.deptName from A member innerjoin B deptment on member.deptId = deptment.deptId;

27. Use varchar/nvarchar instead of char/nchar as much as possible.

Counter example:

  `deptName` char(100) DEFAULT NULL COMMENT '部门名称'

Positive example:

  `deptName` varchar(100) DEFAULT NULL COMMENT '部门名称'

reason:

  • Because the storage space of the variable-length field is small, the storage space can be saved.
  • Secondly, for queries, searching in a relatively small field is more efficient.

28. In order to improve the efficiency of the group by statement, you can filter out unnecessary records before executing the statement.

Counter example:

select job,avg(salary) from employee  group by job having job ='president' or job = 'managent'

Positive example:

select job,avg(salary) from employee where job ='president' or job = 'managent' group by job;

29. If the field type is a string, the where must be enclosed in quotation marks, otherwise the index will fail

Counter example:

select * from user where userid =123;

insert image description here

Positive example:

select * from user where userid ='123';

insert image description here

reason

  • Why is the first statement not indexed without single quotes? This is because when the single quotes are not added, it is a comparison between a string and a number, and their types do not match. MySQL will do an implicit type conversion and convert them to floating point numbers for comparison.

30. Use explain to analyze your SQL plan

When developing and writing SQL every day, try to develop a habit as much as possible. Use explain to analyze the SQL you wrote, especially whether to use the index or not.

explain select * from user where userid =10086 or age =18;

insert image description here



insert image description here

Guess you like

Origin blog.csdn.net/m0_60915009/article/details/131718910