MySQL数据库优化中要用到哪些语句?
以下的文章主要介绍的是MySQL数据库优化的实际操作以及相关推荐,前面我们也讲过一些相关的优化操作策略,我们今天就一起来看看MySQL数据库优化中Group BY 语句、 Order By语句 等。 优化GROUP BY语句 默认情况下,MySQL对所有GROUP BY col1,col2...的字段
以下的文章主要介绍的是MySQL数据库优化的实际操作以及相关推荐,前面我们也讲过一些相关的优化操作策略,我们今天就一起来看看MySQL数据库优化中Group BY 语句、 Order By语句 等。
优化GROUP BY语句
默认情况下,MySQL对所有GROUP BY col1,col2...的字段进行排序。这与在查询中指定ORDER BY col1,col2...类似。因此,如果显式包括一个包含相同的列的ORDER BY子句,则对MySQL的实际执行性能没有什么影响。 如果查询包括GROUP BY 但用户想要避免排序结果的消耗,则可以指定ORDER By NULL禁止排序,例如:
引用
<ol class="dp-xml"> <li class="alt"><span><span>explain select id, sum(moneys) from sales2 group by id \G </span></span></li> <li><span>explain select id, sum(moneys) from sales2 group by id order by null \G </span></li> </ol>
你可以通过比较发现第一条语句会比第二句在Extra:里面多了Using filesort.而恰恰filesort是最耗时的。
MySQL数据库优化ORDER BY语句
在某些情况中,MySQL可以使用一个索引来满足ORDER BY子句,而不需要额外的排序。WHERE 条件和 ORDER BY使用相同的索引,并且ORDER BY的顺序和索引顺序相同,并且ORDER BY的字段都是升序或者都是降序。
例如:
引用
<ol class="dp-xml"> <li class="alt"><span><span>SELECT * FROM t1 ORDER BY key_part1,key_part2,....: </span></span></li> <li> <span>SELECT * FROM t1 WHERE </span><span class="attribute">key_part1</span><span> = 1 ORDER BY key_part1 DESC,key_part2 DESC; </span> </li> <li class="alt"><span>SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 DESC; </span></li> </ol>
但是以下的情况不使用索引:
引用
<ol class="dp-xml"><li class="alt"><span><span>SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC; </span></span></li></ol>
ORDER by的字段混合ASC 和 DESC
<ol class="dp-xml"><li class="alt"><span><span>SELECT * FROM t1 WHERE </span><span class="attribute">key2</span><span>=</span><span class="attribute-value">constant</span><span> ORDER BY key1; </span></span></li></ol>
用于查询行的关键字与ORDER BY 中所使用的不相同
<ol class="dp-xml"><li class="alt"><span><span>SELECT * FROM t1 ORDER BY key1, key2; </span></span></li></ol>
对不同的关键字使用ORDER BY
MySQL数据库优化嵌套查询
MySQL4.1开始支持SQL的子查询。这个技术可以使用SELECT语句来创建一个单列的查询结果,然后把这个查询结果作为过滤条件用在另一个查询中,使用子查询可以一次性地完成多逻辑上需要多个步骤才能完成的SQL操作,同时也可以避免事务或者表锁死,并且些起来也很容易。但是,有些情况下,子查询可以被更有效的连接(JOIN)替代。
例如:
引用
<ol class="dp-xml"> <li class="alt"><span><span>explain select * from sales2 where company_id not in(select id from company2) \G </span></span></li> <li> <span>explain select * from sales2 left join comany2 on </span><span class="attribute">sales2.company_id</span><span> = <br></span><span class="attribute-value">company2</span><span>.id where sales2.company_id is null \G; </span> </li> </ol>
第一句看起来比第二句更简洁,但是第二句比第一就更快。因为使用JOIN来完成这个查询,速度比较快,尤其如果对compay2表中的id建立了索引的话,那么性能将会更好。那为什么在这种情况下使用JOIN会更有效率呢。因为MySQL不需要在内存中创建临时表来完成这个逻辑上需要两个步骤的查询工作。
优化OR条件
对于含有OR的查询子句,如果要利用索引,则OR之间的每个条件列都必须用到索引;如果没有索引,则考虑增加索引。
使用SQL提示
SQL 提示(SQL HINT)是MySQL数据库优化数据库的一个重要手段,简单来说就是在SQL语句中加入一些人为的提示来达到优化的操作的目的。
例如:
引用
<ol class="dp-xml"><li class="alt"><span><span>SELECT SQL_BUFFER_RESULTS * FROM ... </span></span></li></ol>
这个语句将强制MySQL生成一个临时结果集。只要临时结果集生成后,所有表上的锁定均被释放。这能在遇到表锁定问题时或者要花很长时间将结果传给客户端时所帮助,因为可以尽快释放锁资源,
下面是一些在MySQL中常用的SQL提示。
引用
1. USE INDEX
在查询语句中表名的后面,添加USE INDEX 来提供希望MySQL去参考的索引列表,就可以让MySQL不再考虑其他可用的索引。
引用
<ol class="dp-xml"><li class="alt"><span><span>explain select * from sales2 use index (ind_sales2_id) where id 3 \G; </span></span></li></ol>
2. IGNORE INDEX
如果用户只是单纯地想让MySQL忽略一个或者多个索引,则可以使用IGNORE INDEX 作为HINT
3. FORCE INDEX
为强制MySQL使用一个特定的索引,可在查询中使用FORCE INDEX作为HINT。例如当不强制使用索引的时候,因为id的值都是大于0的,因为MySQL会默认进行全表扫描,而不使用索引。例如:
引用
<ol class="dp-xml"><li class="alt"><span><span>expalin select * from sales2 where id </span><span class="tag">></span><span> 0 \G; </span></span></li></ol>
但是,当使用FORCE INDEX进行提示时,即便使用索引的效率不是很高,MySQL还是选择使用了索引,这是MySQL留给用户的一个自行选择执行计划的权利。加入FORCE INDEX提示后在执行上面的SQL
引用
<ol class="dp-xml"><li class="alt"><span><span>explain select * from sales2 force index(index_sales2_id) where id </span><span class="tag">></span><span> 0 \G; </span></span></li></ol>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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

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.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

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.
