Summary of ten principles for optimizing basic statements in MySQL

Summary of ten principles for optimizing basic statements in MySQL

Preface

In the application of database, programmers have summed up a lot of experience through continuous practice. These experiences are some generally applicable rules. Every programmer should understand and remember them. When constructing SQL, develop good habits. Let's take a look at the detailed introduction without further ado:

MySQL basic statement optimization principles

1. Avoid operations on columns as much as possible, as this will cause index failure

select * from t where YEAR(d) >= 2011;

Optimized for

select * from t where d >='2011-0101'

2. When using JOIN, you should use a small result set to drive a large result set, and split complex JOIN queries into multiple queries, because JOINing multiple tables may cause more locking and blocking

3. When using LIKE, avoid using %%

4. Select the specified query field, do not check all of them, save memory

5. Use batch insert statements to save interactions

6. When the cardinality of limit is relatively large, use between. Between is faster than limit, but between also has its flaws. If there is a line break in the middle of the id or the middle part of the id is not read, the data will be less.

select * from t where 1 limit 100000,10

Optimized for

select * from t where id between 100000 and 100010

7. Do not use the rand function to fetch multiple random records

8. Avoid using NULL

9. Don’t use count(id) , use count(*) instead

10. Don’t do unnecessary sorting operations, but try to complete the sorting in the index as much as possible

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of MYSQL configuration parameter optimization
  • Mysql query the most recent record of the sql statement (optimization)
  • Detailed explanation of 30 SQL query optimization techniques for MySQL tens of millions of large data
  • Mysql optimization techniques for querying dates based on time
  • 10 SQL statement optimization techniques to improve MYSQL query efficiency
  • MySQL million-level data paging query optimization solution
  • Optimizing the performance of paging query for MySQL with tens of millions of data
  • MySQL Optimization Summary - Total Number of Query Entries
  • MYSQL development performance research: optimization method for batch inserting data
  • MySQL optimization strategy (recommended)

<<:  Linux kernel device driver system call notes

>>:  A brief discussion on several ways to pass parameters in react routing

Recommend

How to use limit_req_zone in Nginx to limit the access to the same IP

Nginx can use the limit_req_zone directive of the...

Tutorial on installing mysql8 on linux centos7

1. RPM version installation Check if there are ot...

Convert XHTML CSS pages to printer pages

In the past, creating a printer-friendly version ...

Install Percona Server+MySQL on CentOS 7

1. Environmental Description (1) CentOS-7-x86_64,...

Angular Cookie read and write operation code

Angular Cookie read and write operations, the cod...

MySQL 8.0 New Features: Hash Join

The MySQL development team officially released th...

Detailed explanation of MySQL syntax, special symbols and regular expressions

Mysql commonly used display commands 1. Display t...

What you need to know about creating MySQL indexes

Table of contents Preface: 1. Create index method...

What are the image file formats and how to choose

1. Which three formats? They are: gif, jpg, and pn...

MySQL data type selection principles

Table of contents Small but beautiful Keep it sim...

How to use JS code compiler Monaco

Preface My needs are syntax highlighting, functio...

In-depth understanding of CSS @font-face performance optimization

This article mainly introduces common strategies ...

Vue custom directive details

Table of contents 1. Background 2. Local custom i...