1024programmer Blog Distinct multiple fields mysql_MySQL index optimization distinct statement and distinct multi-field operations_Kuiyan Blog

Distinct multiple fields mysql_MySQL index optimization distinct statement and distinct multi-field operations_Kuiyan Blog

MySQL usually uses GROUP BY (essentially a sorting action) to complete the DISTINCT operation. If the DISTINCT operation is combined with the ORDERBY operation, a temporary table is usually used. This will affect performance. In some cases, MySQL can use index optimization DISTINCT operation, but it needs to be learned and used flexibly. This article involves an instance where the DISTINCT operation cannot be completed using the index.

Example 1 Use index to optimize DISTINCT operation

create table m11 (a int, b int, c int, d int, primary key(a)) engine=INNODB;

insert into m11 values ​​(1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4),(5, 5,5,5),(6,6,6,6),(7,7,7,7),(8,8,8,8);

explain select distinct(a) from m11;

mysql> explain select distinct(a) from m11;

+—-+————-+——-+————+—— -+—————+———+———+——+—– -+———-+————-+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+—-+————-+——-+————+——- +—————+———+———+——+—— +———-+————-+| 1 |

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/distinct-multiple-fields-mysql_mysql-index-optimization-distinct-statement-and-distinct-multi-field-operations_kuiyan-blog/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索