为什么mysql嵌套选择比在我的情况下加入更快?

时间:2015-11-03 03:17:53

标签: mysql join

嗨,大家好,

我的问题如下,我认为问题是我的INNER JOIN片段,如何优化?

数据库关系
enter image description here

查询条件:网站名称 查询目标: graph_path

SQL:

  1. 内部加入:

      

    选择 graph.graph_path
       FROM 网站
       INNER JOIN 库存(sites.id = inventory.site_id)
       INNER JOIN 主持人(host.inventory_id = inventory.id)
      (strong> INNER JOIN 图表(graph.host_id = host.id)
       WHERE sites.sitename =' 10071';

      的结果:
      设置1行( 2.74秒

      的说明:
      enter image description here

  2. 嵌套:

      

    选择 graph_path
       FROM
       WHERE host_id =(从主机中选择内容库,其中inventory_id =(从库存中选择id,其中site_id =(从网站名称中选择内容名称为' 10071'));)

      的结果:
      1行( 0.03秒

      的说明:
      enter image description here

1 个答案:

答案 0 :(得分:0)

此查询的最佳索引为:sites(sitename, id)inventory(site_id, id)host(inventory, id)graph(hostid, graph_path)

我猜你只有表中id的索引。在第一个版本中,它首先执行所有连接,然后按站点过滤。在第二个中,它可能首先扫描sites,然后快速查看其余查询。

相关问题