Index overview and index classification

  Indexes are special files (indexes on InnoDB tables are an integral part of a tablespace) that contain pointers to all the records in the table. More generally speaking, the database index is like the table of contents in front of a book, which can speed up the query speed of the database. Indexing is the key to fast searches.

  In database tables, indexing fields can greatly improve query speed. Suppose we create a mytable table:

CREATE TABLE mytable(   ID INT NOT NULL,    username VARCHAR(16) NOT NULL  );   

 

We randomly inserted 10,000 records into it, one of which was: 5555, admin.

Looking for records with username="admin" 

SELECT * FROM mytable WHERE username='admin';

 

If an index has been created on username, MySQL can accurately find the record without any scanning. Instead, MySQL scans all records, i.e. 10000 records to query.

The index is divided into single-column index and composite index. Single-column index, that is, an index contains only a single column, a table can have multiple single-column indexes, but this is not a composite index. Composite index, that is, an index contains multiple columns.

MySQL index types include:

 

(1) Common index

This is the most basic index, it has no restrictions. It can be created in the following ways:

◆Create an index

CREATE INDEX indexName ON mytable(username(length));

For CHAR and VARCHAR types, the length can be less than the actual length of the field; for BLOB and TEXT types, length must be specified, the same below.

◆Modify the table structure

ALTER mytable ADD INDEX [indexName] ON (username(length)) 

◆ Specify directly when creating a table

CREATE TABLE mytable(   ID INT NOT NULL,    username VARCHAR(16) NOT NULL,   INDEX [indexName] (username(length))   );  

Syntax to drop an index:

DROP INDEX [indexName] ON mytable;

 

(2) Unique index

It is similar to the previous ordinary index, the difference is: the value of the index column must be unique, but null values ​​are allowed. In the case of a composite index, the combination of column values ​​must be unique. It can be created in the following ways:

◆Create an index

CREATE UNIQUE INDEX indexName ON mytable(username(length)) 

◆Modify the table structure

ALTER mytable ADD UNIQUE [indexName] ON (username(length)) 

◆ Specify directly when creating a table

CREATE TABLE mytable(   ID INT NOT NULL,    username VARCHAR(16) NOT NULL,   UNIQUE [indexName] (username(length))   ); 

 

(3) Primary key index

It is a special unique index that does not allow nulls. Generally, the primary key index is created at the same time when the table is built:

CREATE TABLE mytable(   ID INT NOT NULL,    username VARCHAR(16) NOT NULL,   PRIMARY KEY(ID)   ); 

Of course, you can also use the ALTER command. Remember: a table can only have one primary key.

 

(4) Combined index

To visually compare single-column and composite indexes, add multiple fields to the table:

CREATE TABLE mytable(   ID INT NOT NULL,    username VARCHAR(16) NOT NULL,   city VARCHAR(50) NOT NULL,   age INT NOT NULL  );  

为了进一步榨取MySQL的效率,就要考虑建立组合索引。就是将 name, city, age建到一个索引里:

ALTER TABLE mytable ADD INDEX name_city_age (name(10),city,age);

建表时,usernname长度为 16,这里用 10。这是因为一般情况下名字的长度不会超过10,这样会加速索引查询速度,还会减少索引文件的大小,提高INSERT的更新速度。

如果分别在 usernname,city,age上建立单列索引,让该表有3个单列索引,查询时和上述的组合索引效率也会大不一样,远远低于我们的组合索引。虽然此时有了三个索引,但MySQL只能用到其中的那个它认为似乎是最有效率的单列索引。

建立这样的组合索引,其实是相当于分别建立了下面三组组合索引:

usernname,city,age   usernname,city   usernname  为什么没有 city,age这样的组合索引呢?这是因为MySQL组合索引“最左前缀”的结果。简单的理解就是只从最左面的开始组合。并不是只要包含这三列的查询都会用到该组合索引,下面的几个SQL就会用到这个组合索引:

SELECT * FROM mytable WHREE username="admin" AND city="郑州" 

SELECT * FROM mytable WHREE username="admin" 

而下面几个则不会用到:

SELECT * FROM mytable WHREE age=20 AND city="郑州"  

SELECT * FROM mytable WHREE city="郑州"

 

(5)建立索引的时机

到这里我们已经学会了建立索引,那么我们需要在什么情况下建立索引呢?一般来说,在WHERE和JOIN中出现的列需要建立索引,但也不完全如此,因为MySQL只对<,<=,=,>,>=,BETWEEN,IN,以及某些时候的LIKE才会使用索引。例如:

SELECT t.Name  FROM mytable t LEFT JOIN mytable m    ON t.Name=m.username WHERE m.age=20 AND m.city='郑州'

此时就需要对city和age建立索引,由于mytable表的userame也出现在了JOIN子句中,也有对它建立索引的必要。

刚才提到只有某些时候的LIKE才需建立索引。因为在以通配符%和_开头作查询时,MySQL不会使用索引。例如下句会使用索引:

SELECT * FROM mytable WHERE username like'admin%' 

而下句就不会使用:

SELECT * FROM mytable WHEREt Name like'%admin' 

因此,在使用LIKE时应注意以上的区别。

 

(6)索引的不足之处

上面都在说使用索引的好处,但过多的使用索引将会造成滥用。因此索引也会有它的缺点:

◆虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件。

◆建立索引会占用磁盘空间的索引文件。一般情况这个问题不太严重,但如果你在一个大表上创建了多种组合索引,索引文件的会膨胀很快。

索引只是提高效率的一个因素,如果你的MySQL有大数据量的表,就需要花时间研究建立最优秀的索引,或优化查询语句。

 

(7)使用索引的注意事项

使用索引时,有以下一些技巧和注意事项:

◆索引不会包含有NULL值的列

只要列中包含有NULL值都将不会被包含在索引中,复合索引中只要有一列含有NULL值,那么这一列对于此复合索引就是无效的。所以我们在数据库设计时不要让字段的默认值为NULL。

◆使用短索引

对串列进行索引,如果可能应该指定一个前缀长度。例如,如果有一个CHAR(255)的列,如果在前10个或20个字符内,多数值是惟一的,那么就不要对整个列进行索引。短索引不仅可以提高查询速度而且可以节省磁盘空间和I/O操作。

◆索引列排序

MySQL查询只使用一个索引,因此如果where子句中已经使用了索引的话,那么order by中的列是不会使用索引的。因此数据库默认排序可以符合要求的情况下不要使用排序操作;尽量不要包含多个列的排序,如果需要最好给这些列创建复合索引。

◆like语句操作

一般情况下不鼓励使用like操作,如果非使用不可,如何使用也是一个问题。like “%aaa%” 不会使用索引而like “aaa%”可以使用索引。

◆不要在列上进行运算

select * from users where YEAR(adddate)<2007; 

将在每个行上进行运算,这将导致索引失效而进行全表扫描,因此我们可以改成

select * from users where adddate<‘2007-01-01’; 

◆不使用NOT IN和<>操作

以上,就对其中MySQL索引类型进行了介绍。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326311680&siteId=291194637