Home Database Mysql Tutorial MySQL SQL优化的索引问题详解

MySQL SQL优化的索引问题详解

Jun 07, 2016 pm 04:09 PM
mysql sql main optimization article index Detailed explanation question

以下的文章主要介绍的是MySQL SQL优化的索引问题,我们大家都知道在一般的数据中,很多人喜欢用相关索引来对MySQL数据库进行优化。我们通过相关索引在一般的情况下,是帮助我们解决大多数的MySQL SQL性能问题。 1. 索引的存储分类 MyISAM存储引擎的表的数据

以下的文章主要介绍的是MySQL SQL优化的索引问题,我们大家都知道在一般的数据中,很多人喜欢用相关索引来对MySQL数据库进行优化。我们通过相关索引在一般的情况下,是帮助我们解决大多数的MySQL SQL性能问题。

1. 索引的存储分类

MyISAM存储引擎的表的数据和索引时自动分开存储的,各自是独立的一个文件;InnoDB存储引擎的表的数据和索引时存储在同一表空间里面,但可以有多个文件组成。

MySQL中索引的存储类型目前只有两种(BTREE和HASH),具体和表的存储引擎相关;MyISAM和InnoDB存储引擎都只支持BTREE索引;MEMORY/HEAP存储引擎可以支持HASH和BTREE索引。

MySQL目前不支持函数索引,但是能对列的前面某一部分进行索引,例如name字段,可以以只取name的前4个字符进行索引,这个特征可以大大缩小索引文件的大小。在设计表结构的时候也可以对文本列根据此特性进行灵活设计。例如

引用

<ol class="dp-xml"><li class="alt"><span><span>create index ind_company2_name on company2(name(4))  </span></span></li></ol>
Copy after login

2. MySQL如何使用索引

索引用于快速找出在某个列中有一特定值的行。对相关列使用索引时提高SELECT操作性能的最佳途径。
查询要使用索引最主要的条件是查询条件中需要使用索引关键字,如果是多列MySQL SQL优化索引,那么只有查询条件使用了多列关键字最左边的前缀时,才可以使用索引,否则将不能使用索引。

1. 使用索引

在MySQL中,下列几种情况下可能使用索引。

对于创建的多列索引,只要查询的条件中用到了最左边的列,索引一般就会使用。

例如:

引用

我们首先按company_id ,Moneys的顺序创建一个复合索引

<ol class="dp-xml"><li class="alt"><span><span>create index ind_sales2_companyid_moneys on sales2(company_id,moneys)  </span></span></li></ol>
Copy after login

如果按company_id进行表查询

引用

使用explain来分析下

<ol class="dp-xml">
<li class="alt"><span><span>explain select * from sales2 where </span><span class="attribute">company_id</span><span> =</span><span class="attribute-value">2000</span><span> \G;   </span></span></li>
<li>
<span>explain select * from sales2 where </span><span class="attribute">moneys</span><span> = </span><span class="attribute-value">1</span><span>\G;  </span>
</li>
</ol>
Copy after login

通过上面你可以发现即便where条件中不是用company_id 和 moneys的组合条件,MySQL SQL优化之索引仍然能用到,这就是索引的前缀特性。但是如果只按照moneys条件查询表,那么索引就不会被用到。

对于使用like的查询,后面如果是常量并且只有%号不在第一字符,索引才能会被使用例如

引用

<ol class="dp-xml">
<li class="alt"><span><span>explain select * from company2 where name like "%3"\G;   </span></span></li>
<li><span>explain select * from company2 where name like "3%"\G;  </span></li>
</ol>
Copy after login

以上两句你可以认为是一样的。其实是不一样的。第一句其实没有用到索引,而第二句才能够利用到索引。另外如果like后面跟的是一个列的名字,那么索引也不会被使用。

如果对大是文本进行搜索,使用全文索引而不用使用like"%..%" 如果列名是索引,使用column_name is null 将使用MySQL SQL优化之索引如 查询name为nll的记录就用到了索引

引用

<ol class="dp-xml"><li class="alt"><span><span>explain select * from company2 where name is null \G;  </span></span></li></ol>
Copy after login

2. 下面一些情况存在索引但不使用索引,你可能认为它会用,但是实际上它就是没用。

引用

1. 如果Mysql估计使用索引比全表扫描更慢,则不使用索引。

例如列key_part1均匀分布在1和100之间,下列查询中使用索引就不是很好

<ol class="dp-xml"><li class="alt"><span><span>select * from table_name where key_part1 </span><span class="tag">></span><span> 1 and key_part1 </span><span class="tag"><span> </span><span class="tag-name">90</span><span>;  </span></span></span></li></ol>
Copy after login

2. 如果使用MEMORY/HEAP表并且where条件中不使用"="进行索引列,那么不会用到索引。heap表只有在" ="的条件下才会使用索引

3. 用or分割开的条件,如果or前的条件中的列有索引,而后面的列中没用MySQL SQL优化之索引,那么涉及的索引都不会被用到

4. 如果不是索引列的第一部分,那么也不会使用。

5. 如果like是以"%"开始

6. 如果列类型是字符串,那么一定记得在where条件中把字符常量值用引号引起来,否则即便这个列上有索引,Mysql也不会使用。因为MYSQL默认把输入的常量值进行转换以后才进行检索。
 

最后查看索引使用情况

如果索引正在工作,Handler_read_key的值将很高,这个值代表了一个行被索引值读的次数,很低的值表明增加索引得到的性能改善不高,因为索引经常不被使用到。Handler_read_rnd_next的值高则说明查询运行低效,并且应该建立索引补救。这个值的含义是在数据文件中读取下一行的请求数。如果正进行大量的表扫描,Handler_read_rnd_next的值较高,则通常说明表索引不正确或者写入的查询没有利用MySQL SQL优化之索引。

还记得怎么看Handler_read_rnd_next 吗? 使用

<ol class="dp-xml"><li class="alt"><span><span>show statuts like 'Handler_read_%';  </span></span></li></ol>
Copy after login

以上的相关内容就是对MySQL SQL优化的索引问题的介绍,望你能有所收获。


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

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

See all articles