yzx3323 2009-12-29 12:33
浏览 206
已采纳

数据库索引有什么好处?请给出具体例子

数据库索引有什么好处?请给出具体例子

  • 写回答

4条回答 默认 最新

  • xxxxxxxxxxxxxxxxx 2009-12-29 13:38
    关注

    以mysql为例子。 表结构:
    mysql> desc demo;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id | int(11) | NO | PRI | | |
    | name | varchar(10) | YES | MUL | NULL | |
    +-------+-------------+------+-----+---------+-------+

    索引状况:
    show index in demo;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+- --------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+- --------+
    | demo | 0 | PRIMARY | 1 | id | A | 4 | NULL | NULL | | BTREE | |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+- --------+

    查询计划:
    explain select * from demo where name like '李%';
    +----+-------------+-------+------+---------------+------+---------+------+- -----+-------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+------+---------------+------+---------+------+- -----+-------------+
    | 1 | SIMPLE | demo | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
    +----+-------------+-------+------+---------------+------+---------+------+- -----+-------------+
    [color=violet]注意到没有使用任何索引.[/color]

    添加索引:
    create index name_index_demo on demo;

    现在的索引状况:
    show index in demo;
    +-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | demo | 0 | PRIMARY | 1 | id | A | 4 | NULL | NULL | | BTREE | |
    | demo | 1 | name_index_demo | 1 | name | A | NULL | NULL | NULL | YES | BTREE | |
    +-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

    新的查询的查询计划:
    explain select * from demo where name like '李%';
    +----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-------------+
    | id | select_type | table | type | possible_keys | [color=red]key[/color] | [color=red]key_len[/color] | ref | rows | Extra |
    +----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-------------+
    | 1 | SIMPLE | demo | range | name_index_demo | [color=red]name_index_demo[/color] | [color=red]13[/color] | NULL | 1 | Using where |
    +----+-------------+-------+-------+-----------------+-----------------+---------+------+------+-------------+
    1 row in set (0.03 sec)

    [color=violet]注意到查询已经使用了我们创建的索引.[/color]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog