MySql 中的严重 SQL 查询优化问题
heavy SQL query optimization issue in MySql
我 运行 对 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 - 有 8,000,000 行,日期列只有两个不同的值,此外 id 列上有一个 BTREE 索引
cust_b - 有 600,000 行,日期列只有两个不同的值
, 另外在 id 列上有一个 BTREE 索引
cust_c - 有 20,000 行
我怀疑问题出在连接表 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;
此外,
- 我更正了 table 名称中的拼写错误。
- 我修正了日期比较中的拼写错误。
- 我将
b
的 date
比较移到了 on
子句中。
- 我为
c
添加了一个别名。
此查询随后可以受益于索引:cust_a(date, id)
、cust_b(id, date)
和 cust_c(id)
。 (cust_b
索引的列可以采用任意顺序。)
尝试查询并查看它 returns 值是否及时。然后你可以把 insert
放回去。
我 运行 对 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 - 有 8,000,000 行,日期列只有两个不同的值,此外 id 列上有一个 BTREE 索引
cust_b - 有 600,000 行,日期列只有两个不同的值 , 另外在 id 列上有一个 BTREE 索引
cust_c - 有 20,000 行
我怀疑问题出在连接表 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;
此外,
- 我更正了 table 名称中的拼写错误。
- 我修正了日期比较中的拼写错误。
- 我将
b
的date
比较移到了on
子句中。 - 我为
c
添加了一个别名。
此查询随后可以受益于索引:cust_a(date, id)
、cust_b(id, date)
和 cust_c(id)
。 (cust_b
索引的列可以采用任意顺序。)
尝试查询并查看它 returns 值是否及时。然后你可以把 insert
放回去。