程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 辛星簡譯MySQL中的last_query_cost

辛星簡譯MySQL中的last_query_cost

編輯:MySQL綜合教程

辛星簡譯MySQL中的last_query_cost


我們都知道在MySQL中可以通過show status like 'last_query_cost' 來查看查上一個查詢的代價,而且它是io_cost和cpu_cost的開銷總和,它通常也是我們評價一個查詢的執行效率的一個常用指標。

下面是一段英文解釋:

The total cost of the last compiled query as computed by the query optimizer. This is useful for comparing the cost of different query plans for the same query. The default value of 0 means that 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 only for simple “flat” queries, not complex queries such as those with subqueries or UNION. For the latter, the value is set to 0.


Q:

When doing query optimization, the SHOW STATUS query returns values that are easy to understand with some practice and explanation.

But last_query_cost is obscure and poorly documented.

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

But do we have further information about this high-level value? What is its unit? How is it calculated (estimated)? etc. How can we use it for an advanced profiling?

A:

This has to do with how the MySQL Query Optimizer works. When you enter and execute a query, MySQL will construct a query plan. This is done by evaluating how the query can be executed in several different ways, and assigning "costs" to the different possibilities. These costs are based mostly on internal statistics, and includes data such as the number of rows in the table, the cardinality of different indices and so forth. When this is 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 is useful for comparing the cost of different query plans for the same query. The default value of 0 means that 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.
 

相信朋友們只要英語過了六級,甚至是四級都能看得懂,因此我這裡只是提煉一下其中的要點,要點如下:

(1)它是作為比較各個查詢之間的開銷的一個依據。

(2)它只能檢測比較簡單的查詢開銷,對於包含子查詢和union的查詢是測試不出來的。

(3)當我們執行查詢的時候,MySQL會自動生成一個執行計劃,也就是query plan,而且通常有很多種不同的實現方式,它會選擇最低的那一個,而這個cost值就是開銷最低的那一個。

(4)它對於比較我們的開銷是非常有用的,特別是我們有好幾種查詢方式可選的時候。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved