2019-04-29-MySQL索引是什么

2019-04-29  本文已影响0人  pepsi1000

来自这里

为什么需要索引

select * from t_user where user_name='foobar'

这条查询语句,在没有创建索引的情况下,执行之后会将t_user表的所有行按顺序扫描下来,判断每一行是否匹配。

如果t_user表存在上百万行数据时,那么数据库执行这条查询语句时将消耗大量时间。

什么是索引

An index is a data structure such as B-Tree that improves the speed of data retrieval on a table at the cost of additional writes and storage to maintain it.

简单来说,索引就是数据库的一种数据结构,例如B-Tree

两种索引

MySQL创建索引的两种方式

方式一

CREATE TABLE t(
   c1 INT PRIMARY KEY,
   c2 INT NOT NULL,
   c3 INT NOT NULL,
   c4 VARCHAR(10),
   INDEX (c2,c3) 
);

方式二

CREATE INDEX idx_c4 ON t(c4);

注意:By default, MySQL creates the B-Tree index if you don’t specify the index type.

存储引擎与支持的索引类型有关。InnoDB 和 MyISAM 只支持 BTREE。

卡片说明

这张卡片的内容属于 MySQL Index Tutorial 的第一篇文章,这个MySQL索引系列教程质量很好,推荐阅读。

上一篇下一篇

猜你喜欢

热点阅读