Home Database Mysql Tutorial Mysql-索引学习(一)_MySQL

Mysql-索引学习(一)_MySQL

Jun 01, 2016 pm 01:44 PM
study Inquire

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
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check your academic qualifications on Xuexin.com

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

12306 How to check historical ticket purchase records How to check historical ticket purchase records

How to check the activation date on Apple mobile phone How to check the activation date on Apple mobile phone Mar 08, 2024 pm 04:07 PM

How to check the activation date on Apple mobile phone

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

Let's learn how to input the root number in Word together

Comparison of similarities and differences between MySQL and PL/SQL Comparison of similarities and differences between MySQL and PL/SQL Mar 16, 2024 am 11:15 AM

Comparison of similarities and differences between MySQL and PL/SQL

How to check the latest price of Tongshen Coin? How to check the latest price of Tongshen Coin? Mar 21, 2024 pm 02:46 PM

How to check the latest price of Tongshen Coin?

Understand these 20 Dune analysis dashboards and quickly capture trends on the chain Understand these 20 Dune analysis dashboards and quickly capture trends on the chain Mar 13, 2024 am 09:19 AM

Understand these 20 Dune analysis dashboards and quickly capture trends on the chain

Learn the main function in Go language from scratch Learn the main function in Go language from scratch Mar 27, 2024 pm 05:03 PM

Learn the main function in Go language from scratch

See all articles