<i id='CJBA4'><tr id='CJBA4'><dt id='CJBA4'><q id='CJBA4'><span id='CJBA4'><b id='CJBA4'><form id='CJBA4'><ins id='CJBA4'></ins><ul id='CJBA4'></ul><sub id='CJBA4'></sub></form><legend id='CJBA4'></legend><bdo id='CJBA4'><pre id='CJBA4'><center id='CJBA4'></center></pre></bdo></b><th id='CJBA4'></th></span></q></dt></tr></i><div id='CJBA4'><tfoot id='CJBA4'></tfoot><dl id='CJBA4'><fieldset id='CJBA4'></fieldset></dl></div>

        • <bdo id='CJBA4'></bdo><ul id='CJBA4'></ul>
      1. <legend id='CJBA4'><style id='CJBA4'><dir id='CJBA4'><q id='CJBA4'></q></dir></style></legend>

        <small id='CJBA4'></small><noframes id='CJBA4'>

      2. <tfoot id='CJBA4'></tfoot>

        MySQL中多列索引的字段顺序是否重要

        时间:2023-10-25

        <tfoot id='Rdh67'></tfoot>
        <legend id='Rdh67'><style id='Rdh67'><dir id='Rdh67'><q id='Rdh67'></q></dir></style></legend>

        <small id='Rdh67'></small><noframes id='Rdh67'>

            <tbody id='Rdh67'></tbody>
          • <bdo id='Rdh67'></bdo><ul id='Rdh67'></ul>
            • <i id='Rdh67'><tr id='Rdh67'><dt id='Rdh67'><q id='Rdh67'><span id='Rdh67'><b id='Rdh67'><form id='Rdh67'><ins id='Rdh67'></ins><ul id='Rdh67'></ul><sub id='Rdh67'></sub></form><legend id='Rdh67'></legend><bdo id='Rdh67'><pre id='Rdh67'><center id='Rdh67'></center></pre></bdo></b><th id='Rdh67'></th></span></q></dt></tr></i><div id='Rdh67'><tfoot id='Rdh67'></tfoot><dl id='Rdh67'><fieldset id='Rdh67'></fieldset></dl></div>

                1. 本文介绍了MySQL中多列索引的字段顺序是否重要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道索引的重要性以及连接顺序如何改变性能.我已经阅读了大量与多列索引相关的内容,但还没有找到我的问题的答案.

                  I know the importance of indexes and how order of joins can change performance. I've done a bunch of reading related to multi-column indexes and haven't found the answer to my question.

                  我很好奇我是否进行多列索引,如果指定它们的顺序很重要.我的猜测是它不会,并且引擎会将它们视为一个组,排序无关紧要.但我想验证一下.

                  I'm curious if I do a multi-column index, if the order that they are specified matters at all. My guess is that it would not, and that the engine would treat them as a group, where ordering doesn't matter. But I wish to verify.

                  例如,来自 mysql 的网站(http://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html)

                  For example, from mysql's website (http://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html)

                  CREATE TABLE test (
                      id         INT NOT NULL,
                      last_name  CHAR(30) NOT NULL,
                      first_name CHAR(30) NOT NULL,
                      PRIMARY KEY (id),
                      INDEX name (last_name,first_name)
                  );
                  

                  在任何情况下,以下会更好还是等效?

                  Would there be any benifit in any cases where the following would be better, or is it equivalent?

                  CREATE TABLE test (
                      id         INT NOT NULL,
                      last_name  CHAR(30) NOT NULL,
                      first_name CHAR(30) NOT NULL,
                      PRIMARY KEY (id),
                      INDEX name (first_name,last_name)
                  );
                  

                  特别是:

                  INDEX name (last_name,first_name)
                  

                  对比

                  INDEX name (first_name,last_name)
                  

                  推荐答案

                  在讨论多列索引时,我使用了电话簿的类比.电话簿基本上是姓氏索引,然后是名字索引.所以排序顺序是由哪个列"在前决定的.搜索分为几类:

                  When discussing multi-column indexes, I use an analogy to a telephone book. A telephone book is basically an index on last name, then first name. So the sort order is determined by which "column" is first. Searches fall into a few categories:

                  1. 如果您查找姓氏为 Smith 的人,您可以轻松找到他们,因为这本书是按姓氏排序的.

                  1. If you look up people whose last name is Smith, you can find them easily because the book is sorted by last name.

                  如果您查找名字为约翰的人,电话簿无济于事,因为约翰分散在整本书中.你必须扫描整个电话簿才能找到它们.

                  If you look up people whose first name is John, the telephone book doesn't help because the Johns are scattered throughout the book. You have to scan the whole telephone book to find them all.

                  如果您查找具有特定姓氏 Smith 和特定名字 John 的人,这本书会有所帮助,因为您会发现 Smiths 排序在一起,并且在该 Smiths 组中,Johns 也按排序顺序查找.

                  If you look up people with a specific last name Smith and a specific first name John, the book helps because you find the Smiths sorted together, and within that group of Smiths, the Johns are also found in sorted order.

                  如果您的电话簿按名字和姓氏排序,则电话簿的排序将在上述情况 #2 和 #3 中对您有所帮助,但在情况 #1 中没有帮助.

                  If you had a telephone book sorted by first name then by last name, the sorting of the book would assist you in the above cases #2 and #3, but not case #1.

                  这解释了查找精确值的情况,但如果您按值范围查找呢?假设您想查找所有名字为 John 且姓氏以S"开头的人(Smith、Saunders、Staunton、Sherman 等).约翰在每个姓氏中的J"下排序,但如果您希望所有姓氏的所有约翰都以S"开头,则约翰不会分组在一起.它们再次分散,因此您最终不得不扫描所有姓氏以S"开头的姓名.而如果电话簿按名字然后按姓氏组织,你会发现所有的约翰在一起,然后在约翰内,所有的S"姓会被分组在一起.

                  That explains cases for looking up exact values, but what if you're looking up by ranges of values? Say you wanted to find all people whose first name is John and whose last name begins with 'S' (Smith, Saunders, Staunton, Sherman, etc.). The Johns are sorted under 'J' within each last name, but if you want all Johns for all last names starting with 'S', the Johns are not grouped together. They're scattered again, so you end up having to scan through all the names with last name starting with 'S'. Whereas if the telephone book were organized by first name then by last name, you'd find all the Johns together, then within the Johns, all the 'S' last names would be grouped together.

                  所以多列索引中列的顺序肯定很重要.一种类型的查询可能需要索引的特定列顺序.如果您有多种类型的查询,您可能需要多个索引来帮助它们,并使用不同顺序的列.

                  So the order of columns in a multi-column index definitely matters. One type of query may need a certain column order for the index. If you have several types of queries, you might need several indexes to help them, with columns in different orders.

                  您可以阅读我的演示文稿如何设计索引,真的 了解更多信息.

                  You can read my presentation How to Design Indexes, Really for more information.

                  这篇关于MySQL中多列索引的字段顺序是否重要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySQL 无法创建外键约束 下一篇:MySQL 索引 NULL 值吗?

                  相关文章

                    <bdo id='MQV4b'></bdo><ul id='MQV4b'></ul>

                  1. <i id='MQV4b'><tr id='MQV4b'><dt id='MQV4b'><q id='MQV4b'><span id='MQV4b'><b id='MQV4b'><form id='MQV4b'><ins id='MQV4b'></ins><ul id='MQV4b'></ul><sub id='MQV4b'></sub></form><legend id='MQV4b'></legend><bdo id='MQV4b'><pre id='MQV4b'><center id='MQV4b'></center></pre></bdo></b><th id='MQV4b'></th></span></q></dt></tr></i><div id='MQV4b'><tfoot id='MQV4b'></tfoot><dl id='MQV4b'><fieldset id='MQV4b'></fieldset></dl></div>

                  2. <legend id='MQV4b'><style id='MQV4b'><dir id='MQV4b'><q id='MQV4b'></q></dir></style></legend><tfoot id='MQV4b'></tfoot>

                      <small id='MQV4b'></small><noframes id='MQV4b'>