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

AI Hentai Generator
Generate AI Hentai for free.

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



Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.
