我的解释语句中的派生表是啥

Posted

技术标签:

【中文标题】我的解释语句中的派生表是啥【英文标题】:What are the derived tables in my explain statement我的解释语句中的派生表是什么 【发布时间】:2014-10-23 13:57:25 【问题描述】:

如何确定 EXPLAIN 结果中列出的派生表?我派生的表似乎没有任何可以使用的键,我认为这意味着需要进行一些优化。

选择语句:

EXPLAIN SELECT * 
FROM vw_environment_deployment_statuses v
  WHERE asset_id=47;

查看SQL:

SELECT
  `ac`.`asset_id` AS `asset_id`,
  `es`.`environment_id` AS `environment_id`,
  `d1`.`deployed_date` AS `last_deployed_date`,
  `d1`.`revision` AS `last_deployed_revision`,
  `d2`.`deployed_date` AS `last_successful_deployed_date`,
  `d2`.`revision` AS `last_successful_deployed_revision`,
  `e`.`acronym` AS `environment`,
  `v3`.`count_is_failed` AS `last_deployed_is_failed`
FROM (((((((((`asset_configurations` `ac`
  JOIN `vw_deployments` `d1`
    ON ((`ac`.`id` = `d1`.`asset_configuration_id`)))
  JOIN `vw_deployments` `d2`
    ON ((`ac`.`id` = `d2`.`asset_configuration_id`)))
  JOIN `servers` `s`
    ON ((`ac`.`server_id` = `s`.`id`)))
  JOIN `environments_servers` `es`
    ON ((`s`.`id` = `es`.`server_id`)))
  JOIN `environments` `e`
    ON ((`es`.`environment_id` = `e`.`id`)))
  JOIN `vw_last_environment_deployment_statuses` `v1`
    ON (((`ac`.`asset_id` = `v1`.`asset_id`) AND (`es`.`environment_id` = `v1`.`environment_id`) AND (`d1`.`deployed_date` = `v1`.`deployed_date`))))
  JOIN `vw_last_successful_environment_deployment_statuses` `v2`
    ON (((`ac`.`asset_id` = `v2`.`asset_id`) AND (`es`.`environment_id` = `v2`.`environment_id`) AND (`d2`.`deployed_date` = `v2`.`deployed_date`))))
  JOIN `vw_environment_fail_count` `v3`
    ON (((`ac`.`asset_id` = `v3`.`asset_id`) AND (`v3`.`environment_id` = `e`.`id`))))
  JOIN `domains` `dom`
    ON ((`ac`.`domain_id` = `dom`.`id`)))

解释说明:

1   PRIMARY ac  ref PRIMARY,FK_asset_configurations_assets_id,FK_asset_configurations_domains_id,FK_asset_configurations_servers_id FK_asset_configurations_assets_id   4   const   15  
1   PRIMARY s   eq_ref  PRIMARY PRIMARY 4   aps_cmdb.ac.server_id   1   Using index
1   PRIMARY es  ref PRIMARY,FK_environments_servers_servers_id2 FK_environments_servers_servers_id2 4   aps_cmdb.ac.server_id   1   Using index
1   PRIMARY dom eq_ref  PRIMARY PRIMARY 4   aps_cmdb.ac.domain_id   1   Using index
1   PRIMARY dep ref FK_deployments_asset_configurations_id  FK_deployments_asset_configurations_id  4   aps_cmdb.ac.id  5   
1   PRIMARY dep ref FK_deployments_asset_configurations_id  FK_deployments_asset_configurations_id  4   aps_cmdb.dep.asset_configuration_id 5   Using where

我很满意...以下派生表我不知道它们是由什么组成的。

