优化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

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











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.

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.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.
