【SQL高级优化】——第九章普通索引、唯一索引

一、普通索引

    这是最基本的索引,它没有任何限制,也是我们大多数情况下用到的索引。

   1、创建普通索引和删除索引的语法

    #创建普通索引语法:create index index_name on table_name(列名);

    #删除索引语法:drop index title_index on table_name;

    注:同表的索引不能重名  不同表的可以重名

    2、验证使用普通索引效果

     (1)创建索引前执行一个select语句,条件是非主键字段。

select * from test where proposalno='TDAA201321020000037968';  -- 执行时间14.857s
explain select * from test where proposalno='TDAA201321020000037968'; 

   (2)给条件字段创建普通索引并查看

create index title_index on test(proposalno);
show index from test;

(3)再次执行之前的select和explain语句

select * from test where proposalno='TDAA201321020000037968';  -- 查询时间0.026s
explain select * from test where proposalno='TDAA201321020000037968'; 

 可以看到type=ref,查询性能得到了提升,key=title_index说明用到了索引。

二、唯一索引

 1、创建并查看唯一索引

create unique index proposalno_index on test(proposalno);
show index from test;

2、 再次执行之前的select和explain语句

select * from test where proposalno='TDAA201321020000037968';  -- 0.024s
explain select * from test where proposalno='TDAA201321020000037968'; 

可以看到type=const,比之前的普通索引的执行计划type=ref,性能又有了提升。key=proposalno_index,说明也用到了索引。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值