left join inner join 等超级慢

xiye_jfb 2014-01-23 11:37:15

A表
f_line f_produce f_date f_qty --大概1W条
1005 KX-AAA-R1 2014-01-03 80
1004 KX-BBB-R1 2014-01-05 90
B表
f_line f_produce f_date f_qty --大概1W条
1005 KX-AAA-R1 2014-01-03 80
1004 KX-CCC-R1 2014-01-05 90
A表和B表其他字段就不打出来了,主要需要数据的字段
--现在的sql
select a.f_linename,a.f_producemodel,left(a.f_date,7),sum(a.f_qty),b.f_linename,b.f_producemodel,left(b.f_date,7),sum(b.f_qty) from a
left join b on a.f_linename = b.f_linename and a.f_producemodel = b.f_producemodel
and left(a.f_date,7) = left(b.f_date,7) where a.f_batch = 28 and b.f_batch = 28
group by a.f_linename,a.f_producemodel,left(a.f_date,7),
b.f_linename,b.f_producemodel,left(b.f_date,7)

查询很慢 而且left join 的效果都没体现出来 因为A表和B表每个月的数据有很多重复的
要合计月份数据left join 找出A表存在的数据B表不存在的出来看 请问怎么查询才快
...全文
280 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
發糞塗牆 2014-01-23
  • 打赏
  • 举报
回复
引用 11 楼 u012208913 的回复:
[quote=引用 7 楼 yupeigu 的回复:] 另外,你试试这样: update statistics a update statistcis b
这个用上快多了 为什么我加Where后 left join 的效果就没了?[/quote]根据查询的语句,优化器会帮你创建统计信息,但是如果是多列的话,要手动创建,用数据库引擎优化顾问可以看看缺失了什么
LongRui888 2014-01-23
  • 打赏
  • 举报
回复
引用 11 楼 u012208913 的回复:
[quote=引用 7 楼 yupeigu 的回复:] 另外,你试试这样: update statistics a update statistcis b
这个用上快多了 为什么我加Where后 left join 的效果就没了?[/quote] 这个是用来更新统计信息的,一般能使得查询更快。
LongRui888 2014-01-23
  • 打赏
  • 举报
回复
引用 14 楼 u012208913 的回复:
上班不能上QQ

--A create table a(f_line varchar(30),f_produce varchar(30), f_date varchar(30),f_qty int,f_batch int) --B create table b(f_line varchar(30),f_produce varchar(30), f_date varchar(30),f_qty int,f_batch int) insert a select '1005','KX-AAA-R1','2014-01-03',80,28 union all select '1004','KX-BBB-R1','2014-01-05',90,28 insert b select '1005','KX-AAA-R1','2014-01-03',80,28 union all select '1004','KX-CCC-R1','2014-01-05',90,28 
你创建表看看

select a.f_line,a.f_produce,left(a.f_date,7), sum(a.f_qty),b.f_line,b.f_produce,left(b.f_date,7),sum(b.f_qty)   from a left join b on a.f_line = b.f_line and a.f_produce = b.f_produce  and left(a.f_date,7) = left(b.f_date,7)  --where a.f_batch = 28 and b.f_batch = 28    group by a.f_line,a.f_produce,left(a.f_date,7), b.f_line,b.f_produce,left(b.f_date,7)
有where 没 where 是2个不同的结果你看看
你这个写法有问题,如果用left join,那么必须要把b表的,关联条件放到 on后面: select a.f_line,a.f_produce,left(a.f_date,7), sum(a.f_qty),b.f_line,b.f_produce,left(b.f_date,7), sum(b.f_qty) from a left join b on a.f_line = b.f_line and a.f_produce = b.f_produce and left(a.f_date,7) = left(b.f_date,7) and b.f_batch = 28 where a.f_batch = 28 group by a.f_line,a.f_produce,left(a.f_date,7), b.f_line,b.f_produce,left(b.f_date,7)
LongRui888 2014-01-23
  • 打赏
  • 举报
