如何确定 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
最佳答案
神圣的嵌套子查询和附加词。啊!
派生表是为使您能够进行查询而创建的临时表。它们可以明确地表述如下:
SELECT
foo.horse
FROM
(SELECT horse from bar) as foo
其中
foo
是派生表。在服务器上执行查询时,这些通常会变成临时表。在您的情况下,它们不是那么明确。这可能是因为您正在查询其中包含 View 的 View ,而 lord 只知道它的深度。派生表很好,因为它们允许您在将表(或 View )连接到另一个表、 View 或派生表之前从表(或 View )中选择数据。但它们也有不利的一面,它们没有被编入索引。派生表上的连接成本更高,因为您无法控制索引。如果您的数据很小,或者您在nested(nested(nested())) 设计中很小心,那么一切都会好起来的。
最后,而且不相关,我相信你的伪命题是多余的。我相信如果您取消它们,您的查询将更具可读性。