优化MySQL数据库中的查询语句详解
很多时候基于php+MySQL建立的网站所出现的系统性能瓶颈往往是出在MySQL上,而MySQL中用的最多的语句就是查询语句,因此,针对MySQL数据库查询语句的优化就显得至关重要!本文就此问题做出详细分析如下: 1、判断是否向MySQL数据库请求了不需要的数据,如下列
很多时候基于php+MySQL建立的网站所出现的系统性能瓶颈往往是出在MySQL上,而MySQL中用的最多的语句就是查询语句,因此,针对MySQL数据库查询语句的优化就显得至关重要!本文就此问题做出详细分析如下:
1、判断是否向MySQL数据库请求了不需要的数据,如下列情况:
(1)、查询不需要的数据,例如你需要10条数据,但是你选出了100条数据加了limit做限制。
(2)、多表关联时返回全部列
(3)、总是取出全部列select*......取出全部列,会让优化器无法完成索引覆盖扫描这类优化,还为服务器带来额外的I/O、内存、和cpu的消耗
(4)、重复查询相同的数据例如,在用户评论的地方需要查询用户的头像的URL,那么用户多次评论的时候将这个数据缓存起来,需要的时候从缓存取出,这样性能会更好。
2、mysql是否在扫描额外的记录
最简单衡量查询开销的三个指标如下:响应时间、扫描的行数、返回的行数
响应时间:服务时间和排队时间。服务时间是指数据库处理这个查询真正花费的时间。排队时间是指服务器因为等待某些资源而没有真正执行的查询。
扫描的行数和返回的行数:理想情况下扫描的行数和返回的行数应该是相同的。
一般MYSQL能够使用如下三种方式应用where条件记录,从好到坏依次为:
(1)、在索引中使用where条件来过滤不匹配的记录,在存储索引层完成。
(2)、使用索引覆盖扫描来返回记录,直接从索引中过滤不需要的记录并返回命中的结果,在mysql服务器层完成,但无需在回表查询记录。
(3)、从数据表中返回数据,然后过滤不满足条件的记录,在mysql服务器层完成,需要先从数据表读出记录然后过滤
如果发现查询需要扫描大量的数据但返回少数的行,那么通常可以尝试下面的技巧:
(1)、使用索引覆盖扫描,把所有需要的列都放到索引中,这样存储引擎无须返回表获取对应行就可以返回结果了。
(2)、改变库表结构,使用单独的汇总表。
(3)、重写这个复杂的查询
3、重构查询的方式
(1)、一个复杂查询还是多个简单查询:
Mysql内部每秒能够扫描内存中上百万条数据,相比之下,mysql响应数据给客户端就慢得多,在其他条件都相同的时候,使用尽可能少的查询当然是好的,但有时候将一个大查询分解为多个小查询都是很有必要的。
(2)、切分查询:
删除旧数据是一个很好的例子,在定期清除大量数据时,如果用一个大的语句一次性完成的话,则可能一次锁住很多数据,占满整个事物日志。耗尽系统资源,阻塞很多小的但很重要的查询。
Mysql>deletefrommessageswherecreated<DATE_SUB(NOW(),INTERVAL3MONTH);
改写:
Rows_affected=0; Do{ Rows_affected=do_query( “deletefrommessageswherecreated<DATE_SUB(NOW(),INTERVAL3MONTH)”; ) }
(3)、分解关联查询:
可以让缓存的效率更高,在应用程序中可以方便的缓存单条数据
就查询分解后,执行单个查询可以减少锁的竞争
在应用层做关联,可以更容易对数据库进行拆分,更容易做到高性能和高扩展
查询本身效率也会更高。
可以减少冗余数据的查询,在应用层做关联查询,意味着对于某条数据应用只需要查询一次,而在数据库中做查询,可能需要重复的访问一部分数据。
适合场景:
①当应用程序能够方便的缓存单个查询结果的时候;
②当可以将数据分布到不同的mysql服务器上的时候;
③当能够使用IN()的方式代替关联查询的时候;
④当查询中使用一个数据表的时候。

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

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.

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.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

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

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

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.

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

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
