设为首页 收藏本站
查看: 351|回复: 0

[经验分享] MySQL--索引

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-9-7 10:40:57 | 显示全部楼层 |阅读模式
- | 索引的作用
1、约束
2、加速查找
- | 为什么索引可以这么快?
为什么索引可以这么快?
name列建立索引
创建一个文件,name列所有数据拿出来
通过数字来标记,在一种特殊的数据结构中查找
层级越多,越快
(B-tree索引)
| 索引的种类
-
| 普通索引
-- 加速查找
1-创建表-创建索引
--创建表的同时创建索引
create table in1(
    nid int not bull auto_increment primary key,
    name varchar(32) not null,
    email varchar(64) not null,
    index ix_name (name)
);
2-创建索引
-- 在创建表之后再去创建索引
create index 索引名 on 表名(列名)
create index index_name on table_name(column_name)
3-删除索引
# error
drop index index_name on table_name;
4-查看索引
show index from table_name
-
| 唯一索引
-- 加速查找,约束列数据不能重复,null
1-创建表-创建索引
-- 创建表的同时创建唯一索引
create table in1(
    nid int not null auto_increment primary key,
    name varchar(32) not null,
    email varchar(64) not null,
    extra text,
    unique ix_name (name)
);
2-创建索引
-- 在创建表之后再取创建索引
create unique index 索引名 on 表名(列名)
3-删除索引
drop unique index 索引名 on 表名
-
| 主键索引
-- 加速查找,约束列数据不能重复,不能null
1-创建表-创建索引
create table in1(
    nid int not null auto_increment primary key,
    name varchar(32) not null,
    email varchar(64) not null,
    extra text,
    index ix_name (name)
)
OR
create table in1(
    nid int not null auto_increment,
    name varchar(32) not bull,
    email varchar(64) not null,
    extra text,
    primary key(nid),
    index ix_name (name)
)
2-创建主键
alter table 表名add primary key(列名)
3-删除主键
alter table 表名 drop primary key;
alter table 表名 modify 列名 int, drop primary key;
-
| 组合索引
-- 多列可以创建一个索引文件
2-联合唯一索引:
    有约束,两列数据都不相同,才能插入,不然报错
# 查找:最左匹配(最左前缀)
select * from tb1 where name = 'alex'
select * from tb1 where name = 'alex' and pwd = '123'
select * from tb1 where pwd = '123'  # 不会走索引
- | 索引的优缺点
--增删改慢,耗费资源
--查找快
- | 覆盖索引
-- 下例中nid设置了索引
select * from tb where nid=1;
# 先去索引中找
# 再取数据表中找
select nid from tb where nid < 10;
-- 情况应用上了索引,并且不用去数据库中操作,覆盖索引
# 只需要在索引表中就能获取数据
- | 合并索引
-- 列: nid, name(单独索引) ,email(单独索引), pwd
    select * from tb where name = 'alex'
    select * from tb where email = 'alex3714@163.com'
    select * from tb where name = 'alex' or email = 'alex3714@163.com'
    # 合并索引
- | 执行计划
-- 相对比较准确的表达出当前SQL运行的状况
-- 是否走索引,走的是什么索引
1、 explain SQL语句
    type:ALL   -- 全数据表扫描
    type:index    -- 全索引表扫描
2、limit
    select * from tb1 where email = '123';
    select * from tb1 where email limit = 1;
----- SQL: ALL、Index, 都是
有优化的余地 -----
3、range
    执行范围查询,type是range,
    如果没有索引type是ALL,全表扫描
注意:!= 和 > 符号,不走索引



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-404087-1-1.html 上篇帖子: MySQL--数据的查找 下篇帖子: MySQL--事务
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表