回复
引用 14 楼 u012208913 的回复:
上班不能上QQ

--A create table a(f_line varchar(30),f_produce varchar(30), f_date varchar(30),f_qty int,f_batch int) --B create table b(f_line varchar(30),f_produce varchar(30), f_date varchar(30),f_qty int,f_batch int) insert a select '1005','KX-AAA-R1','2014-01-03',80,28 union all select '1004','KX-BBB-R1','2014-01-05',90,28 insert b select '1005','KX-AAA-R1','2014-01-03',80,28 union all select '1004','KX-CCC-R1','2014-01-05',90,28 
你创建表看看

select a.f_line,a.f_produce,left(a.f_date,7), sum(a.f_qty),b.f_line,b.f_produce,left(b.f_date,7),sum(b.f_qty)   from a left join b on a.f_line = b.f_line and a.f_produce = b.f_produce  and left(a.f_date,7) = left(b.f_date,7)  --where a.f_batch = 28 and b.f_batch = 28    group by a.f_line,a.f_produce,left(a.f_date,7), b.f_line,b.f_produce,left(b.f_date,7)
有where 没 where 是2个不同的结果你看看
还不能上qq啊。。。
發糞塗牆 2014-01-23
  • 打赏
  • 举报
回复
left join的话,有无where是会有不同啊
xiye_jfb 2014-01-23
  • 打赏
  • 举报
回复
上班不能上QQ

--A create table a(f_line varchar(30),f_produce varchar(30), f_date varchar(30),f_qty int,f_batch int) --B create table b(f_line varchar(30),f_produce varchar(30), f_date varchar(30),f_qty int,f_batch int) insert a select '1005','KX-AAA-R1','2014-01-03',80,28 union all select '1004','KX-BBB-R1','2014-01-05',90,28 insert b select '1005','KX-AAA-R1','2014-01-03',80,28 union all select '1004','KX-CCC-R1','2014-01-05',90,28 
你创建表看看

select a.f_line,a.f_produce,left(a.f_date,7), sum(a.f_qty),b.f_line,b.f_produce,left(b.f_date,7),sum(b.f_qty)   from a left join b on a.f_line = b.f_line and a.f_produce = b.f_produce  and left(a.f_date,7) = left(b.f_date,7)  --where a.f_batch = 28 and b.f_batch = 28    group by a.f_line,a.f_produce,left(a.f_date,7), b.f_line,b.f_produce,left(b.f_date,7)
有where 没 where 是2个不同的结果你看看
發糞塗牆 2014-01-23
  • 打赏
  • 举报
回复
ctrl+m然后执行你的查询
LongRui888 2014-01-23
  • 打赏
  • 举报
回复
引用 9 楼 u012208913 的回复:

--A
create table a(f_line varchar(30),f_produce varchar(30),
f_date varchar(30),f_qty int,f_batch int)
--B
create table b(f_line varchar(30),f_produce varchar(30),
f_date varchar(30),f_qty int,f_batch int)
insert a
select '1005','KX-AAA-R1','2014-01-03',80,28 union all 
select '1004','KX-BBB-R1','2014-01-05',90,28
insert b
select '1005','KX-AAA-R1','2014-01-03',80,28 union all 
select '1004','KX-CCC-R1','2014-01-05',90,28

where a.f_batch = 28 and b.f_batch = 28 加了这代码查的结果就是 1005 KX-AAA-R1 2014-01 80 1005 KX-AAA-R1 2014-01 80 没加就是 1004 KX-BBB-R1 2014-01 90 NULL NULL NULL NULL 1005 KX-AAA-R1 2014-01 80 1005 KX-AAA-R1 2014-01 80 --sql
select a.f_line,a.f_produce,left(a.f_date,7), sum(a.f_qty),b.f_line,b.f_produce,left(b.f_date,7),sum(b.f_qty)  
from a left join b on a.f_line = b.f_line and a.f_produce = b.f_produce  and left(a.f_date,7) = left(b.f_date,7)
 --where a.f_batch = 28 and b.f_batch = 28  
 group by a.f_line,a.f_produce,left(a.f_date,7), b.f_line,b.f_produce,left(b.f_date,7)
