Home > Database > Mysql Tutorial > body text

Mysql-索引学习(一)_MySQL

WBOY
Release: 2016-06-01 13:44:39
Original
996 people have browsed it

bitsCN.com

使用索引可以提高查询效率,废话不多说,先来个例子。

 

Sql代码:

CREATE TABLE `person` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  `name` varchar(20) DEFAULT NULL,  
  `birthday` datetime DEFAULT NULL,  
  `isMan` int(11) DEFAULT NULL,  
  `salary` double DEFAULT NULL,  
  `test` int(11) DEFAULT NULL,  
  PRIMARY KEY (`id`)  
)  
CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `birthday` datetime DEFAULT NULL,
  `isMan` int(11) DEFAULT NULL,
  `salary` double DEFAULT NULL,
  `test` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

 

 在插入一些数据。例如:


/

 

运行:

EXPLAIN select name from person where name='linjia4'

观察结果:


/


 row=23是因为这个表总共有23条记录,即是说做了全表扫描

 

在搜索的“name”加上索引:

create index testtest on person (name)

再执行EXPLAIN select name from person where name='linjia4'

观察结果:


/


 rows=1,即是所当查询语句执行时通过name的索引,直接定位到那条数据,不用做全表扫描

 

索引的效果是显而易见的,大大的提高了查询的速度。但索引也不能滥用,增加索引是要付出代价的。

 

接下来的文章会介绍索引更深层次的知识,我希望通过自己的理解,用一种简单易懂的方式阐述知识

摘自:邻家的技术仓库

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!