MySQL story - query performance optimization

Query performance optimization


1. Query optimizer hints (hint)

The HIGH_PRIORITY and LOW_PRIORITY
hints tell MySQL which statements have relatively higher priority and which are relatively lower when multiple statements access a table at the same time.
DELAYED
is valid for INSERT and REPLACE. MySQL will immediately return the statement using this hint to the client, put the inserted row data into the buffer, and then write the data in batches when the table is idle.
The STRAIGHT_JOIN
hint can be placed after the SELECT keyword or between the names of any two related tables. The first usage is to have all tables in a query related in the order they appear in the statement. The second usage is to fix the association order of the two tables before and after it.
SQL_SMALL_RESULT and SQL_BIG_RESULT
are only valid for select statements. They tell the optimizer how to use temporary tables and sorting for group by or distinct queries.
The SQL_BUFFER_RESULT
hint tells the optimizer to put the query results into a temporary table and then release the table lock as quickly as possible.
SQL_CACHE and SQL_NO_CACHE
tell MySQL whether this result set should be cached in the query cache.
SQL_CALC_FOUND_ROWS
will make the result set returned by MySQL contain more information.
FOR UPDATE and LOCK IN SHARE MODE
use this prompt to add row locks to the data that meets the query conditions.
USE INDEX, IGNORE INDEX and FORCE INDEX
These hints will tell the optimizer which indexes to use or not to use to query records (for example, which index to use when determining the order of association.) The optimizer_search_depth parameter
controls
the limit of the optimizer when exhaustively executing the plan.
The optimizer_prune_level
parameter is turned on by default, which allows the optimizer to decide whether to skip certain execution plans based on the number of rows that need to be scanned.
The optimizer_switch
variable contains some flags that enable/disable optimizer features.

2. Optimize specific types of queries

Optimize count() query
count() can count the value of a certain column or the number of rows.
When counting column values, the column is required to be non-null (NULL is not counted). count(*) counts the number of rows.
SELECT
COUNT(color = 'blue' OR NULL) AS blue,
COUNT(color = 'red' OR NULL) AS red
FROM
items

SELECT
sum(IF(color = ‘blue’, 1, 0)) AS blue,
sum(IF(color = ‘red’, 1, 0)) AS red
FROM
items

SELECT
sum(color = ‘blue’) AS blue,
sum(color = ‘red’) AS red
FROM
items

Optimize limit
paging. When the offset is very large, for example, LIMIT 10000,20 may need to query 10020 results, and then return 20 results.
You can use bookmarks to record the location of the last query, and then the next query will start scanning from the bookmark position.
select * from user where id>10000 limit 10000,20

Use user-defined variables
to define user-defined variables: SET @one :=1;
SET@last_week :=CURRENT_DATE-INTERVAL 1 WEEK;
However, you need to pay attention when writing SQL statements and using user-defined variables. When assigning and reading variables Variables may be in different stages of the query, so it is best to write the assignment and reading of variables together, or to understand the execution process of the SQL statement.


Guess you like

Origin blog.csdn.net/weixin_45841848/article/details/132704190