1   PRIMARY <derived9>  ALL (null)  (null)  (null)  (null)  148 Using where; Using join buffer
1   PRIMARY <derived7>  ALL (null)  (null)  (null)  (null)  148 Using where; Using join buffer
1   PRIMARY <derived4>  ALL (null)  (null)  (null)  (null)  150 Using where; Using join buffer
1   PRIMARY e   eq_ref  PRIMARY PRIMARY 4   v3.environment_id   1   Using where
9   DERIVED <derived15> ALL (null)  (null)  (null)  (null)  195 Using temporary; Using filesort
9   DERIVED ac  eq_ref  PRIMARY,FK_asset_configurations_assets_id,FK_asset_configurations_domains_id,FK_asset_configurations_servers_id PRIMARY 4   lsd.asset_configuration_id  1   Using where
9   DERIVED es  ref PRIMARY,FK_environments_servers_servers_id2 FK_environments_servers_servers_id2 4   aps_cmdb.ac.server_id   1   Using index
9   DERIVED d   eq_ref  PRIMARY PRIMARY 4   aps_cmdb.ac.domain_id   1   Using index
9   DERIVED Asset   eq_ref  PRIMARY PRIMARY 4   aps_cmdb.ac.asset_id    1   Using index
9   DERIVED e   eq_ref  PRIMARY PRIMARY 4   aps_cmdb.es.environment_id  1   Using index
9   DERIVED s   eq_ref  PRIMARY PRIMARY 4   aps_cmdb.es.server_id   1   Using where; Using index
9   DERIVED dep ref FK_deployments_asset_configurations_id  FK_deployments_asset_configurations_id  4   aps_cmdb.ac.id  5   Using where
9   DERIVED dep ref FK_deployments_asset_configurations_id  FK_deployments_asset_configurations_id  4   aps_cmdb.dep.asset_configuration_id 5   Using where
9   DERIVED <derived12> ALL (null)  (null)  (null)  (null)  197 Using where; Using join buffer
15  DERIVED ac  index   PRIMARY UK_asset_configurations 777 (null)  229 Using where; Using index; Using temporary; Using filesort
15  DERIVED dep ref FK_deployments_asset_configurations_id  FK_deployments_asset_configurations_id  4   aps_cmdb.ac.id  5   Using where
12  DERIVED ac  index   PRIMARY UK_asset_configurations 777 (null)  229 Using where; Using index; Using temporary; Using filesort
12  DERIVED dep ref FK_deployments_asset_configurations_id  FK_deployments_asset_configurations_id  4   aps_cmdb.ac.id  5   
7   DERIVED ac  ALL PRIMARY,FK_asset_configurations_servers_id  (null)  (null)  (null)  229 Using where; Using temporary; Using filesort
7   DERIVED es  ref FK_environments_servers_servers_id2 FK_environments_servers_servers_id2 4   aps_cmdb.ac.server_id   1   Using index
7   DERIVED s   eq_ref  PRIMARY PRIMARY 4   aps_cmdb.ac.server_id   1   Using index
7   DERIVED dep ref FK_deployments_asset_configurations_id  FK_deployments_asset_configurations_id  4   aps_cmdb.ac.id  5   Using where
4   DERIVED ac  ALL PRIMARY,FK_asset_configurations_servers_id  (null)  (null)  (null)  229 Using where; Using temporary; Using filesort
4   DERIVED es  ref FK_environments_servers_servers_id2 FK_environments_servers_servers_id2 4   aps_cmdb.ac.server_id   1   Using index
4   DERIVED s   eq_ref  PRIMARY PRIMARY 4   aps_cmdb.ac.server_id   1   Using index
4   DERIVED dep ref FK_deployments_asset_configurations_id  FK_deployments_asset_configurations_id  4   aps_cmdb.ac.id  5   

【问题讨论】:

【参考方案1】:

神圣嵌套的子查询和副查询。啊!

派生表是为使您的查询工作而创建的临时表。它们可以明确表示为:

SELECT
    foo.horse
FROM
    (SELECT horse from bar) as foo

其中foo 是派生表。这些通常会在服务器上的查询执行中变成临时表。在您的情况下,它们并不那么明确。这可能是因为您正在查询带有视图的视图,而主只知道它的深度。

派生表很好,因为它们允许您在将表(或视图)连接到另一个表、视图或派生表之前从表(或视图)中选择数据。虽然它们有一个缺点,但它们没有被索引。由于您失去了对索引的控制,派生表上的连接成本更高。如果您的数据很小,或者您在嵌套(嵌套(嵌套()))设计中很小心,那么一切都会好起来的。

最后,无关紧要,我相信你的附庸是多余的。我相信如果您取消它们,您的查询将更具可读性。

【讨论】:

我认为括号是由 dbForge 添加的。当我编辑查询时,我讨厌它们并把它们去掉......叹息。那里必须有一个设置来阻止这种情况的发生。由于我无法在视图中进行子查询,因此不确定我的选择是什么。 嗨 Nevil,好久不见,碰巧在这里看到了你的答案(你在 2018 年回答了我的一个问题:P)。最近因为工作的关系一直在学习一些SQL知识,在这里问了question。问题基本解决了,但我仍在努力了解细节。回到正题,根据那个问题的答案的参考,mysql是不是早在2012年5.6就支持自动生成派生表的索引了?

以上是关于我的解释语句中的派生表是啥的主要内容,如果未能解决你的问题,请参考以下文章

帮忙解释下面这段Linux kernel中的Makefile语句是啥意思?

MySQL的SQL语句 - 数据操作语句(13)- 子查询(13)

“oracle ”截断表是啥意思?

写语句不能在派生类型的用户定义格式化 I/O 过程中生成新行

Write语句不能在用户定义的格式化I / O过程中为派生类型生成新行

Android光标中的“fillWindow()中的无效语句”是啥意思?