1. <legend id='aXNuA'><style id='aXNuA'><dir id='aXNuA'><q id='aXNuA'></q></dir></style></legend>
    2. <small id='aXNuA'></small><noframes id='aXNuA'>

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

      如何向 MySQL 表添加索引?

      时间:2023-08-17

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

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

              <tbody id='g2Msw'></tbody>
              <bdo id='g2Msw'></bdo><ul id='g2Msw'></ul>
              1. 本文介绍了如何向 MySQL 表添加索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个非常大的 MySQL 表,其中包含大约 150,000 行数据.目前,当我尝试运行

                I've got a very large MySQL table with about 150,000 rows of data. Currently, when I try and run

                SELECT * FROM table WHERE id = '1';
                

                代码运行良好,因为 ID 字段是主索引.但是,对于项目的最新发展,我必须通过另一个字段搜索数据库.例如:

                the code runs fine as the ID field is the primary index. However, for a recent development in the project, I have to search the database by another field. For example:

                SELECT * FROM table WHERE product_id = '1';
                

                此字段以前未编入索引;但是,我添加了一个,因此 mysql 现在对该字段进行索引,但是当我尝试运行上述查询时,它运行速度非常慢.EXPLAIN 查询显示,当我已经添加了 product_id 字段时,没有索引,因此查询需要 20 分钟到 30 分钟的任何时间来返回单行.

                This field was not previously indexed; however, I've added one, so mysql now indexes the field, but when I try to run the above query, it runs very slowly. An EXPLAIN query reveals that there is no index for the product_id field when I've already added one, and as a result the query takes any where from 20 minutes to 30 minutes to return a single row.

                我的完整解释结果是:

                | id | select_type | table | type | possible_keys| key  | key_len | ref  | rows  | Extra       |
                +----+-------------+-------+------+--------------+------+---------+------+-------+------------------+
                |  1 | SIMPLE      | table | ALL  | NULL         | NULL | NULL    | NULL |157211 | Using where |
                +----+-------------+-------+------+--------------+------+---------+------+-------+------------------+
                

                注意到我刚刚看了一下,ID 字段存储为 INT 而 PRODUCT_ID 字段存储为 VARCHAR 可能会有所帮助.这可能是问题的根源吗?

                It might be helpful to note that I've just taken a look, and ID field is stored as INT whereas the PRODUCT_ID field is stored as VARCHAR. Could this be the source of the problem?

                推荐答案

                ALTER TABLE `table` ADD INDEX `product_id_index` (`product_id`)
                

                永远不要在 MySQL 中比较 integerstrings.如果 idint,请删除引号.

                Never compare integer to strings in MySQL. If id is int, remove the quotes.

                这篇关于如何向 MySQL 表添加索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:用于散列密码字段的数据类型和长度是多少? 下一篇:MySQL:如何重置或更改 MySQL 根密码?

                相关文章

                最新文章

                • <bdo id='rNnjk'></bdo><ul id='rNnjk'></ul>

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

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