MySQL 8 transaction management, database maintenance, improve performance

1. Transaction

Not all engines support transactions. MyISAM and InnoDB engine are two most commonly used, the former does not support explicit transaction management, and support the latter.

Transactions can be used to maintain the integrity of the database, which ensures that the bulk of the MySQL operations either fully executed or not executed at all.

Transaction transaction: refers to a set of SQL statements;

Rollback rollback: SQL statement refers to revoke the designation process;

Submit commit: refers to the result of the SQL statement is not stored into the database table;

Retention point savepoint: means temporary placeholders provided in the transaction, it may back-off step (with different back the entire transaction process).

 

Control Transaction:

MySQL use the following statement to identify the transaction begins:

  START TRANSACTION

Use ROLLBACK:

MySQL's ROLLBACK command rollback (undo) MySQL statement.

  SELECT * FROM ordertotals;

  START TRANSACTION;

  DELETE FROM ordertotals;

  SELECT * FROM ordertotals;

  ROLLBACK;

  SELECT * FROM ordertotals;

Here is a table to show (nothing to do with affairs). With START TRANSACTION to start a transaction. ROLLBACK to roll back the transaction by the middle of the SQL statement is a set of SQL commands of the transaction.

ROLLBACK only be used within a transaction (after the execution of a START TRANSACTION command).

Which transactions can be rolled back? Transaction management for INSERT, UPDATE, and DELETE statements, a SELECT statement can not be rolled back, you can not roll back CREATE or DROP operation.

 

Use COMMIT:

General statements are MySQL database table for direct execution and prepared. This is called implicit submission that the author (writing or save) operation is automatic.

In a transaction block, filed will not be performed implicitly. For clear submitted using the COMMIT statement:

  START TRANSACTION;

  DELETE FROM orderitems WHERE order_num = 20010;

  DELETE FROM orders WHERE order_num = 20010;

  COMMIT;

In this example, two DELETE statements generally do not want to submit SQL statements directly to the same, but only in order to submit after the COMMIT. If there is an error SQL statement transaction commit, the entire transaction is not committed (automatically revoked).

 

Implicit transaction closed: When the COMMIT or ROLLBACK statement is executed, the transaction will automatically turn off (future changes implied submission).

 

 

Using Hold point:

Simple ROLLBACK and COMMIT statements can be written or revoke the entire transaction process. However, to do so only for simple transactions, more complex transaction may require partial committed or rolled back.

 

To support part of the transaction is rolled back, it must be replaced with a placeholder in the transaction processing block in place. This requires a rollback, you can fall back to a placeholder.

These placeholders called retention point. To create a placeholder, use the SAVEPOINT statement as follows:

  SAVEPOINT delete1;

Reservations are taken to identify every point it's only the name, so that when the rollback, MySQL know where to return retreated.

  ROLLBACK TO delete1; // fall back to save some delete1

 

The release of retention points:

  Retention point automatically released after the transaction is completed (execute a ROLLBACK or COMMIT). You can also use RELEASE SAVEPOINT expressly reserved the release point.

 

Change the default commit behavior:

MySQL's default behavior is to automatically submit all changes. That is in addition to the transaction at any time execute a MySQL statement that are actually executed against the table, and the changes take effect immediately.

To indicate that MySQL does not automatically commit the changes, you need to use the following statement:

  SET autocommit=0;

autocommit flag determines whether to automatically commit the changes, whether or not COMMIT statement. Autocommit is set to 0 (false) indicates the MySQL does not automatically submit the changes (until autocommit is set to true so far).

 

autocommit flag is for each connection rather than the server.

 

 

2. Database Maintenance

Backup data:

Like all the same data, MySQL data must be backed up frequently. Because MySQL database is a disk-based file, ordinary and routine backup system will be able to MySQL data backup. However, since these files are always open and in use, an ordinary file backup copy is not always effective.

 

Database backup alternatives:

  (1) using the command-line utility mysqldump to dump all the contents of a database to an external file. Before performing regular backups should run this utility properly in order to properly backup dump file.

  (2) the available command-line utility mysqlcopy copy all the data from a database (not all database engines support this utility).

  (3) You can use the MySQL BACKUP TABLE or SELECT INTO OUTFILE dump all data to an external file. These two statements are accepted by the system file name to be created, the file system must not exist, or an error occurs. Data can be restored with RESTORE TABLE.

 

In order to ensure that all data is written to disk (including index data), you may need to use FLUSH TABLES statement prior to performing the backup.

 

 

Database maintenance:

MySQL provides a series of statements, it can (should) be used to ensure proper and normal operation of the database.

