Mysql slow query lookup and tuning test _mysql

Source: Internet
Author: User
Tags create index
Edit the MY.CNF or My.ini file to remove the comments from the following lines of code:
Copy Code code as follows:

Log_slow_queries =/var/log/mysql/mysql-slow.log
Long_query_time = 2
Log-queries-not-using-indexes

This will allow slow queries and queries that do not use indexes to be recorded.
After doing so, execute the tail-f command on the Mysql-slow.log file, and you will see the slow and unused indexes recorded in the query.
Random extraction of a slow query, the implementation of explain:
Copy Code code as follows:

Explain Low_query

you will see the following results
+----+-------------+---------------------+------+---------------+------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+---------------------+------+---------------+------+---------+------+------+-------------+
| 1 | Simple | some_table | All | NULL | NULL | NULL | NULL | 166 | Using where |
+----+-------------+---------------------+------+---------------+------+---------+------+------+-------------+
Note the rows and key columns above. Rows shows how many rows the query affects, and we don't want this value to be too large. The key shows which index is used, or null to indicate that the query is not using any indexes.
If you want to make queries faster, you might want to add indexes for some columns:
CREATE INDEX myapp_mytable_myfield_idx on myapp_mytable (MyField);
In addition to configuring a MySQL profile to implement slow queries, there are a few ways to record slow queries:
Copy Code code as follows:

SELECT T.table_schema as ' db ',
T.table_name as ' TABLE ',
S.index_name as ' INDEX NAME ',
S.column_name as ' FIELD NAME ',
S.seq_in_index ' SEQ in INDEX ',
S2.max_columns as ' # cols ',
S.cardinality as ' card ',
T.table_rows as ' est ROWS ',
ROUND ((S.cardinality/ifnull (t.table_rows, 0.01)), 2) as ' sel% '
From INFORMATION_SCHEMA. STATISTICS s
INNER JOIN information_schema. TABLES T on s.table_schema = T.table_schema and s.table_name = T.table_name
INNER JOIN (
SELECT TABLE_SCHEMA, TABLE_NAME, index_name, MAX (seq_in_index) as Max_columns
From INFORMATION_SCHEMA. STATISTICS
WHERE table_schema!= ' MySQL ' GROUP by TABLE_SCHEMA, TABLE_NAME, index_name-as s2 on s.table_schema = s2. Table_schema and s.table_name = s2. table_name and s.index_name = s2. Index_name
WHERE t.table_schema!= ' mysql '/Filter out the MySQL system DB * *
T.table_rows >/* Only tables with some ROWS/*
And s.cardinality is not NULL/* Need at least one Non-null value in the field * *
and (S.cardinality/ifnull (t.table_rows, 0.01)) < 1.00/* Unique indexes are perfect anyway * *
ORDER BY ' sel% ', S.table_schema, s.table_name/* DESC for best non-unique indexes *
LIMIT 10;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.