如何确定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
答案 0 :(得分:6)
神圣的嵌套子查询和parantheticals。啊!
派生表是为使您查询工作而创建的临时表。它们可以明确地表述如下:
SELECT
foo.horse
FROM
(SELECT horse from bar) as foo
foo
是派生表。这些通常会在查询在服务器上的执行中变成临时表。在你的情况下,他们并不那么明确。这可能是因为您正在查询其中包含视图的视图,而且主只知道它有多深。
派生表很好,因为它们允许您在将表连接到另一个表,视图或派生表之前从表(或视图)中选择数据。但他们有一个缺点,他们没有索引。由于您无法控制索引,因此对派生表的连接更加昂贵。如果您的数据很小,或者您在嵌套(嵌套(嵌套())设计中小心,那么一切都会好的。
最后,并且无关,我相信你的parantheticals是多余的。我相信如果您取消它们,您的查询将更具可读性。