MySQL optimization strategy (recommended)

MySQL optimization strategy (recommended)

In summary:

  1. 1. Consider performance when designing the database and creating tables
  2. 2. SQL writing needs to be optimized
  3. 3. Partition, table, and database

When designing a table:

  1. 1. Avoid null values ​​in fields. Null values ​​are difficult to query and optimize and take up extra index space. It is recommended to use the default number 0 instead of null.
  2. 2. Try to use INT instead of BIGINT. If it is non-negative, add UNSIGNED (this will double the value capacity). Of course, it is better to use TINYINT, SMALLINT, or MEDIUM_INT.
  3. 3. Use enumeration or integer instead of string type
  4. 4. Try to use TIMESTAMP instead of DATETIME
  5. 5. Do not have too many fields in a single table. It is recommended to limit the number of fields to 20.
  6. 6. Use integer to store IP

index:

  1. 1. The more indexes, the better. You need to create indexes based on the query. Consider creating indexes for the columns involved in the WHERE and ORDER BY commands. You can use EXPLAIN to check whether the index is used or the full table scan is performed.
  2. 2. Avoid using NULL values ​​for fields in the WHERE clause, otherwise the engine will abandon the use of indexes and perform a full table scan.
  3. 3. Fields with very sparse value distribution are not suitable for indexing, such as "gender" fields with only two or three values.
  4. 4. Create only prefix index for character fields
  5. 5. It is best not to use character fields as primary keys
  6. 6. No foreign keys are needed, constraints are guaranteed by the program
  7. 7. Try not to use UNIQUE, and let the program ensure the constraints
  8. 8. When using multi-column indexes, keep the order and query conditions consistent and delete unnecessary single-column indexes

Summary: Use the appropriate data type and choose the appropriate index

The writing of sql needs to be optimized:

Use limit to limit the records in the query results

Avoid select * and list the fields you need to search for.

Use joins instead of subqueries

Split large delete or insert statements

You can find out the slow SQL by turning on the slow query log

No column operations: SELECT id WHERE age + 1 = 10. Any operation on the column will result in a table scan, including database tutorial functions, calculation expressions, etc. When querying, try to move the operation to the right of the equal sign.

The SQL statement should be as simple as possible: one SQL statement can only be executed on one CPU; large statements should be split into small statements to reduce the lock time; one large SQL statement can block the entire database

Rewrite OR to IN: OR has an efficiency of n, while IN has an efficiency of log(n). The number of INs is recommended to be controlled within 200.

No functions or triggers are needed to implement

Avoid %xxx-style queries

Use JOIN sparingly

Use the same type for comparison, such as '123' and '123', 123 and 123

Try to avoid using the != or <> operators in the WHERE clause, otherwise the engine will abandon the index and perform a full table scan.

For consecutive values, use BETWEEN instead of IN: SELECT id FROM t WHERE num BETWEEN 1 AND 5

Don't use the entire table for list data. Use LIMIT to split the data into pages. The number of pages should not be too large.

engine:

MyISAM

Row locks are not supported. All tables that need to be read are locked when reading, and exclusive locks are added to the tables when writing.

No support for transactions

Foreign keys are not supported

Does not support safe recovery after a crash

Support inserting new records into the table while the table is being read and queried

Supports indexing of the first 500 characters of BLOB and TEXT, and full-text indexing

Supports delayed update of indexes, greatly improving write performance

For tables that will not be modified, compression is supported to greatly reduce disk space usage

InnoDB

Support row locks and use MVCC to support high concurrency

Support Affairs

Support foreign keys

Supports safe recovery after crash

No full-text indexing support

In general, MyISAM is suitable for SELECT-intensive tables, while InnoDB is suitable for INSERT- and UPDATE-intensive tables.

Partition, table, and database (read-write separation)

The above is a detailed explanation and integration of MySQL optimization strategies introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • How to solve the problem of case insensitivity in MySQL queries
  • Detailed explanation of how MySQL solves phantom reads
  • Detailed explanation of the 10061 unknown error when using Navicat to connect to a remote Linux MySQL database
  • Detailed explanation of MySQL database addition, deletion and modification operations
  • How to add index to mysql using shell script
  • MySQL character set garbled characters and solutions
  • MySQL starts slow SQL and analyzes the causes
  • Will MySQL execute the update statement again if it has the same data as the original one?
  • Reasons and solutions for slow MySQL query stuck in sending data
  • What should I do if I want to cancel an incorrect MySQL command?

<<:  Detailed explanation of Nginx rewrite jump application scenarios

>>:  Vue+express+Socket realizes chat function

Recommend

Three ways to implement text color gradient in CSS

In the process of web front-end development, UI d...

Analysis of MySQL's planned tasks and event scheduling examples

This article uses examples to describe MySQL'...

Linux gzip command compression file implementation principle and code examples

gzip is a command often used in Linux systems to ...

Pitfalls based on MySQL default sorting rules

The default varchar type in MySQL is case insensi...

Websocket+Vuex implements a real-time chat software

Table of contents Preface 1. The effect is as sho...

Solution to overflow of html table

If the table is wide, it may overflow. For exampl...

Detailed explanation of new relational database features in MySQL 8.0

Preface The latest version of MySQL 8.0 is 8.0.4 ...

Example of implementing QR code scanning effects with CSS3

Online Preview https://jsrun.pro/AafKp/ First loo...

The w3c organization gives style recommendations for html4

This is the style recommendation given by the W3C ...

Explanation of the precautions for Mysql master-slave replication

1. Error error connecting to master 'x@xxxx:x...

Detailed explanation of Linux lsof command usage

lsof (list open files) is a tool to view files op...

MYSQL transaction tutorial Yii2.0 merchant withdrawal function

Preface I am a PHP programmer who started out as ...