首页  >  文章  >  数据库  >  Mysql-索引学习(一)_MySQL

Mysql-索引学习(一)_MySQL

WBOY
WBOY原创
2016-06-01 13:44:39997浏览

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
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn