Sinsing the last_query_cost of MySQL in a simple translation

Source: Internet
Author: User

We all know that in MySQL you can see the cost of checking the previous query through show status like ' Last_query_cost ', and it's the sum of the cost of Io_cost and Cpu_cost, It is often a common indicator of how efficiently we evaluate the execution of a query.

Here is an English explanation:

The total cost of the last compiled query as computed by the query optimizer. This was useful for comparing the cost of different query plans for the same query. The default value of 0 means that is no query has been compiled yet. The default value is 0. Last_query_cost has session scope.

The Last_query_cost value can be computed accurately-simple ' flat ' queries, not complex queries such as those wit H subqueries or UNION. For the latter, the value was set to 0.


Q:

When doing query optimization, the SHOW STATUS query returns values that is easy to understand with some practice and exp Lanation.

But Last_query_cost is obscure and poorly documented.

The only thing explained are that it must being read as an anti-macho value:the smaller the better.

But does we have further information on this high-level value? What's its unit? How is it calculated (estimated)? etc. How can we use the IT for a advanced profiling?

A:

This is the MySQL Query Optimizer works. When you enter and execute a query, MySQL would construct a query plan. This was done by evaluating how the query can be executed in several different ways, and assigning ' costs ' to the different Possibilities. These costs is based mostly on internal statistics, and includes data such as the number of rows in the table, the Cardin Ality of different indices and so forth. When this was done, MySQL choses the least expensive plan and executes the query. The Last_query_cost value is this cost value.

As you ' ve no doubt seen in the manual:

The total cost of the last compiled query as computed by the query optimizer. This was useful for comparing the cost of different query plans for the same query. The default value of 0 means that is no query has been compiled yet. The default value is 0. Last_query_cost has session scope.

This is indeed true. The value is only useful as a quantitative measurement to compare different queries.

I believe that friends as long as the English has six levels, or even four can understand, so I'm just refining a few of the points, the main points are as follows:

(1) It is a basis for comparing the overhead between the various queries.

(2) It can only detect a relatively simple query cost, for queries that contain subqueries and unions are not tested.

(3) When we execute a query, MySQL automatically generates an execution plan, called query plan, and usually has many different implementations, and it chooses the lowest one, and that cost is the least expensive one.

(4) It is very useful to compare our expenses, especially when we have several kinds of query options available.

Sinsing the last_query_cost of MySQL in a simple translation

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.