(. 1) the ANALYZE TABLE , the table is used to check whether the correct key. ANALYZE TABLE returns status information as shown below:

  ANALYZE TABLE orders;

(2) CHECK TABLE is used for many of the issues on the table to check . The index also check on MyISAM tables. CHACK TABLE supports a range of ways for MyISAM tables. CHANGED checks since the last inspection altered table. EXTENDED perform the most thorough examination, FAST check not only normal closing table, MEDIUM check all the deleted links and key test, QUICK only a quick scan. The following was found with CHECK TABLE and fix the problem:

  CHECK TABLE orders, orderitems;

(3) If a MyISAM table access produce incorrect and inconsistent results, you may need to use REPAIR TABLE fix the corresponding table. This statement should be used infrequently, if required frequent use may have bigger problems to solve.

(4) If a large amount of data deleted from a table, OPTIMIZE TABLE should be used to recover the space used to optimize the performance of the table.

 

 

Diagnostic startup problems:

Server startup problem typically occurs when making changes in configuration or to MySQL server itself. MySQL reports an error when the problem occurred, but because most of the MySQL server is automatically activated as a system process or service, these messages may not see.

 

mysqld command-line options:

  --help Displays help --------- a list of options.

  --safe-mode loading subtracting certain preferred configuration of the server.

  --verbose display text message present (in order to obtain more detailed help message --help used in combination);

  --version display version information and then exit.

 

 

View the log file:

MySQL to maintain a series of log file administrator dependent. The main log files are as follows.

Error Log: It contains startup and shutdown issues and any critical error details. This log is usually called hostname.err, located in the data directory. This name is available --log-error log command-line options to change.

 

Query Log: It records all activities of MySQL , is very useful when diagnosing problems. This log file can quickly become very large, so it should not be long-term use. This log file typically named hostname.log, located in the data directory. This name can use command-line option --log change.

 

Binary Log: It records the updated data (or possibly updated data) of all statements. This log usually named hostname-bin, located within the data directory. This name can be used --log-bin command-line options to change.

 

Slow query log: This log records to perform any query slow. This log in determining the need to optimize the database where useful. This log usually named hostname-slow.log, located in the data directory. This name can be used --log-slow-queries command-line options to change.

 

When using logs, FLUSH LOGS statement can be used to refresh and restart all the log files.

 

 

 

3. improve performance

(1) First, MySQL (like all DBMS) has specific hardware recommendations.

(2) In general, the key production DBMS should run on its own dedicated server.

(3) MySQL is a series of pre-set default settings, starting with these settings are usually good. But after some time you may need to adjust memory allocation, buffer size. (See currently set, use SHOW VARIABLES; and SHOW STATUS ;.)

(4) a MySQL multi-user multi-threaded the DBMS , which usually perform multiple tasks simultaneously. If one of the slow implementation of these tasks, all requests will be executed slowly. If you experience significant performance bad, you can use SHOW PROCESSLIST Show all active processes (and their thread ID and execution time). You can also use the end of a particular process KILL command (to use this command requires login as administrator).

(5) there is always written in the same SELECT statement is more than one way. We should test links, and sub-query to find out the best way.

(6) Use EXPLAIN statement allows MySQL to explain how it will execute a SELECT statement.

(7) In general, a stored procedure than a MySQL statement executes each block therein.

(8) should always use the correct data type.

(9) Never retrieve the data even more than demand. In other words, do not use SELECT * (unless really needed each column).

(10) Some operations (including INSERT) supports an optional DELAYED keyword, if you use it, will return control to the caller immediately, and once it is possible to actually perform the operation.

(11) when importing data, should be closed automatically committed. You may also want to delete the index (including FULLTEXT index), and then rebuild them after the import is complete.

(12) have an index database table to improve the performance of data retrieval. SELECT statement to be analyzed to determine the index used to find duplicate WHERE and ORDER BY clauses. If a simple WHERE clause to return the results take too long, it can be concluded that the column (or several columns) is subject to index used therein.

(13) SELECT statement you have a complex set of OR conditions do? By using multiple SELECT statements and connects them UNION statement, you can see significant performance improvements.

(14) the index to improve data retrieval performance, but the damage data insert, delete and update performance. If there are some tables, they often are not searched to collect data, do not index them before it is necessary. (Indexes can be added and deleted as required).

(15) LIKE very slow. In general, it is best to use FULLTEXT rather than LIKE.

(16) The database is constantly changing entity. A well optimized table for a while probably unrecognizable. Since the change and use the table of contents, the ideal optimization and configuration will change.

(17) The most important rule is that each rule under certain conditions will be broken.

 

Guess you like

Origin www.cnblogs.com/cjj-ggboy/p/12546367.html