In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "mysql's analysis of primary key index". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Will InnoDB add a primary key at the end of the index and when? CREATE TABLE t (a char (32) not null primary key, b char (32) not null, KEY idx1 (a) b), KEY idx2 (b)) Engine=InnoDB
After inserting some of the data, you can see that the idx1 and idx2 indexes are the same size. This shows that the internal structure of idx1 and idx2 is the same, so it is impossible for idx1 to be stored internally.
As long as the user-defined index field contains the primary key field, the field will no longer be automatically added to the index by InnoDB. If the user's index field does not fully contain the primary key field, InnoDB will add the remaining primary key fields to the end of the index.
So in our initial example, the internal sizes of the idx1 and idx2 indexes are exactly the same and there is no difference.
Finally, add the following example of combining primary keys:
CREATE TABLE t (a char (32) not null, b char (32) not null, c char (32) not null, d char (32) not null, PRIMARY KEY (a) KEY idx1 (c), KEY idx2 (d)) Engine=InnoDB
This table InnoDB automatically completes the primary key dictionary, idx1 is actually internally stored as (cmemaphe b), and idx2 is actually internally stored as (dmaine bpena).
But the Server layer doesn't know about this automatically added field, so the MySQL optimizer doesn't know that this field exists, so if you have a query:
SELECT * FROM t WHERE d=x1 AND b=x2 ORDER BY a
In fact, the idx2 stored internally can make this query completely indexed, but because the Server layer doesn't know about it, the MySQL optimizer may choose idx2 to filter and sort a fields, or directly use competitive scanning to avoid sorting.
If we define the table structure as KEY idx2, then MySQL knows that there are all three field indexes, and InnoDB finds that the user-defined index contains all the primary key fields and does not add any more storage space.
Therefore, it is sincerely recommended that all DBA indexes should be appended with the primary key field after the index field required by the business, which does not have any loss, but may bring you a pleasant surprise.
This is the end of mysql's Analysis of Primary key Index. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.