>  기사  >  데이터 베이스  >  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으로 문의하세요.