MYSQL SQL Optimization

Source: Internet
Author: User

1. Learn the frequency of various SQL executions with the show status command

The show [session|global]status command can provide server state information, com_xxx indicates the number of times each statement was executed. Innodb_xxx at least for INNODB engines

These parameters make it easy to understand whether the current database is inserted as primary or query-focused, and how many types of SQL execution scale.

Connections: Number of attempts to connect to MySQL server

Uptime: Server Working time

Slow_querier: Slow Query count

2. Locating SQL statements that perform less efficiently

Location by slow query log, not very accurate

You can use show processlist to view the current MySQL threads in progress, including the state of the thread, whether the lock table, and so on, can see the execution of SQL in real time. Some locking table operations are also optimized.

3. Analyze the execution plan for inefficient SQL through explain

Select_type: Represents the type of select, a common value simple (i.e. not using table joins or subqueries). PRIMARY (main query, or outer query). Union (second or subsequent query statement in Union), subquery (the first select in a subquery, etc.)

Table: output result set tables

Type: Indicates how MySQL finds the desired row in the table, or is called a service type.

Common, all INDEX RANGE REF eq_ref const,system null from left to right, performance from worst to best

All: Full table scan, traverse full table

Index: Indexed full scan, MySQL traverses entire index query matching rows

Range:, index range Scan, common to <,<=,>,>=,between and other operators

Ref, using a non-unique index scan or prefix scan of a unique index, returns a row of records that match a single value

Eq_ref: Similar to ref, the difference is that the index used is a unique index, for one day the record match in the key table of each index, simply, is the use of primary key or unique index as the association condition in a multi-table connection

Const, Systom: There is a maximum of one matching row in a single table, and the query is very fast, so the other columns in the matching row can be handled by the optimizer as constants in the current query. Lee

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.