不加where我2个表有很多数据 我要where筛选2表的数据啊
有qq不,方便的话,帮你看看
xiye_jfb 2014-01-23
  • 打赏
  • 举报
回复
引用 7 楼 yupeigu 的回复:
另外,你试试这样: update statistics a update statistcis b
这个用上快多了 为什么我加Where后 left join 的效果就没了?
LongRui888 2014-01-23
  • 打赏
  • 举报
回复
引用 8 楼 u012208913 的回复:
[quote=引用 5 楼 DBA_Huangzj 的回复:]
执行计划......

不懂,可否详细 ?[/quote]

xiye_jfb 2014-01-23
  • 打赏
  • 举报
回复

--A
create table a(f_line varchar(30),f_produce varchar(30),
f_date varchar(30),f_qty int,f_batch int)
--B
create table b(f_line varchar(30),f_produce varchar(30),
f_date varchar(30),f_qty int,f_batch int)
insert a
select '1005','KX-AAA-R1','2014-01-03',80,28 union all 
select '1004','KX-BBB-R1','2014-01-05',90,28
insert b
select '1005','KX-AAA-R1','2014-01-03',80,28 union all 
select '1004','KX-CCC-R1','2014-01-05',90,28

where a.f_batch = 28 and b.f_batch = 28 加了这代码查的结果就是 1005 KX-AAA-R1 2014-01 80 1005 KX-AAA-R1 2014-01 80 没加就是 1004 KX-BBB-R1 2014-01 90 NULL NULL NULL NULL 1005 KX-AAA-R1 2014-01 80 1005 KX-AAA-R1 2014-01 80 --sql
select a.f_line,a.f_produce,left(a.f_date,7), sum(a.f_qty),b.f_line,b.f_produce,left(b.f_date,7),sum(b.f_qty)  
from a left join b on a.f_line = b.f_line and a.f_produce = b.f_produce  and left(a.f_date,7) = left(b.f_date,7)
 --where a.f_batch = 28 and b.f_batch = 28  
 group by a.f_line,a.f_produce,left(a.f_date,7), b.f_line,b.f_produce,left(b.f_date,7)
不加where我2个表有很多数据 我要where筛选2表的数据啊
xiye_jfb 2014-01-23
  • 打赏
  • 举报
回复
引用 5 楼 DBA_Huangzj 的回复:
执行计划......
不懂,可否详细 ?
LongRui888 2014-01-23
  • 打赏
  • 举报
回复
另外,你试试这样: update statistics a update statistcis b
LongRui888 2014-01-23
  • 打赏
  • 举报
回复
引用 4 楼 u012208913 的回复:
改成这样试试:
select a.f_linename,a.f_producemodel,left(a.f_date,7),
sum(a.f_qty),b.f_linename,b.f_producemodel,left(b.f_date,7),sum(b.f_qty) 
from a
left hash join b on a.f_linename = b.f_linename and a.f_producemodel = b.f_producemodel 
                 and left(a.f_date,7) = left(b.f_date,7) where a.f_batch = 28 and b.f_batch = 28 
group by a.f_linename,a.f_producemodel,left(a.f_date,7),
b.f_linename,b.f_producemodel,left(b.f_date,7)
效果一样 A表合并后有2888条数据 B表合并后有3444条数据 刚刚的sql查出来有3057条数据 就是inner join的效果 而不是left join的效果 我现在的sql是

select 字段.. into #a from a where 筛选 group by 合并
select 字段.. into #b from b where 筛选 group by 合并
select * from #a a left join #b b on a.f_linename = b.f_linename and 
a.f_producemodel = b.f_producemodel and a.f_date = b.f_date 
这样查询秒速 而且查出2888条数据 这才是left join 
得到这些结果我还有2个表要关联的 用的是inner join 每次的结果都要放在临时表吗?
[/quote] 先把结果集放到临时表,然后再关联,确实能提高查询速度。
發糞塗牆 2014-01-23
  • 打赏
  • 举报
