What is slow query?
Slow query definition and role
Slow query log, by definition, slow query log, refer to mysql record all SQL statements executed more than log time threshold long_query_time parameter setting. The log can bring good help for the optimized SQL statements. By default, the slow query log is disabled, to use the slow query log function, you must first turn on slow query log function.
Start slow queries
Common arrangement
slow_query_log start stop technology slow query log
slow_query_log_file slow query log was specified storage path and file (the default data file and put together)
long_query_time designated record slow query log SQL execution time was cutting value (unit: seconds, 10 seconds by default)
log_queries_not_using_indexes whether the records are not to use the index SQL
Local log_output log storage [TABLE] [FILE] [FILE, TABLE]
Record was qualified SQL
check sentence
Data modification statements
SQL has been rolled back too
Slow query log parsing
Commonly used slow query log analysis tool (mysqldumpslow)
Summary In addition to query other identical SQL, and the order of the results according to parameters specified in the output analysis.
grammar
mysqldumpslow -s r -t 10 slow-mysql.log
-s order (c,t,l,r,at,al,ar)
c: total number of
t: total time
l: the lock time
r: the total data line
at, al, ar: t, l, r [e.g. Mean: at = Total time / total number]
-t top front of days taken to specify output as a result
Example:
mysqldumpslow.pl -s t -t 10 D:\DESKTOP-2EKGEE5-slow.log
Common optimizations
Server Hardware
MySql Server Optimization
SQL optimization itself
Anti-paradigm design optimization
Index Tuning
Anti-paradigm design
What is anti-paradigm design?
Anti-normalization is for the paradigm in terms of
The so-called anti-normalization is to get the performance and efficiency have to read and consider the appropriate database design paradigm was obtained in violation of the requirements
It has allowed a small amount of redundancy, in other words the anti-normalization is to use space for time
Three Forms
The first big paradigm of database design
All fields in a database table having only a single attribute
Column single attribute is composed of basic data types
The tables are designed simple two-dimensional table
The second great paradigm of database design
It requires a table having only natural key, i.e. the table can not meet the second paradigm presence of a non-primary key column only dependence on the part of the primary key
Order number and product ID no direct link
After modifying ->
Third paradigm of database design
Each attribute refers feifei main portion is dependent on neither transmission nor rely on natural key, i.e. on the basis of the second paradigm transmitted along the non-primary key for the primary key dependency
Number and order number associated with customer management
Customer name and order number associated with management
Customer number and customer name associated
This column delete the customer's name, placed only in the customer table
What indexes are?
MySQL is the official definition of the index: the index (Index) to help MySQL efficiently get the data structure of the data.
Index can be obtained in nature: the index is a data structure.
The simplest index
Index slightly more complex
The index MySql
Above is a data table, a total of two seven records, the far left is the physical address of the data record
mysql innodb default storage engine only explicit support B-Tree (B + Tree is technically) Index
Index Classification
Common Index: i.e. only contains a single column index, a table can have multiple separate index
The only index: the index column value must be unique, but allow free value
Composite index: the index comprises a plurality of columns
Creating an index
CREATE [UNIQUE ] INDEX indexName ON mytable(columnname(length));
ALTER TABLE 表名 ADD [UNIQUE ] INDEX [indexName] ON (columnname(length))
View Index
SHOW INDEX FROM table_name
Delete Index
DROP INDEX [indexName] ON mytable
Implementation plan
What execution plan?
Use EXPLAIN keyword can simulate SQL query optimizer to perform, so they know how to handle your MySQL is a SQL statement. Analysis of performance bottlenecks in your query or table structure
grammar
Explain + SQL statement
The role of the implementation plan
Reading order table
Data read operation type of operation
Which indexes can be used
Which indexes are actually used
References between tables
Each table of how many rows the query optimizer
Information contained in the Plan of Implementation
Implementation plan -ID
Implementation plan -select_type
Implementation plan -table
Data show that this line of tables on which
Implementation plan -type
type shows the type of access is a more important indicator, the resulting value from best to worst are:
system> const > eq_ref > ref > fulltext > ref_or_null > index_merge> unique_subquery > index_subquery > range > index > ALL
We need to remember
system>const>eq_ref>ref>range>index>ALL
Implementation plan -key_len
key_len index indicates the number of bytes used
According to this value, you can determine the index usage, particularly when the composite index, to determine whether or not all the index fields are used in the query.
char and varchar with character encoding is also closely linked,
latin1 1 byte, 2 bytes GBK, utf8 occupies 3 bytes. (Different character encodings take up storage space)
Index optimization strategy
1. Try to match the full value of the policy
EXPLAIN
SELECT * FROM staffs WHERE NAME = 'July';
EXPLAIN
SELECT * FROM staffs WHERE NAME = 'July' AND age = 25;
EXPLAIN
SELECT * FROM staffs WHERE NAME = 'July' AND age = 25 AND pos = 'dev';
2. The best strategy left-prefix rule
If the index multiple columns, the most left-prefix to comply with the law. It refers to a query from the leftmost in the forefront of the beginning and not skip index column in the index.
3. The strategy does not do anything on indexed columns
Not do anything column index (calculated, function (automatic or manual) type conversion), the index will lead to failure of the steering full table scan
EXPLAIN SELECT * FROM staffs WHEREleft(NAME,4) = 'July';
4. The scope of the policy conditions put final
Storage engine can not use the index in the right column of the range conditions
5. Try to use strategy covering index
Try to use a covering index (an index only access query (indexed columns and query columns consistent)) to reduce the select *
6. strategy is not equal to even use
mysql is not equal in use (! = or <>) can not be used when the index will lead to a full table scan
Policy 7.Null / Not influential
May affect the note null / notnull of the index
Queries should be careful strategy 8.Like
Wildcard like to begin ( '% abc ...') mysql failure index table scan operation becomes perfected
9. Policy character type quotes
String failure without single quotation marks index
UNION efficient change strategies 10.OR
The full value of my favorite matches, the most left-prefix to comply;
Take the lead in Big Brother can not die, the middle brother can not be broken;
Less computationally column indexes, failure after full range;
LIKE percentage write rightmost, covering indexes do not write *;
There is unequal null OR, pay attention to the impact index;
VARCHAR quotation marks can not be lost, SQL optimization has a knack.