search
HomeDatabaseSQLHow to optimize SQL queries? (detailed explanation)

How to optimize SQL queries? (detailed explanation)

Why should we optimize

The throughput bottleneck of the system often appears in the access speed of the database, that is, as the As the application runs, there will be more and more data in the database, and the processing time will slow down accordingly. Moreover, the data is stored on the disk, and the read and write speed cannot be compared with that of the memory.

How Optimization

1. When designing the database: design of database tables and fields, storage engine

2. Make good use of the functions provided by MySQL itself, such as indexes and optimization of statement writing

3. MySQL cluster, sub-database and sub-table, read-write separation

The Internet has a lot of experience in optimizing SQL statements, so this article puts aside these and tries to optimize the DAO layer. and database design optimization, and list two simple examples

Example 1:ERPQuery optimization

Current situation analysis:

1. Lack of associated indexes
2. Mysql itself has limited performance and does not support the association of multiple tables. The current performance is mainly focused on list queries, which are associated with many tables

Countermeasures:

1 Add necessary indexes: View the execution records through explain, and add indexes according to the execution plan;
2 First count the primary keys of the main table of business data, obtain a smaller result set, and then Then use the result set association query;
1) First query and display the primary key of the business data based on the main table and conditions
2) Use the primary key as the query condition, and then associate other related tables to query the required business fields
3 ) When querying the main table, for query conditions that need to be associated with other tables, you need to set up table association only when this condition is set

例如 有如下表 TT_A   TT_B    TT_C  TT_D

假设未优化前的SQL是这样的

SELECT
    A.ID,
    ....
    B.NAME,
    .....
    C.AGE,
    ....
    D.SEX
    .....

FROM  TT_A A
LEFT JOIN TT_B B ON A.ID  = B.ITEM_ID
LEFT JOIN TT_C C ON B.ID = C.ITEM_ID
LEFT JOIN TT_D D ON C.ID = D.ITEM_ID
WHERE 1=1AND A.XX = ?AND A.VV = ?.....

那么优化后的SQL是

第一步

SELECT
    A.ID

FROM  TT_A A
WHERE 1=1AND A.XX = ?AND A.VV = ?第二步

SELECT
    A.ID,
    ....
    B.NAME,
    .....
    C.AGE,
    ....
    D.SEX
    .....
FROM  ( SELECT A.ID,..... FROM  TT_A  WHERE ID IN (1,2,3..)  ) A
LEFT JOIN TT_B B ON A.ID  = B.ITEM_ID
LEFT JOIN TT_C C ON B.ID = C.ITEM_ID
LEFT JOIN TT_D D ON C.ID = D.ITEM_ID
WHERE 1=1AND A.XX = ?AND A.VV = ?

Summary:

This kind of optimization is suitable for list queries, because the conditions of a list query are generally linked to the main table, so use this to establish key field indexes, and at the same time greatly reduce the main table through the restriction of query conditions. amount of data. In this way, it will be much faster when correlating other tables.

Example 2: Article search optimization

Suppose you want to create an article search function for Tieba. The simplest and most direct way is The storage structure is to use a relational database to create such a relational database table TT_ARTICLES for storing articles:

Then, if the current search keyword is "target", we can Use string matching to perform matching queries on the CONTENT column:

select * from ARTICLES where CONTENT like '% 目标 %';

This easily implements the search function. However, this approach has obvious problems, that is, using % for string matching is very inefficient, so such a query needs to traverse the entire table (full table scan). When there are a few or dozens of articles, this is not a problem, but if there are hundreds of thousands or millions of articles, this method is completely unfeasible. Not to mention that a single relational database table cannot accommodate such large data. Even if it can accommodate it, it needs to be scanned again. The time cost here is unimaginable

So, we have to introduce "inversion" "indexing" technology. In the scenario described above, we can split this concept into two parts to explain: Okay, the ARTICLES table above still exists, but now a keyword table KEYWORDS needs to be added, and the KEYWORD column needs to be indexed. Therefore, the record of this keyword can be quickly found:

Of course, we also need a relationship table to combine the KEYWORDS table and the ARTICLES table, KEYWORD_ID and ARTICLE_ID as unions Primary key

#You see, this is actually a many-to-many relationship, that is, the same keyword can appear in multiple articles, and one article can contain multiple different keywords. In this way, we can first find the corresponding KEYWORD_ID from the KEYWARDS table based on the indexed keywords, then find the ARTICLE_ID based on its association table above, and then use it to find the corresponding article in the ARTICLES table.

Summary:

This seems to be three searches, but because the index is used each time, a full table scan is eliminated. When the amount of data is small, The speed is not slow, and when implemented using SQL, this process can be completely put into a SQL statement. When the amount of data is small, the above method is good enough. This solves the performance problems caused by full table scans and string % match queries.

Summary:

During the technical interview, if you can give practical examples, or directly talk about the problems and gains in your development process, the interview branch will be Add a lot, and your answers should be more logical. Don't go here and there, which can easily confuse yourself. For example, when asked about how to optimize SQL, you should not directly answer adding indexes. You can answer like this:

