SQL服务器索引的日期最佳实践

时间:2017-02-06 11:05:35

标签: sql-server indexing

我有一个非常简单的表格,其中包含user_id_project_iddayhours列。一个简单的数据透视查询用于计算每天在每个项目上花费的小时数:

SELECT * 
FROM 
    (SELECT
         hours, project_id,
         LEFT(DATENAME(dw, day), 3) AS workday
     FROM
         activities
     WHERE
         user_id = '" . $user . "' 
         AND DATEPART(wk, day) = '" . $week . "' 
         AND DATEPART(yyyy, day) = '" . $year . "') temp
PIVOT (
                sum([hours])
                for workday in ([mon],[tue],[wed],[thu],[fri])
            ) AS pvt
            ORDER BY project;

$ user,$week$year参数是HTML表单参数。由于DATEPART子句中的WHERE函数,它可以提高性能,以便为表创建额外的列“week”和额外的列“year”并添加两列的索引?或者在日期栏中添加索引是否足够?

0 个答案:

没有答案