回复
执行计划......
xiye_jfb 2014-01-23
  • 打赏
  • 举报
回复
改成这样试试:
select a.f_linename,a.f_producemodel,left(a.f_date,7),
sum(a.f_qty),b.f_linename,b.f_producemodel,left(b.f_date,7),sum(b.f_qty) 
from a
left hash join b on a.f_linename = b.f_linename and a.f_producemodel = b.f_producemodel 
                 and left(a.f_date,7) = left(b.f_date,7) where a.f_batch = 28 and b.f_batch = 28 
group by a.f_linename,a.f_producemodel,left(a.f_date,7),
b.f_linename,b.f_producemodel,left(b.f_date,7)
[/quote] 效果一样 A表合并后有2888条数据 B表合并后有3444条数据 刚刚的sql查出来有3057条数据 就是inner join的效果 而不是left join的效果 我现在的sql是

select 字段.. into #a from a where 筛选 group by 合并
select 字段.. into #b from b where 筛选 group by 合并
select * from #a a left join #b b on a.f_linename = b.f_linename and 
a.f_producemodel = b.f_producemodel and a.f_date = b.f_date 
这样查询秒速 而且查出2888条数据 这才是left join 
得到这些结果我还有2个表要关联的 用的是inner join 每次的结果都要放在临时表吗?
LongRui888 2014-01-23
  • 打赏
  • 举报
回复
引用 楼主 u012208913 的回复:

A表
f_line f_produce  f_date      f_qty  --大概1W条
1005   KX-AAA-R1  2014-01-03   80
1004   KX-BBB-R1  2014-01-05   90
B表
f_line f_produce  f_date      f_qty  --大概1W条
1005   KX-AAA-R1  2014-01-03   80
1004   KX-CCC-R1  2014-01-05   90
A表和B表其他字段就不打出来了,主要需要数据的字段
--现在的sql
select a.f_linename,a.f_producemodel,left(a.f_date,7),sum(a.f_qty),b.f_linename,b.f_producemodel,left(b.f_date,7),sum(b.f_qty) from a
left join b on a.f_linename = b.f_linename and a.f_producemodel = b.f_producemodel 
and left(a.f_date,7) = left(b.f_date,7) where a.f_batch = 28 and b.f_batch = 28 
group by a.f_linename,a.f_producemodel,left(a.f_date,7),
b.f_linename,b.f_producemodel,left(b.f_date,7)
查询很慢 而且left join 的效果都没体现出来 因为A表和B表每个月的数据有很多重复的 要合计月份数据left join 找出A表存在的数据B表不存在的出来看 请问怎么查询才快
改成这样试试:
select a.f_linename,a.f_producemodel,left(a.f_date,7),
sum(a.f_qty),b.f_linename,b.f_producemodel,left(b.f_date,7),sum(b.f_qty) 
from a
left hash join b on a.f_linename = b.f_linename and a.f_producemodel = b.f_producemodel 
                 and left(a.f_date,7) = left(b.f_date,7) where a.f_batch = 28 and b.f_batch = 28 
group by a.f_linename,a.f_producemodel,left(a.f_date,7),
b.f_linename,b.f_producemodel,left(b.f_date,7)
發糞塗牆 2014-01-23
  • 打赏
  • 举报
回复
回复请引用。
發糞塗牆 2014-01-23
  • 打赏
  • 举报
回复
现有的执行计划贴出来看看,ctrl+M,然后运行你的查询,
發糞塗牆 2014-01-23
  • 打赏
  • 举报
回复
我可能会用between 2013-12-01 00:00:00.000 and 2013-12-31 23:59:59.997这样吧
加载更多回复(7)

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