热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

辛星简译MySQL中的last_query_cost_MySQL

我们都知道在MySQL中可以通过showstatuslikelast_query_cost来查看查上一个查询的代价,而且它是io_cost和cpu_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)它对于比较我们的开销是非常有用的,特别是我们有好几种查询方式可选的时候。

推荐阅读
  • php还能用多少年(php还行吗)
    导读:很多朋友问到关于php还能用多少年的相关问题,本文编程笔记就来为大家做个详细解答,供大家参考,希望对大家有所帮助!一起来看看吧!本文目录一览: ... [详细]
  • 错误:requireluasql.mysqlstdin:1:moduleluasql.mysqlnotfound:nofieldpackage.preload[lua ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • Openresty+Lua+Redis灰度发布
    Openresty+Lua+Redis灰度发布灰度发布,简单来说,就是根据各种条件,让一部分用户使用旧版本,另一部分用户使用新版本。百度百科中解释:灰度发布是指在黑与白之间,能够平 ... [详细]
  • Server Installation for Jitsi Meet
    2019独角兽企业重金招聘Python工程师标准ServerInstallationforJitsiMeetThisdescribesconfiguringaserverji ... [详细]
  • 整整的花了一个下午的时候,才在lua中调用了动态链接库。比起其他脚本语言,lua的调用方式算是比较繁琐的,但是lua的编程思想非常的统一& ... [详细]
  • 我有一个非常大的TSV文件。第一行是标题。如果字段为空白,则以下各行包含数 ... [详细]
  • 单目标应用:最有价值球员算法(Most Valuable Player Algorithm,MVPA)求解旅行商问题TSP
    一、最有价值球员算法最有价值球员算法(MostValuablePlayerAlgorithm,MVPA)由Bouchekara等人于20 ... [详细]
  • redis知识汇总[随笔记录]
      ... [详细]
  • --文件IO:分简单模式与完全模式。简单模式在做一些简单的文件操作时较为合适。但是在进行一些高级的文件操作的时候,简单模式就显得力不从心。例如同时读取多 ... [详细]
  • 样式迁移(风格迁移)
    原文来源:https:arxiv.org、https:github.com作者:FujunLuan、SylvainParis、EliShechtman、KavitaB ... [详细]
  • 基于Redis实现分布式锁剖析
    之前的文章《分布式锁详解-分别利用Zookeeper和数据库实现分布式锁》,由于篇幅太长,又碰上加班时间不够充裕,所以没有把Redis的实 ... [详细]
  • luarestyqlesswebUI界面运行
    lua-resty-qless-web是lua-resty-qless的web管理界面以及lua-resty-template模版引擎开发的,里面实现了一个简单的路由功能备注:de ... [详细]
  • 1、概念解读1.1什么是链接?链接是一种在共享文件和访问它的用户的若干目录项之间建立联系的方法。Linux系统中有两种链接:硬链接(HardLink)和软链接(SoftLink), ... [详细]
  • 《计算机专业英语基础知识》由会员分享,可在线阅读,更多相关《计算机专业英语基础知识(25页珍藏版)》请在人人文库网上搜索。1、专业英语知识补充,本章学习 ... [详细]
author-avatar
零泉爱
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有