索引的增删改成查

1 创建表时就指定索引

create table t1(

  id int,

  name char(5),

  unique key nui_name(name),

  primary key (id)

);

create table t2(

  id int,

  name char(5),

  index idx_name(name)

);

2 在创建完表后为其添加索引

create table t3(

  id int,

  name char(5)

);

create inde idx_name on t3(name);

alter table t3 add index idx_id(id);

alter table t3 add primary key(id);

查看
mysql> show create table t3;
+-------+-------------------------------------
| Table | Create Table
+-------+-------------------------------------
| t3 | CREATE TABLE `t3` (
`id` int(11) DEFAULT NULL,
`name` char(5) DEFAULT NULL,
KEY `idx_name` (`name`),
KEY `idx_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------
1 row in set (0.00 sec)


删除
drop index idx_id on t3;

alter table t3 drop primary key;

 

 

 

 

#加索引可以加快查询效率,但是会降低写的效率
mysql> select count(*) from s1 where id=100000;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.27 sec)

mysql> create index idx_id on s1(id); #速度很慢
Query OK, 0 rows affected (3.27 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select count(*) from s1 where id=100000;#查询条件中的字段是索引字段,速度很快
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from s1 where name='egon'; #查询条件中的字段是非索引字段,速度仍然很慢
+----------+
| count(*) |
+----------+
| 793686 |
+----------+
1 row in set (0.30 sec)


select count(*) from s1 where name='egon' and gender ='male' and id > 3333 and email ='xxx';
对于多个and条件,会依次往右找到一个区分度高的索引字段,加速查询
对于多个or条件,会依次从左到右判断
create index idx_xx on s1(name,gender,id,email); #无法加速查询


create index idx_xx on s1(name,gender,email,id); #可以加速查询

 

 

 

 


create index idx_name on s1(name);
create index idx_id on s1(id);
select count(*) from s1 where name='egon' and id=3000;
select count(*) from s1 where name='egon' and id>3000;

 

select count(*) from s1 where name='egon' or id=3000;

 

 

#总结:正确使用索引的注意事项
1、应该选择区分度高的字段作为索引字段
2、范围问题(:>、>=、<、<=、!= 、between...and...、like、),范围过大,即便是有索引速度也会很慢
3、索引字段一定不能参与计算:select * from s1 where id*10 > 12000;
4、最左前缀匹配原则:
应该把范围查询字段往右放


create index idx_xx on s1(id,name,gender,email);
id name gender email
id gender email
id email

gender email


#可以匹配的条件是:
1、id=1 and name = 'egon' and gender='male' and email ='xxx';
2、id=1 and email ='xxx';
3、id=1 and name = 'egon' and email ='xxx';
4、name = 'egon' and email ='xxx';
5、name = 'egon' and gender='male' and email ='xxx';

select count(*) from s1 where id = 1 and name = 'egon' and gender ='male' and email = 'xxxx';
select count(*) from s1 where name = 'egon' and email ='xxx';
select count(*) from s1 where name = 'egon' and gender='male' and email ='xxx';

select count(*) from s1 where name = 'egon' and id>3 and gender='male' and email ='xxx';

 

create index idx_xx on s1(gender,email,name,id)
能够匹配条件中出现的字段如下
gender email name id/id email name gender
gender id
gender email
gender email id
gender

select count(*) from s1 where name = 'egon' and id>3 and gender='male' and email ='xxx';

select count(*) from s1 where name = 'egon' and id>3 and email ='xxx';
select count(*) from s1 where gender='male';
select count(*) from s1 where gender='xxx';

 

转载于:https://www.cnblogs.com/djjv/p/7762669.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值