MySQL Advanced (5) Sort Index Optimization Always Small Table Drives Big Table Order by Keyword Optimization for Group by Keyword

MySQL advanced (5) sort index optimization

5 Sort index optimization

5.1 Forever small table drives big table

Optimization principle : small tables drive large tables, that is, small data sets drive large data sets

Case :

SELECT * FROM A WHERE id in (select id from B)
--等价于
for select id from B
for select * from A where A.id = B.id
--for 表示循环

When the data set is less than the Table B Table A dataset, with superior exist in

select * from A where exists( select 1 from B where A.id = B.id)
--等价于
for select * from A
for select * from B where A.id = B.id
--exists:将主查询的数据,放入到子查询中做条件验证,根据验证结果(TRUE或FALSE),来决定主查询的数据是否保留

When the data set of Table A Table B is less than the data set, with superior exist in

Conclusion : When the subquery data set is smaller than the main query, use in; when the main query data set is smaller than the subquery, use exists

5.2 Order by keyword optimization

The key point is: avoid file sort

Optimization principle :

  • Order by clause try to use index index to sort, avoid using filesort to sort.
  • As far as possible to complete the sorting operation on the index column, follow the best left prefix when the index is created
  • Try not to use select * when order by, it will take up a lot of buffer size
  • If you must use file sort sorting, you can modify the MySQL parameters to choose two-way sorting or single-way sorting
    • Two-way sorting : scan the disk twice , sort them by reading the row pointer and order by column, and then obtain the data according to the sorted list.
    • Single-way sorting : scan the disk once , read out all the columns that need to be queried, and then sort them in the buffer . Overall efficiency is better than two-way sorting
    • The problem arising from single-way sorting: single-way sorting takes up more sort_buffer (in memory) than two-way sorting . If the total data size fetched is larger than the buffer size configured by the server, it will lead to multiple fetching of part of the data and sorting again. More times of IO. (It needs to be solved by modifying the MYSQL server parameters, increasing the size of sort_buffer_size and max_size_for_sort_data)

Test table structure :

CREATE TABLE tblA(
	age int4,
	birth TIMESTAMP not null
);

insert into tblA(age,birth) VALUES(22,NOW());
insert into tblA(age,birth) VALUES(23,NOW());
insert into tblA(age,birth) VALUES(24,NOW());

create index idx_tblA_AgeBirth on tblA(age,birth);
select * from tblA

Test case :

Insert picture description here

Insert picture description here

Conclusion :

  • MySQL supports two sorting methods, file sort and index. Index means that MySQL scans the index itself to complete the sorting, which is more efficient;
  • Order by To use index sorting, two situations need to be met: Order by statement uses the principle of the leftmost front of the index ; there is no situation where ASC and DESC are used simultaneously

Insert picture description here

5.3 Optimization of Group by keywords

  • The essence of group by is to sort first and then group, following the best left prefix built by the index
  • Consistent with order by, when the index column cannot be used, increase the size of sort_buffer_size and max_size_for_sort_data
  • Where is higher than having, the qualifications that can be written in where should not be written in having

Guess you like

Origin blog.csdn.net/weixin_44634197/article/details/108902745