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

        <small id='4M3sw'></small><noframes id='4M3sw'>

      1. <tfoot id='4M3sw'></tfoot>

        <legend id='4M3sw'><style id='4M3sw'><dir id='4M3sw'><q id='4M3sw'></q></dir></style></legend>

        涉及范围时,索引中首先出现更高基数的列?

        时间:2023-06-02
      2. <i id='R6nRQ'><tr id='R6nRQ'><dt id='R6nRQ'><q id='R6nRQ'><span id='R6nRQ'><b id='R6nRQ'><form id='R6nRQ'><ins id='R6nRQ'></ins><ul id='R6nRQ'></ul><sub id='R6nRQ'></sub></form><legend id='R6nRQ'></legend><bdo id='R6nRQ'><pre id='R6nRQ'><center id='R6nRQ'></center></pre></bdo></b><th id='R6nRQ'></th></span></q></dt></tr></i><div id='R6nRQ'><tfoot id='R6nRQ'></tfoot><dl id='R6nRQ'><fieldset id='R6nRQ'></fieldset></dl></div>
          <bdo id='R6nRQ'></bdo><ul id='R6nRQ'></ul>

          <tfoot id='R6nRQ'></tfoot>
            <tbody id='R6nRQ'></tbody>

              1. <legend id='R6nRQ'><style id='R6nRQ'><dir id='R6nRQ'><q id='R6nRQ'></q></dir></style></legend>

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

                • 本文介绍了涉及范围时,索引中首先出现更高基数的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  CREATE TABLE `files` (
                    `did` int(10) unsigned NOT NULL DEFAULT '0',
                    `filename` varbinary(200) NOT NULL,
                    `ext` varbinary(5) DEFAULT NULL,
                    `fsize` double DEFAULT NULL,
                    `filetime` datetime DEFAULT NULL,
                    PRIMARY KEY (`did`,`filename`),
                    KEY `fe` (`filetime`,`ext`),          -- This?
                    KEY `ef` (`ext`,`filetime`)           -- or This?
                  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
                  

                  表中有一百万行.文件时间大多不同.ext 值的数量是有限的.因此,filetime 的基数很高,而 ext 的基数要低得多.

                  There are a million rows in the table. The filetimes are mostly distinct. There are a finite number of ext values. So, filetimehas a high cardinality and ext has a much lower cardinality.

                  查询同时涉及extfiletime:

                  WHERE ext = '...'
                    AND filetime BETWEEN ... AND ...
                  

                  这两个指标哪个更好?为什么?

                  Which of those two indexes is better? And why?

                  推荐答案

                  首先,让我们试试 FORCE INDEX 选择 effe.时间太短,无法清楚地了解哪个更快,但`EXPLAIN 显示了一个差异:

                  First, let's try FORCE INDEX to pick either ef or fe. The timings are too short to get a clear picture of which is faster, but `EXPLAIN shows a difference:

                  首先在 filetime 上强制范围.(注意:WHERE 中的顺序没有影响.)

                  Forcing the range on filetime first. (Note: The order in WHERE has no impact.)

                  mysql> EXPLAIN SELECT COUNT(*), AVG(fsize)
                      FROM files FORCE INDEX(fe)
                      WHERE ext = 'gif' AND filetime >= '2015-01-01'
                                        AND filetime <  '2015-01-01' + INTERVAL 1 MONTH;
                  +----+-------------+-------+-------+---------------+------+---------+------+-------+-----------------------+
                  | id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows  | Extra                 |
                  +----+-------------+-------+-------+---------------+------+---------+------+-------+-----------------------+
                  |  1 | SIMPLE      | files | range | fe            | fe   | 14      | NULL | 16684 | Using index condition |
                  +----+-------------+-------+-------+---------------+------+---------+------+-------+-----------------------+
                  

                  强制低基数ext优先:

                  mysql> EXPLAIN SELECT COUNT(*), AVG(fsize)
                      FROM files FORCE INDEX(ef)
                      WHERE ext = 'gif' AND filetime >= '2015-01-01'
                                        AND filetime <  '2015-01-01' + INTERVAL 1 MONTH;
                  +----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
                  | id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                 |
                  +----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
                  |  1 | SIMPLE      | files | range | ef            | ef   | 14      | NULL |  538 | Using index condition |
                  +----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
                  

                  显然,rows 表示 ef 更好.但是让我们检查优化器跟踪.输出相当庞大;我只会展示有趣的部分.不需要FORCE;跟踪将显示两个选项,然后选择更好的.

                  Clearly, the rows says ef is better. But let's check with the Optimizer trace. The output is rather bulky; I'll show only the interesting parts. No FORCE is needed; the trace will show both options then pick the better.

                               ...
                               "potential_range_indices": [
                                  ...
                                  {
                                    "index": "fe",
                                    "usable": true,
                                    "key_parts": [
                                      "filetime",
                                      "ext",
                                      "did",
                                      "filename"
                                    ]
                                  },
                                  {
                                    "index": "ef",
                                    "usable": true,
                                    "key_parts": [
                                      "ext",
                                      "filetime",
                                      "did",
                                      "filename"
                                    ]
                                  }
                                ],
                  

                  ...

                                "analyzing_range_alternatives": {
                                  "range_scan_alternatives": [
                                    {
                                      "index": "fe",
                                      "ranges": [
                                        "2015-01-01 00:00:00 <= filetime < 2015-02-01 00:00:00"
                                      ],
                                      "index_dives_for_eq_ranges": true,
                                      "rowid_ordered": false,
                                      "using_mrr": false,
                                      "index_only": false,
                                      "rows": 16684,
                                      "cost": 20022,               <-- Here's the critical number
                                      "chosen": true
                                    },
                                    {
                                      "index": "ef",
                                      "ranges": [
                                        "gif <= ext <= gif AND 2015-01-01 00:00:00 <= filetime < 2015-02-01 00:00:00"
                                      ],
                                      "index_dives_for_eq_ranges": true,
                                      "rowid_ordered": false,
                                      "using_mrr": false,
                                      "index_only": false,
                                      "rows": 538,
                                      "cost": 646.61,               <-- Here's the critical number
                                      "chosen": true
                                    }
                                  ],
                  

                  ...

                            "attached_conditions_computation": [
                              {
                                "access_type_changed": {
                                  "table": "`files`",
                                  "index": "ef",
                                  "old_type": "ref",
                                  "new_type": "range",
                                  "cause": "uses_more_keyparts"   <-- Also interesting
                                }
                              }
                  

                  使用 fe(范围列第一),可以使用范围,但它估计扫描 16684 行以获取 ext='gif'.

                  With fe (range column first), the range could be used, but it estimated scanning through 16684 rows fishing for ext='gif'.

                  使用ef(低基数ext优先),它可以使用索引的两列并在BTree中更有效地向下钻取.然后它发现了大约 538 行,所有这些行都对查询有用——不需要进一步过滤.

                  With ef (low cardinality ext first), it could use both columns of the index and drill down more efficiently in the BTree. Then it found an estimated 538 rows, all of which are useful for the query -- no further filtering needed.

                  结论:

                  • INDEX(filetime, ext) 只使用了第一列.
                  • INDEX(ext, filetime) 使用了两列.
                  • = 中涉及的列首先放在索引中不管基数.
                  • 查询计划不会超出第一个范围"列.
                  • 基数"与复合索引和此类查询无关.
                  • INDEX(filetime, ext) used only the first column.
                  • INDEX(ext, filetime) used both columns.
                  • Put columns involved in = tests first in the index regardless of cardinality.
                  • The query plan won't go beyond the first 'range' column.
                  • "Cardinality" is irrelevant for composite indexes and this type of query.

                  (使用索引条件"意味着存储引擎 (InnoDB) 将使用索引中超出用于过滤的列.)

                  ("Using index condition" means that the Storage Engine (InnoDB) will use columns of the index beyond the one used for filtering.)

                  这篇关于涉及范围时,索引中首先出现更高基数的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:计算移动平均 MySQL? 下一篇:如何通过 Elasticsearch 模糊匹配电子邮件或电话?

                  相关文章

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

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