Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用年份()时的SQL Server 2008索引方法_Sql Server_Sql Server 2008_Tsql_Indexing - Fatal编程技术网

Sql server 使用年份()时的SQL Server 2008索引方法

Sql server 使用年份()时的SQL Server 2008索引方法,sql-server,sql-server-2008,tsql,indexing,Sql Server,Sql Server 2008,Tsql,Indexing,我有一个“customertransactions”表,其列“transactiondate”类型为DateTime 我将用以下内容查询它: SELECT SUM(balance) AS totalbal FROM customertransactions WHERE accountcode=? AND (MONTH(GETDATE())-MONTH(transactiondate)+12*(YEAR(GETDATE())-YEAR(transactiondate)))>= 3 。。。显

我有一个“customertransactions”表,其列“transactiondate”类型为DateTime

我将用以下内容查询它:

SELECT SUM(balance) AS totalbal FROM customertransactions WHERE accountcode=?
AND (MONTH(GETDATE())-MONTH(transactiondate)+12*(YEAR(GETDATE())-YEAR(transactiondate)))>= 3
。。。显然,正在为“accountcode”传递经过清理的参数

我的问题是-我如何最好地创建一个指数来优化它


主要是,我会考虑索引<代码>会计准则< /代码>。此外,如果您可以重写date子句,使其保持原样,那么您也可以从索引
transactiondate
中获益


一如既往,考虑数据的基数,并在添加索引时检查查询计划。没有硬性规定。

你是说“交易超过3个月”吗?那么您的代码是错误的,应该是
其中datediff(m,transactiondate,getdate())>=3
。您确定在尝试调整索引之前不能更好地编写查询吗?商业规则是什么?@GSerg实际上,你的WHERE子句不是sargable。应该这样写:
where transactiondate OK,采纳GSerg的建议,那么它只是‘transactiondate’上的一个严格的非聚集索引?@AlanB是的,但也采纳了RedFilter的建议。