Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySql中的大量SQL查询优化问题_Mysql_Sql_Query Optimization - Fatal编程技术网

MySql中的大量SQL查询优化问题

MySql中的大量SQL查询优化问题,mysql,sql,query-optimization,Mysql,Sql,Query Optimization,我正在MySQL上运行一个非常繁重的查询,这需要几个小时的时间,我希望能够缩短处理时间。 看起来是这样的: insert into customers_input select * from ( select * from cust_a a join cut_b b on a.id=b.id where a.date='2015-01-01' and a.date='2015-01-01' ) a left join ( select * from cust_c

我正在MySQL上运行一个非常繁重的查询,这需要几个小时的时间,我希望能够缩短处理时间。 看起来是这样的:

insert into customers_input
select *
from 
(
   select *
   from cust_a a join cut_b b on a.id=b.id
   where a.date='2015-01-01' and a.date='2015-01-01'
) a left join
(
   select *
   from cust_c
) b on a.id=b.id;
cust_a-有8000000行,日期列只有两个不同的值,id列上还有一个BTREE索引

cust_b-有600000行,日期列只有两个不同的值 ,此外,id列上还有一个BTREE索引

cust_c-有20000行

我怀疑问题出在连接表cust_a和cust_b的子查询(a)上,原因很简单,因为自从我添加了这个子查询以来,处理时间急剧增加

如有任何意见或建议,将不胜感激


提前感谢

首先,不要使用子查询。您的查询也可以写为:

select *
from cust_a a join
     cust_b b
     on a.id = b.id and a.date = b.date
     where a.date = '2015-01-01' left join
     cust_c c
     on a.id = c.id;
另外,

  • 我修正了表格名称的输入错误
  • 我修正了日期比较中的打字错误
  • 我将
    b
    date
    比较移至
    on
    子句
  • 我为
    c
    添加了一个别名
然后,此查询可以受益于索引:
cust\u a(日期,id)
cust\u b(id,日期)
cust\u c(id)
。(客户索引的列可以是任意顺序。)


尝试查询,看看它是否及时返回值。然后,您可以将插入的
放回。

您需要向我们显示表和索引定义,以及每个表的行数。也许您的表定义不好。可能索引没有正确创建。也许你在你认为你有的专栏上没有索引。如果看不到表和索引定义,我们无法判断。我们需要行计数,因为这会影响查询规划。如果您知道如何进行
解释
或获得执行计划,请将结果也放在问题中。如果您没有索引,请访问。在MySQL的最新版本之前,这些子查询可能没有索引,因此需要(tmp表的)大型表扫描。谢谢!取出子查询并放置索引有助于分配。只剩几分钟了。。。