【发布时间】:2018-05-07 13:42:46
【问题描述】:
以下查询大约需要 30 秒才能给出结果。 table1 包含~20m 行 table2 包含 ~10000 行
我正在努力寻找提高性能的方法。有什么想法吗?
declare @PreviousMonthDate datetime
select @PreviousMonthDate = (SELECT DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()) - 1, '19000101') as [PreviousMonthDate])
select
distinct(t1.code), t1.ent, t3.lib, t3.typ from table1 t1, table2 t3
where (select min(t2.dat) from table1 t2 where t2.code=t1.code) >@PreviousMonthDate
and t1.ent in ('XXX')
and t1.code=t3.cod
and t1.dat>@PreviousMonthDate
谢谢
【问题讨论】:
-
你使用的是哪个数据库??
-
一些样本数据会很好。马上,如果你能取消那些相关的子查询,你可能会更好。
-
@TimBiegeleisen 谢谢 .. 我没有注意到 ..
-
@scaisEdge,我正在使用 sybase
-
DISTINCT是SELECT DISTINCT的一部分,适用于整个选定行。跳过那些多余的括号,写select distinct t1.code, t1.ent, ...让事情更清楚。
标签: sql subquery aggregate-functions sybase query-performance