mysql optimization knowledge remarks

1. Batch insert data .

If 100,000 pieces of data need to be inserted, then 100,000 insert statements are required, each of which needs to be submitted to the relational engine for analysis and optimization, and then it can reach the storage engine to do the actual insertion work. The above-mentioned simultaneous insertion of multiple entries is an optimization.

2.truncate table

   It is faster than delete, but mysql log is not recorded after truncate is deleted, and data cannot be recovered.

  If there is no foreign key association, innodb executes truncate to drop the table (original table) first, and then create an empty table like the original table, which is much faster than delete to delete row records one by one.

  If the table has a foreign key association, truncate table will report an error. If the foreign key specifies a delete cascade, the child table associated with delete will also delete all table data.

  If the innodb_file_per_table parameter is used, the truncate table can reuse the freed hard disk space. In the InnoDB Plugin, the truncate table is automatically recycled. If the InnoDB Plugin is not used, you need to use the optimize table to optimize the table and free up space.

  After truncate table deletes the table, optimize table is especially important, especially for big data databases, the table space can be released!

  TRUNCATE TABLE is functionally identical to the DELETE statement without the WHERE clause: both delete all rows in the table. But TRUNCATE TABLE is faster than DELETE and uses less system and transaction log resources.

  The DELETE statement deletes one row at a time and records an entry in the transaction log for each row deleted. TRUNCATE TABLE deletes data by freeing the data pages used to store the table data, and only records the freeing of pages in the transaction log.

  TRUNCATE TABLE deletes all rows in the table, but leaves the table structure and its columns, constraints, indexes, etc. unchanged. The count value used for new row identification is reset to the seed for this column. If you want to preserve the identity count value, use DELETE instead.

  If you want to drop the table definition and its data, use the DROP TABLE statement.

auto_increment counter resets to 0 after truncate table

3. Be very careful with a large DELETE or INSERT operation , because these two operations will lock the table. Once the table is locked, other operations will not be able to enter. Therefore, we have to leave it to the DBA to split and reorganize the database strategy, such as limiting the processing of 1000 records.

The official MySQL manual knows that the speed of deleting data is proportional to the number of indexes created. Therefore, in a very large database, it is very important to handle the index relationship when deleting. Recommended compromise method: delete these indexes before deleting data, then delete the useless data, and re-create the index after deletion.

4. Update multiple records 

Update score  

  SET change_type = CASE id  

    WHEN 1 THEN 'value1'  

    WHEN 2 THEN 'value2'  

    WHEN 3 THEN 'value3'  

  END  

WHERE id IN (1,2,3)  

5.REPLACE statement If a table has a unique index on a field, when a record is inserted into the table using an existing key value, a primary key conflict error will be thrown. If we want to overwrite the original record value with the new record value, we can use the REPLACE statement.

When using REPLACE to insert records, if the records are not repeated (or insert new records into the table), the REPLACE function is the same as INSERT. If there are duplicate records, REPLACE will use the new record value to replace the original record value.

The biggest advantage of using REPLACE is that you can combine DELETE and INSERT into one atomic operation. This eliminates the need to consider complex operations such as adding transactions when using DELETE and INSERT at the same time.

When using REPLACE, there must be only one PRIMARY KEY or UNIQUE index in the table, otherwise, it is meaningless to use a REPLACE statement.

UPDATE does nothing when there are no matching records, while REPLACE updates when there are duplicate records and inserts when there are no duplicate records.

UPDATE can optionally update some fields of a record. When REPLACE finds a duplicate record, it completely deletes the record and inserts a new record. That is, all fields are updated.

References: MySQL Optimization Series (1)--Basic Operations of Libraries and Tables and Data Addition, Deletion and Modification

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325377712&siteId=291194637
Recommended