Hello interviewer, first of all, our project DB data volume has encountered a bottleneck, resulting in list query being very slow and giving users a poor experience. In order to solve this problem, there are many methods, such as the most basic database table design, Basic SQL optimization, MYSQL clustering, read-write separation, sub-database and sub-table, adding a cache layer to the architecture, etc. Their advantages and disadvantages... We combine these and then combine them with the characteristics of our project. Finally, we choose when selecting technology. Who.

If you answer the questions in such an orderly and well-reasoned way and also talk about so many knowledge points outside the questions, the interviewer will think that you are not just a person who can write code, but that you have clear logic. You have your own understanding and thinking about technology selection

This article comes from the SQL Tutorial column, welcome to learn!

The above is the detailed content of How to optimize SQL queries? (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:cnblogs. If there is any infringement, please contact admin@php.cn delete
Getting Started with SQL: Essential Concepts and SkillsGetting Started with SQL: Essential Concepts and SkillsApr 22, 2025 am 12:01 AM

SQL is a language used to manage and operate relational databases. 1. Create a table: Use CREATETABLE statements, such as CREATETABLEusers(idINTPRIMARYKEY, nameVARCHAR(100), emailVARCHAR(100)); 2. Insert, update, and delete data: Use INSERTINTO, UPDATE, DELETE statements, such as INSERTINTOusers(id, name, email)VALUES(1,'JohnDoe','john@example.com'); 3. Query data: Use SELECT statements, such as SELEC

SQL: The Language, MySQL: The Database Management SystemSQL: The Language, MySQL: The Database Management SystemApr 21, 2025 am 12:05 AM

The relationship between SQL and MySQL is: SQL is a language used to manage and operate databases, while MySQL is a database management system that supports SQL. 1.SQL allows CRUD operations and advanced queries of data. 2.MySQL provides indexing, transactions and locking mechanisms to improve performance and security. 3. Optimizing MySQL performance requires attention to query optimization, database design and monitoring and maintenance.

What SQL Does: Managing and Manipulating DataWhat SQL Does: Managing and Manipulating DataApr 20, 2025 am 12:02 AM

SQL is used for database management and data operations, and its core functions include CRUD operations, complex queries and optimization strategies. 1) CRUD operation: Use INSERTINTO to create data, SELECT reads data, UPDATE updates data, and DELETE deletes data. 2) Complex query: Process complex data through GROUPBY and HAVING clauses. 3) Optimization strategy: Use indexes, avoid full table scanning, optimize JOIN operations and paging queries to improve performance.

SQL: A Beginner-Friendly Approach to Data Management?SQL: A Beginner-Friendly Approach to Data Management?Apr 19, 2025 am 12:12 AM

SQL is suitable for beginners because it is simple in syntax, powerful in function, and widely used in database systems. 1.SQL is used to manage relational databases and organize data through tables. 2. Basic operations include creating, inserting, querying, updating and deleting data. 3. Advanced usage such as JOIN, subquery and window functions enhance data analysis capabilities. 4. Common errors include syntax, logic and performance issues, which can be solved through inspection and optimization. 5. Performance optimization suggestions include using indexes, avoiding SELECT*, using EXPLAIN to analyze queries, normalizing databases, and improving code readability.

SQL in Action: Real-World Examples and Use CasesSQL in Action: Real-World Examples and Use CasesApr 18, 2025 am 12:13 AM

In practical applications, SQL is mainly used for data query and analysis, data integration and reporting, data cleaning and preprocessing, advanced usage and optimization, as well as handling complex queries and avoiding common errors. 1) Data query and analysis can be used to find the most sales product; 2) Data integration and reporting generate customer purchase reports through JOIN operations; 3) Data cleaning and preprocessing can delete abnormal age records; 4) Advanced usage and optimization include using window functions and creating indexes; 5) CTE and JOIN can be used to handle complex queries to avoid common errors such as SQL injection.

SQL and MySQL: Understanding the Core DifferencesSQL and MySQL: Understanding the Core DifferencesApr 17, 2025 am 12:03 AM

SQL is a standard language for managing relational databases, while MySQL is a specific database management system. SQL provides a unified syntax and is suitable for a variety of databases; MySQL is lightweight and open source, with stable performance but has bottlenecks in big data processing.

SQL: The Learning Curve for BeginnersSQL: The Learning Curve for BeginnersApr 16, 2025 am 12:11 AM

The SQL learning curve is steep, but it can be mastered through practice and understanding the core concepts. 1. Basic operations include SELECT, INSERT, UPDATE, DELETE. 2. Query execution is divided into three steps: analysis, optimization and execution. 3. Basic usage is such as querying employee information, and advanced usage is such as using JOIN connection table. 4. Common errors include not using alias and SQL injection, and parameterized query is required to prevent it. 5. Performance optimization is achieved by selecting necessary columns and maintaining code readability.

SQL: The Commands, MySQL: The EngineSQL: The Commands, MySQL: The EngineApr 15, 2025 am 12:04 AM

SQL commands are divided into five categories in MySQL: DQL, DDL, DML, DCL and TCL, and are used to define, operate and control database data. MySQL processes SQL commands through lexical analysis, syntax analysis, optimization and execution, and uses index and query optimizers to improve performance. Examples of usage include SELECT for data queries and JOIN for multi-table operations. Common errors include syntax, logic, and performance issues, and optimization strategies include using indexes, optimizing queries, and choosing the right storage engine.

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

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.