Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of MySQL single-column index and federated index

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "the use of MySQL single-column index and federated index". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the use of MySQL single-column index and federated index.

This paper introduces the selection of single-column index and federated index by optimizer through a case.

There are two indexes on the ord_seq field of the order table, the single column index (order_seq) and the federated index (order_seq,order_type)

MySQL > explain select * from `order` where order_seq = 15021312577 +- -+-+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +- -+ | 1 | SIMPLE | order | NULL | ref | idx_ordseq Idx_ordseq_ordtype | idx_ordseq | 8 | const | 1 | 100.00 | NULL | +-+-- -+

You can see that the optimizer chose the single-column index idx_ordseq (order_seq) instead of the federated index. Because the leaf node of the index contains a single key value, theoretically a page can hold more records.

If you change the scene:

MySQL > explain select * from `order` where order_seq = 1502131212577 order by order_type desc +- -+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +- -+-+ | 1 | SIMPLE | order | NULL | ref | idx_ordseq Idx_ordseq_ordtype | idx_ordseq_ordtype | 8 | const | 1 | 100.00 | Using where | + -+-+ 1 row in set 1 warning (0.00 sec)

The joint index of where condition field and sort field is added to solve the problem of filesort.

At this point, I believe you have a deeper understanding of "the use of MySQL single-column index and federated index". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report