Access SQL - SUM(IIF... vs IIF(SUM... performance implication

时间:2017-06-15 10:04:04

标签: sql ms-access

I have looked online but have been unable to find information about this.

I am just wondering which of the following SQL queries would execute most quickly for a large dataset in MS Access:

SELECT SUM(IIF([Field] = 0, null, [Field])) AS SumField
FROM tbl1

or

SELECT IIF(SUM([Field]) = 0, null, Sum([Field])) AS SumField
FROM tbl1

2 个答案:

答案 0 :(得分:1)

To get a definitive answer, you would need to find some documentation stating whether or not that sum will be executed twice in your second script or not.

Failing that, you can simply take the safe course of action, which is your first query that will guarantee the sum is only calculated the once.

答案 1 :(得分:1)

The two do different things. The first adds numbers that are not zero. The result can be zero (for instance, if the values are -1 and 1). The second can never by zero.

In any case, the difference in performance is going to be minimal. There is perhaps a wee amount of overhead for calculating the IFF() for every row, but the effort to do the aggregation should swamp that. And, a wee amount of overhead for calculating the SUM() twice in the second case.

The more important issue is getting the logic correct. Also, the first is equivalent to:

SELECT SUM([Field]) as sumField
FROM tbl;
相关问题