last day (15 days later) » 

20:44
room topic changed to T-SQL Optimization: Discussing question: T-SQL Optimization (no tags)
room topic changed to T-SQL Optimization: Discussing question: dba.stackexchange.com/questions/176296/t-sql-optimization (no tags)
Thanks a bunch for taking the time to respond
Just trying to get a handle on INCLUDE
So in the case of this index, where it's one column (totalcount) and INCLUDEs the other 4 columns. Correct me if I'm wrong, when the index is used, the engine seeks on the index and returns many pages (80%) which satisfy the totalcount > 0 clause.
@JacobH I'm not sure I understand your last comment (under the answer, not here)
@JacobH Yes, that's right.
Then from that 80%, the engine does an additional scan on those pages to return the remaining data (we'll still use the 1% number)
But if you were to use the 4 columns in the index and INCLUDE totalcount, the engine would seek the 1% of records in the index and then scan 80% of the 1% to find totalcount > 0
21:00
No, it's one operation. One seek to find where the totalcount>0 starts and then read/scan till the end (of the index).
Ok that makes sense
Then it has to do additional work, for the WHERE and GROUP BY because the data are in rather random order, regarding cgcode, year, month
@JacobH Yes (more or less) for that. It would seek the 1% of the data and then additional work to keep only the 80% of them (or whatever) checking the totalcount>0 and month/year conditions
Right the percentages are estimates I understand that we're just using the numbers for comparison
yes, exactly.
It could 5% instead of 80% and 10% (or 0.01%) for the cgcode. Depends on the data.
And that may change the efficiency of the indexes.
Right right
Ok I just needed to wrap my head around what INCLUDE does
you have been very helpful, thanks for the chat :)
21:04
Welcome
21:47
4
A: T-SQL Optimization

ypercubeᵀᴹFirst of all, I would rewrite this condition. As it is, it can't use any index effectively.: datefromparts(cast(year as char(4)),cast(month as char(2)),'01') < getdate() Rewrite it so there is no functions or calculations applied to the columns: ( year < year(getdate()) or year = year(getda...

Nice answer. Theoretically, would include (id, totalcount) in the index be better performance than the filtered where totalcount > 0? I guess only for this query and not for any that don't have the where clause? It would be interesting to see what the existing index has for columns at the moment.
Index definition has been added
@JacobH right, if there are different conditions (regarding totalcount) used in different queries, then option A is better. If only this one (totalcount > 0) is used, then option B looks much better. It may be much more efficient (space-wise and efficiency-wise) if the number of rows matching the condition is small. And even if it isn't (very small), the index would still store one column less than the include (totalcount) index.
So since the actual index is on totalcount INCLUDE cgcode, year, month, fein how much is the performance difference from cgcode, year, month, feing INCLUDE totalcount? Assuming the same query is run.
@JacobH Ah, about the existing index on the table? The query has to do a full index scan on it (well, a "partial" scan, only for rows with totalcount>0). So, if the number of rows with totalcount>0 is (say) 80%, 80% of the index has to be read. For the (cgcode, year, month, fein, id) INCLUDE (totalcount) - even without my rewrite - only the part that has cgcode = 123 has to be read. If there are 100 distinct cgcodes (and even distribution), only 1% of the index will be read.
21:47
Even when the existing index includes those 4 columns as well? What effect does INCLUDE have in the latter case? A seek on "80% of 1%" instead of a seek on "1% of 80%"? Would be interesting to see if @Kevin3NF tests your index and reports back with the performance increase.
← *7 messages moved from answer

last day (15 days later) »