select * from user where name='liuyi' limit 1; 如果没有建立索引,查一条数据的时候后面加上limit 1速度会快点
查询一个表有多少数据
select count(1) fomr user;
开启查询计时:
set profiling=1; 开启
select * from user;查询数据
show profiles; 显示用了多少时间
查看搜索时是全表扫描还是用的索引:
explain select * from user where name='liuyi';
显示all 是通过全表查找,res是普通索引,const是主键索引和唯一索引
如果创建的表引擎不是 innodb ,type显示的system
索引:
普通索引
- 创建表的时候创建:
create table user(id int,name char(10),index index_name (name))
- 给一个表添加索引:
create index index_name on user(name)
- 删除索引:
drop index index_user_name on user;
alter table user drop index index_name;
- 查看索引:
show index from user;
唯一索引
- 创建表的时候创建:
create table user(id int,name char(10),unique index_name (name));
- 给一个表添加唯一索引:
create unique index index_name on user(name);
主键索引
组合索引
注意:联合索引有多个字段,搜索的时候最左前缀才能走索引
列如:(name,email) 做了联合索引
- select * from user where name='liuyi' 走的索引
- select * from user where name='liuyi' and email='xxx' 走的索引
- select * from user where email='xx' 走的就是全表扫描了
全文索引

浙公网安备 33010602011771号