select count(*) from ( select top 100001 ID from T ) x
We need to scan the index to respond to the request. This at least limits the index scan to the first 10,0001 rows. If the table has 1 m rows, this saves 90% of the work.
(Using SQL Server syntax here, please translate because I cannot do this).
Instead of an identifier, you can select some indexed column. I do not know if the optimizer can do this myself.
Note that smart tricks, such as looking at the values ββof an identifier or column identifier, do not work at all.
To support the above query, create a dummy column of type bit and index it. The index will be very compact and the fastest to scan.
usr
source share