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.

(last_query_cost对于简单的查询可以精确的得到计算,但于包含子查询或union的复杂查询值是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.

There's some interesting resources on the query optimizer available online, if you want to learn more. Unfortunately, I don't have any links for you readily available, but it shouldn't be too hard to find some resources through a simple search for "mysql query optimizer".

参考:

https://dev.mysql.com/doc/refman/5.5/en/server-status-variables.html#statvar_Last_query_cost
http://stackoverflow.com/questions/1071708/further-information-about-last-query-cost-in-mysql

last_query_cost的更多相关文章

  1. Mysql命令show global status求根溯源

    近来,发现好多公司对mysql的性能监控是通过show global status实现的,因此对于这个命令想要探究一番,看他是否是实时更新的. 在此之前,我们必须搞明白mysql对于这个命令的执行过程 ...

  2. mysql 查询成本

    SELECT SQL_NO_CACHE spu from dp_distributor_products_1_online where dpId > 15 AND dpId <= 60; ...

  3. MySQL运行状态show status详解

    状态名 作用域 详细解释 Aborted_clients Global 由于客户端没有正确关闭连接导致客户端终止而中断的连接数 Aborted_connects Global 试图连接到MySQL服务 ...

  4. MySQL运行状态show status中文详解(转)

    要查看MySQL运行状态,要优化MySQL运行效率都少不了要运行show status查看各种状态,下面是参考官方文档及网上资料整理出来的中文详细解释: 状态名 作用域 详细解释 Aborted_cl ...

  5. 高性能MySQL笔记 第6章 查询性能优化

    6.1 为什么查询速度会慢   查询的生命周期大致可按照顺序来看:从客户端,到服务器,然后在服务器上进行解析,生成执行计划,执行,并返回结果给客户端.其中“执行”可以认为是整个生命周期中最重要的阶段. ...

  6. MySQL如何关联查询

    总的来说,mysql认为任何一个查询都是一次关联,并不仅仅是一个查询需要用到两个表匹配才叫关联,所以,在mysql中,每一个查询,每一个片段(包括子查询,甚至单表select)都可能是关联.所以,理解 ...

  7. mysql:键缓存

    myisam的主要优化参数: key_buffer_size - 这对MyISAM表来说非常重要,是用来设置整个MySQL中常规Key Cache的大小.一般来说,如果MySQL运行在32位平台,此值 ...

  8. mysql show processlist命令 详解

    SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...

  9. MySQL查询优化 (一)

    以下的文章主要讲述的是MySQL查询优化的5个十分好用方法,熟悉SQL语句的人都清楚,如果要对一个任务进行操作的话,SQL语句可以有很多种相关写法,但是不同的写法查询的性能可能会有天壤之别. 本文列举 ...

随机推荐

  1. 04 mysql 基础三 (进阶)

    mysql 基础三 阶段一 mysql 单表查询 1.查询所有记录 select * from department; ​ select * from student; ​ select * from ...

  2. python2.7入门---正则表达式

        正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配.Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.re 模块使 Pytho ...

  3. python基础之反射、面向对象进阶

    isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象,如果是返回True 1 class F ...

  4. 初步学习pg_control文件之八

    接前文  初步学习pg_control文件之七  继续 看:catalog_version_no 代码如下: static void WriteControlFile(void) { ... /* * ...

  5. C++11中default的使用

    In C++11, defaulted and deleted functions give you explicit control over whether the special member ...

  6. Scala function programming

    1. Arbitrary multi parameters funcs sum(1,2,3,4,5) = sum(1 to 5: _*)the equal '=' can be ignored if ...

  7. 【个人训练】(UVa11129)An antiarithmetic permutation

    题意与解析 一条非常有趣的二分题.一开始没有懂解法,去网上看了半天全是做法没有这样做为什么是对的(或者说的很含糊).一做完回顾一下立刻有点开朗的感觉. 题意很简单,维护一个0-n-1的数列,使其选出长 ...

  8. spring-boot分页插件

    1.分页插件,spring-boot.,第一次调用时,存值到 model.addAttribute("status", id);页面获取2.页面获取 后台存入的值,放在input ...

  9. javaX邮件发送

    /** * *  * @param mailServerHost 邮件服务器 * @param mailServerPort 端口 * @param validate 是否需要身份验证 * @para ...

  10. VS2013 启用avalon 智能提示 Intelligence

    第一步: 关闭VS2013. 第二步: 进入目录: C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Packages\schem ...