The WHERE clause is the limiting factor of your query using the index.
In a standard SQL Server query, indexes are used either to quickly select records (which this index may indicate) or to restrict the returned records (which this index did not allow). So why does this index not allow you to quickly reduce the limit?
When the query optimizer considers optimizations based on the WHERE clause, it looks for an index that either starts with the element (s) in the WHERE clause, or one that can be used to efficiently identify records that are allowed (or not allowed) in the result set.
Using this index, the server can first find various user identifiers. Then he would like to limit the lines examined in accordance with the WHERE clause. However, for this, the optimizer is likely to appreciate that he will have to carry out the equivalent of a full index or scan the table AFTER finding the user identifiers.
An alternative strategy that may be possible is to scan the index, identify user IDs and dates together. This is what the optimizer chose.
One possible solution for this is a different index - one by date, then a user ID - in addition to the one that is used. This would limit the number of records scanned to identify maximum UserID values ββand therefore be slightly faster.
Note that your index would be fast if you didn't need a WHERE clause. But the where clause requires the optimizer to consider a use case in which the WHERE clause restricts the items selected for the last row examined.
Also, the index in which the Date field was DESCENDING may be more efficient.
source share