mysql 索引

索引

用空间换时间

create index empno_index on emp(empno);
-- empno_index 这是索引的名称 
-- emp 表的名字
-- empno 被添加索引的字段名
  • 索引类型

    • 主键索引,主键自动的为主索引(类型primary key)

    • 唯一索引(unique)

    • 普通索引(index)

    • 全文索引(fulltext)【适用于myisam】

      一般开发,不适用mysql自带的全文索引,而是使用:全文搜索solr和elasticsearch(es)

create table t1(
id int primary key, -- 主键,同时也是索引,称为主键索引
name varchar(32)
);

create table t2(
id int unique --id 是唯一的,同时也是索引,称为unique索引 
);

创建索引

-- 查看表是否有索引
show indexes from t25;
-- 添加唯一索引
create unique index id_index on t25(id);

-- 添加普通索引
create index id_index on t25(id);

-- 如果某列的值,是不会重复的,则优先考虑使用unique索引,否则使用普通索引

-- 添加普通索引方式2
alter table t25 add index in_index(id)

-- 添加主键索引
alter table t26 add primary key(id)

删除索引

drop index id_index on t25
--         索引名称      表名

-- 删除主键索引
alter table t26 drop primary key

查询索引

show index from t26;

show indexes from t26;

show keys from t26;

desc t26;

创建索引的规则

  • 较频繁的作为查询条件字段应该创建索引
  • 唯一性太差的字段不适合单独创建索引,即使频繁作为查询条件
  • 更新非常频繁的字段不适合创建索引
  • 不会出现在where子句中字段不该创建索引。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GaoJa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值