Use the index to improve query speed

Use the index to improve query speed
1. Introduction

in web development, page templates, business logic (including caching, connection pooling) and three parts database, which is responsible for the implementation of the database in SQL query and returns the query results, the most important is the impact site speed performance bottlenecks. This paper mainly deals MySql database, the electricity supplier war two-eleven, led to Taobao technology hot, and Taobao now go IOE (the I on behalf of IBM acronym, that is to IBM's storage devices and minicomputers; O on behalf of Oracle acronym, that is to Oracle databases, using MySQL and Hadoop alternative solution,; E is representative of EMC2, that is, to the equipment of EMC2, EMC2 alternative use PC Server), extensive use MySql cluster! Let MySql once again become the shining star! The important step is to optimize the data of the index established for the slow query mysql appeared in, we can improve query speed by using the index. Index is used to quickly find a specific value in a column in a row. Do not use an index, MySQL will be a full table scan, starting from Article 1 of the logs and read the entire list until you find the relevant rows.

2.mysql index type and create a
common index types are
(1) the primary key index

It is a special unique index, does not allow nulls. Usually at the same time create a primary key index when construction of the table:

CREATE TABLE user(
id int unsigned not null auto_increment,
name varchar(50) not null,
email varchar(40) not null,
primary key (id)
); 

(2) general index

This is the most basic index, it does not have any restrictions. Created:

create index idx_name on user(
name(20)
); 

mysql support prefix index, generally does not name more than 20 characters, so we are here when indexing defines the length of 20, it saves the index file size

(3) unique index
that is similar to the foregoing general index, is different: the value of the index columns must be unique, but allow nulls. If it is a combination of the index, the column value must be unique. Created:

CREATE UNIQUE INDEX idx_email ON user(
email
); 

(4) full-text index

MySQL supports full text indexing and search capabilities. MySQL full-text index of type FULLTEXT index. FULLTEXT indexes only for MyISAM table;

CREATE TABLE articles (
   id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
   title VARCHAR(200),
   body TEXT,
   FULLTEXT (title,body)
    );

mysql> SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('database');

Search result:
+ ---- + + --------------------- ------------------- + ---------------------
| the above mentioned id | title | body |
+ ---- + --------------- + ------------------------------------------ + ----
| 5 | MySQL vs. YourSQL | an In at The following Database Comparison ... |
| 1 | MySQL the Tutorial | Stands for the DBMS DataBase ... |
+ ---- + --------------- + ------------------------------------------ + ----
2 rows the SET in (0.00 sec)
MATCH () function of natural language search for a string in the execution of its database. A library is a set of one or two included in the column FULLTEXT. AGAINST search string as a parameter () is given in. For each row in the table, the MATCH () returns a correlation value, i.e., the search string and the MATCH () specified in the table column of a similarity measure between the lines of text.
(5) composite index
CREATE TABLE test (
    id INT NOT NULL,
    last_name CHAR(30) NOT NULL,
    first_name CHAR(30) NOT NULL,
    PRIMARY KEY (id),
    INDEX name (last_name,first_name)
);
name index is an index on the last_name and first_name. It may be used to index last_name, or a specified value and the query last_name first_name within a known range. Thus, name index for the following query:
the SELECT * Test the FROM the WHERE last_name = 'Widenius,';
the SELECT * Test the FROM the WHERE last_name = 'Widenius,' the AND FIRST_NAME = 'Michael';
but not in SELECT * FROM test WHERE first_name = ' Michael '; this is because the index is the result of a combination of MySQL "the most left-prefix", the simple combination of understanding is only the beginning from the leftmost.

3. Use the index under what circumstances
(1) build the index for the search field, if your table, you often used to do a field search, then, give it to index it. In general, the column appears in the WHERE and JOIN need to index to improve query speed.
For example (table has the name field) fps retrieved from the table's name is "Li Wu," who,
following with explain to explain the implementation indexed and non-indexed difference:

a. not established before the index

explain select name from fps where name="李武";


[SQL] select name from fps where name="李武";

The impact of data fields: 0

Time: 0.003ms

b. After indexing

create index idx_name on fps(
name
);

explain select name from fps where name = " Wu Li";

[SQL] name from the SELECT FPS the WHERE name = "Wu Li";
data field effect: 0

时间: 0.001ms

(2)下面我们就来看看这个EXPLAIN分析结果的含义。 
table:这是表的名字。 
type:连接操作的类型。下面是MySQL文档关于ref连接类型的说明: 
“对于每个来自于前面的表的行组合,所有有匹配索引值的行将从这张表中读取。如果联接只使用键的最左边的前缀,或如果键不是
UNIQUE或PRIMARY KEY(换句话说,如果联接不能基于关键字选择单个行的话),则使用ref。如果使用的键仅仅匹配少量行,该联接
类型是不错的。” 在本例中,由于索引不是UNIQUE类型,ref是我们能够得到的最好连接类型。 如果EXPLAIN显示连接类型是“ALL”,而且你并不想从表里面选择出大多数记录,那么MySQL的操作效率将非常低,因为它要扫描整个表。你可以加入更多的索引来解决这个问题。预知更多信息,请参见MySQL的手册说明。 
possible_keys: 
可能可以利用的索引的名字。这里的索引名字是创建索引时指定的索引昵称;如果索引没有昵称,则默认显示的是索引中第一个列的名字
(在本例中,它是“idx_name”)。
Key: 
它显示了MySQL实际使用的索引的名字。如果它为空(或NULL),则MySQL不使用索引。 
key_len: 
索引中被使用部分的长度,以字节计。
ref: 
它显示的是列的名字(或单词“const”),MySQL将根据这些列来选择行。在本例中,MySQL根据三个常量选择行。 
rows: 
MySQL所认为的它在找到正确的结果之前必须扫描的记录数。显然,这里最理想的数字就是1。 本例中未索引前遍历的记录数为1041,而建立索引后为1
Extra:
这里可能出现许多不同的选项,其中大多数将对查询产生负面影响。在本例中,MySQL只是提醒我们它将用using where,using index子句限制搜索结果集。 

4.最常用的存储引擎:
(1)Myisam存储引擎:每个Myisam在磁盘上存储成三个文件。文件名都和表名相同,扩展名分别为.frm(存储表定义)、.MYD(存储数据)、.MYI(存储索引)。数据文件和索引文件可以放置在不同目录,平均分布io,获得更快的速度。对存储大小没有限制,MySQL数据库的最大有效表尺寸通常是由操作系统对文件大小的限制决定的,
(2)InnoDB存储引擎:具有提交、回滚、奔溃恢复能力的事务安全。与Myisam相比,InnoDB的写效率差一些并且会占用更多的磁盘空间以保留数据和索引。
(3)如何选择合适的引擎
下面是常用存储引擎适用的环境:
Myisam:它是在Web、数据仓储和其他应用环境下最常使用的存储引擎;
InnoDB:用于事务处理应用程序,具有更多特性,包括ACID事务特性。

参考资料:

http://www.cnblogs.com/ggjucheng/archive/2012/11/04/2754128.html
http://blog.csdn.net/jiedushi/article/details/6530568


发布了41 篇原创文章 · 获赞 114 · 访问量 68万+

Guess you like

Origin blog.csdn.net/hil2000/article/details/8194703