search
HomeDatabaseMysql TutorialMySQLSQL优化索引的实际问题破解

MySQLSQL优化索引的实际问题破解

Jun 07, 2016 pm 04:11 PM
optimizationPractical problemscrackindex

MySQLSQL优化索引的实际问题解决在实际中的应用比例还是占为多数的,如果你对MySQLSQL优化索引的实际问题解决技术,心存好奇的话,以下的文章将会揭开它的神秘面纱。 一般在数据中,很多朋友很喜欢用索引来对数据库优化。通过MySQLSQL优化索引通常可以帮助我

MySQLSQL优化索引的实际问题解决在实际中的应用比例还是占为多数的,如果你对MySQLSQL优化索引的实际问题解决技术,心存好奇的话,以下的文章将会揭开它的神秘面纱。

一般在数据中,很多朋友很喜欢用索引来对数据库优化。通过MySQLSQL优化索引通常可以帮助我们解决大多数的SQL性能问题。

1. 索引的存储分类

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

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

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

引用

create index ind_company2_name on company2(name(4))

2. MySQL如何使用索引

索引用于快速找出在某个列中有一特定值的行。对相关列使用索引时提高SELECT操作性能的最佳途径。

查询要使用索引最主要的条件是查询条件中需要使用索引关键字,如果是多列索引,那么只有查询条件使用了多列关键字最左边的前缀时,才可以使用索引,否则将不能使用索引。

1. 使用索引

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

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

例如:

引用

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

create index ind_sales2_companyid_moneys on sales2(company_id,moneys)

如果按company_id进行表查询

引用

使用explain来分析下

explain select * from sales2 where company_id =2000 \G;

explain select * from sales2 where moneys = 1\G;

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

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

引用

explain select * from company2 where name like "%3"\G;

explain select * from company2 where name like "3%"\G;

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

如果对大是文本进行搜索,使用全文索引而不用使用like"%..%"

如果列名是索引,使用column_name is null 将使用索引如

查询name为nll的记录就用到了索引

引用

explain select * from company2 where name is null \G;

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

引用

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

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

select * from table_name where key_part1 > 1 and key_part1

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

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

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

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

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

最后查看索引使用情况

如果索引正在工作,Handler_read_key的值将很高,这个值代表了一个行被索引值读的次数,很低的值表明增加索引得到的性能改善不高,因为索引经常不被使用到。Handler_read_rnd_next的值高则说明查询运行低效,并且应该建立MySQLSQL优化索引补救。

这个值的含义是在数据文件中读取下一行的请求数。如果正进行大量的表扫描,Handler_read_rnd_next的值较高,则通常说明表索引不正确或者写入的查询没有利用索引。


Statement
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)