T-Sql数据时间年份分组过多的数据

时间:2015-08-07 14:16:06

标签: sql-server sql-server-2008 tsql sql-server-2008-r2

对此查询的回复返回2分钟......

SELECT YEAR(T1.SP_CreateDate) AS DATEYEAR, COUNT(1) AS TOTALCOUNT 
FROM LP_SocialPost T1 WHERE T1.SP_BO_ID = 67
GROUP BY YEAR(T1.SP_CreateDate)


DATEYEAR    TOTALCOUNT
---------|-------------
2015        452036

LP_SocialPost表的总数据计数= 2.8M

如何缩短查询时间?

感谢名单。

2 个答案:

答案 0 :(得分:1)

如果您的桌子上没有索引,请在桌面上创建Non Clustered Index

您可以在SP_BO_ID列上创建包含SP_CreateDate列的索引。此索引将帮助您查询以执行index seek而不是表扫描

CREATE NONCLUSTERED INDEX indexname
  ON LP_SocialPost (SP_BO_ID)
  include (SP_CreateDate) 

答案 1 :(得分:0)

也许......使用分析......

取决于现有索引。

SELECT YEAR(T1.SP_CreateDate) AS DATEYEAR, 
       COUNT(1) over (partition by Year(T1.SP_CreateDate)) AS TOTALCOUNT 
FROM LP_SocialPost T1 
WHERE T1.SP_BO_ID = 67

https://msdn.microsoft.com/en-us/library/ms189461.aspx了解有关分析的更多信息