Database import and export data, slow query, table locked solution

1. Import and export data

1. Connect to mysql from the command line and import it

mysql -h 192.168.0.1 -u user1 -p   enter

password1

MySQL> show databases;

MySQL> use database1;

MySQL> show tables;


MySQL> source  c:/test.sql  #文件导入数据库database1中对应的表格



说明1: -u, 用户名

说明2: -h,指明IP或域名

说明3: -P(大写),指明端口 

说明4: -p(小写),即密码

Data output

mysqldump

parameter name abbreviation meaning
--host -h Server IP address
--port -P Server port number
--user -u MySQL username
--pasword -p MySQL password
--databases Specify the database to backup
--all-databases Back up all databases on mysql server
--compact Compressed mode, producing less output
--comments Add annotation information
--complete-insert Output the completed insert statement
--lock-tables Before backing up, lock all database tables
--no-create-db/--no-create-info Disable generation of create database statements
--force Continue backup operations when errors occur
--default-character-set Specify default character set
--add-locks

Lock database table when backing up database table

-- tables  table1  table2

example:

1. Export all data from the two databases db1 and db2
mysqldump -uroot -proot --databases db1 db2 >/tmp/user.sql

2. Export the a1 and a2 tables in db1.
Note:
The specified export table can only be exported for one database, and the exported content is different from the exported database. There is no judgment statement for creating a database in the export text of the exported specified table, only Delete table - create table - import data.
mysqldump -uroot -proot --databases db1 --tables a1 a2 >/tmp/db1.sql

3. Conditional export, export the data with id=1 in db1 table a1.
If the conditions of multiple tables are the same, multiple tables can be exported at one time.
• The field is an integer
mysqldump -uroot -proot --databases db1 --tables a1 --where='id=1' >/tmp/a1.sql
• The field is a string, and the exported sql does not contain drop table, create table
mysqldump -uroot -proot --no-create-info --databases db1 --tables a1 --where="id='a'" >/tmp/a1.sql

2. Slow query

2.1 Crawl slow queries

To optimize slow SQL statements, you can generally follow the following steps:

1. Turn on the slow query log, set the slow SQL statement to exceed a few seconds, and capture the slow SQL statement;

2. Check the execution plan through explain and analyze slow SQL statements;

3. Create an index and adjust the statement, then check the execution plan and compare the tuning results.

Parameter slow_query_log: Indicates whether to enable slow query log. The statement "set global slow_query_log=on" temporarily turns on the slow query log. If you want to turn off the slow query log, you only need to execute "set global slow_query_log=off".

  • Parameter slow_query_log_file: When using a file to store slow query logs (when log_output is set to "FILE" or "FILE, TABLE"), specify in which log file the slow query log is stored. The default slow query log file name is "host name - slow.log", the location of the slow query log is the directory location corresponding to the datadir parameter. Also, after MySQL 5.7.2, if
  • If the slow log is written to a file, you need to set log_timestamps (the default is UTC time, which is 8 hours later than us, and needs to be set to the system time log_timestamps=SYSTEM) to control the time zone written to the slow log file (this parameter also Affects general log and error log).
  • Parameter long_query_time: Indicates "how long a query takes" to be considered a "slow query". The default value is 10 seconds, indicating that a query exceeding 10 seconds is considered a slow query. The statement "set long_query_time=5" means that from now on all SQL statements that take more than 1 second to execute will be recorded in the slow query file.
  • Parameter log_queries_not_using_indexes: Indicates whether the running SQL statement will be recorded in the slow query log as a slow query statement if it does not use indexes. OFF means not recording, ON means recording.
  • Parameter log_throttle_queries_not_using_indexes: When log_queries_not_using_indexes is set to ON, query statements that do not use indexes will also be recorded in the slow query log as slow query statements. Use log_throttle_queries_not_using_indexes to limit the number of times such statements are recorded in the slow query log per minute, because in a production environment there may be many statements that do not use indexes. Such statements are frequently recorded in the slow query log, which may cause slowdowns. The query log grows rapidly and continuously, which the administrator can control through this parameter.

  The slow query log gives information such as account number, host, running time, lock time, returned rows, etc., and then uses this information to analyze what went wrong with this SQL statement. After you start using the slow query function, the slow query log may become larger and larger, and the slow query log cannot be viewed intuitively through the vi or cat command. In this case, you can use MySQL's built-in mysqldumpslow command for analysis.

2.2 Use explain to analyze query statements

To use explain, just add the word explain before the select keyword in the query. MySQL will set a flag on the query and return information about each step in the execution plan when the query is executed instead of executing it.

1) id: reflects the reading order of the table or the order in which select clauses are executed in the query.

① If the ID is the same, the execution order is from top to bottom.

② The IDs are different. If it is a subquery, the ID sequence number will be incremented. The larger the ID value, the higher the priority and will be executed first.

③ If the id is the same, it can be considered as a group and executed sequentially from top to bottom; among all groups, the larger the id value, the higher the priority and the earlier it is executed.

(2) select_type: Indicates the type of select, mainly used to distinguish complex queries such as ordinary queries, joint queries, subqueries, etc.

① Simple: A simple select query. The query does not contain subqueries or unions.

