获取每个用户的最大日期记录并将其插入临时 table - 性能问题
Getting record with max date per user and inserting it to temp table - performance issue
我需要从语句 table 中获取每个用户帐户的最大日期并插入临时文件 table。语句 tables 有超过 4000 万条记录。我尝试了以下查询,但花了 4 分钟多的时间。有没有更好的方法来做到这一点?
select useraccount, max(date)
into #temptable
from statement
group by useraccount
分组可能很昂贵。 4 分钟对于处理和创建一个大的 table 似乎并不坏。但是如果你在 (useraccount, date)
上有一个索引,你可以尝试:
select useraccount, date
into #temptable
from statement s
where date = (select max(s2.date) from statement s2 where s2.useraccount = s.useraccount);
尝试创建一个包含用户帐户和日期列的非聚集列存储索引,如下所示
CREATE NONCLUSTERED COLUMNSTORE INDEX [NCCIX_statement_useraccount_date] ON [dbo].[statement]
(
[useraccount]
,[date]
)WITH (DROP_EXISTING = OFF, COMPRESSION_DELAY = 0)
GO
我需要从语句 table 中获取每个用户帐户的最大日期并插入临时文件 table。语句 tables 有超过 4000 万条记录。我尝试了以下查询,但花了 4 分钟多的时间。有没有更好的方法来做到这一点?
select useraccount, max(date)
into #temptable
from statement
group by useraccount
分组可能很昂贵。 4 分钟对于处理和创建一个大的 table 似乎并不坏。但是如果你在 (useraccount, date)
上有一个索引,你可以尝试:
select useraccount, date
into #temptable
from statement s
where date = (select max(s2.date) from statement s2 where s2.useraccount = s.useraccount);
尝试创建一个包含用户帐户和日期列的非聚集列存储索引,如下所示
CREATE NONCLUSTERED COLUMNSTORE INDEX [NCCIX_statement_useraccount_date] ON [dbo].[statement]
(
[useraccount]
,[date]
)WITH (DROP_EXISTING = OFF, COMPRESSION_DELAY = 0)
GO