② primary: If the query contains any complex subparts, the outermost query is marked as primary.

③ subquery: subquery in select or where list.

④ Derived: For the subqueries contained in the from list, MySQL will recursively execute these subqueries and put the results in a temporary table.

⑤ Union: If the second select appears after union, it will be marked as union; if union is included in the subquery of the from clause, the outer select will be marked as derived.

⑥ Union result: the result set after union.

(3) table: Displays the name of the table in the database accessed in this step (displays which table the data in this row refers to). Sometimes it is not the real table name, but may be the abbreviation of the result of the execution of several steps.

(4) type: The access method to the table, indicating the way MySQL finds the required rows in the table, also known as the "access type". Common access types include ALL, index, range, ref, eq_ref, const, system, and NULL (from left to right, performance from worst to best).

① ALL: Full Table Scan, MySQL will traverse the entire table to find matching rows.

② index:: Full Index Scan, the difference between index and ALL is that the index type only traverses the index tree.

③ Range: Index range scan, returns a batch of rows that only retrieve a given range, and uses an index to select rows. Generally, queries such as between, <, >, in, etc. appear in the where statement. This range scan index is better than a full table scan because it only needs to start at a certain point in the index and end at another point, without scanning the entire index.

④ ref: Non-unique index scan, returns all rows that match a single value. It is essentially an index access. It returns all rows that match a single value. However, it may find multiple rows that meet the conditions. So it should be a mixture of search and scan.

⑤ eq_ref: Similar to ref, the difference is that the index used is a unique index. For each index key, only one record in the table matches it, which is common in primary key or unique index scans. To put it simply, primary key or unique key is used as the association condition in multi-table connections.

⑥ const, system: Use these types to access when MySQL optimizes a certain part of the query and converts it to a constant. If the query condition uses constants, they can be found once through the index, often appearing in indexes using primary key or unique. System is a special case of const type, used when the queried table has only one row.

⑦ NULL: MySQL decomposes the statement during the optimization process, and does not even need to access the table or index during execution. For example, selecting the minimum value from an index column can be completed through a separate index lookup.

(5) possible_keys: Indicates which index MySQL can use to find rows in the table. If there is an index on the field involved in the query, the index will be listed, but it will not necessarily be used by the query.

(6) key: Displays the index that MySQL actually decides to use. If no index is selected, the display is NULL. To force MySQL to use or ignore the index on the possible_keys column, use FORCE INDEX or IGNORE INDEX in the query. If a covering index is used in the query (the field to be queried after selecting is exactly the same as the created index field), the index will only appear in the key list.

(7) key_len: Displays the number of bytes used in the index.

(8) ref: Indicates the connection matching conditions of the above table, that is, which columns or constants are used to find the value on the index column.

(9) rows: Displays MySQL's estimate of the number of rows to be read to find the required records based on table statistics and index selection.

(10) Extra: This column contains instructions and descriptions of how MySQL solves the query, including extra information that is not suitable for display in other columns but is very important to the execution plan.

① Using where: Without reading all the information in the table, the required data can be obtained only through the index. This occurs when all requested columns of the table are in the same index part, which means that the MySQL server will retrieve the rows after the storage engine. to filter.

② Using temporary: Indicates that MySQL needs to use a temporary table to store the result set. MySQL uses temporary tables when sorting query results, which is common in sorting (order by) and group query (group by).

③ Using filesort: When the Query contains an order by operation and the sorting operation cannot be completed using the index, it is called "file sorting". When creating the index, the data will be sorted first. Using filesort occurs generally because the conditions after order by cause the index to fail. , it is best to optimize.

④ Using join buffer: Indicates that the connection cache is used. For example, when querying, the number of multi-table joins is very large, so increase the join buffer in the configuration file. If this value appears, it should be noted that depending on the specific circumstances of the query, it may be necessary to add an index to improve it.

⑤ Using index: Only use the information in the index tree without further searching to read the actual rows to retrieve column information in the table. A covering index is used in the corresponding select operation to avoid accessing the data rows of the table, which is very efficient. Covering index: The selected data columns can only be obtained from the index without reading the data rows, and are consistent with the number of indexes created (the query column is less than or equal to the number of indexes) and the order. If you want to use a covering index, you should pay attention to select columns that only need to be used, and do not use select *. At the same time, if all fields are indexed together, the index file will be too large and performance will decrease.

⑥ Using Index Condition: Indicates that ICP optimization has been performed.

To summarize, generate an execution plan for the explain command: first focus on the query type type column. If the all keyword appears, it means a full table scan, and no index is used; look at the key column again. If the key column is NULL, it means no index is used; then Look at the rows column. The larger the value in this column, the more rows need to be scanned and the longer the response time. Finally, look at the Extra column and avoid words like Using filesort or Using temporary, which will greatly affect performance.

3. Database table is locked.

show processlist

Displaying wait for... lock information in the state of the process means that the current process is waiting for the lock.

implement

SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX;
  • 1

Get multiple pieces of data:
trx_mysql_thread_id

Execute the command kill corresponding thread

kill 

 reference:

mysql slow query optimization - Bai Yunying - Blog Park

Guess you like

Origin blog.csdn.net/qq_37674086/article/details/126192127