I have a set of tables with a large row sets (think billions of rows) and I need to get the number of rows count between two parameter values and set two values from the result; I have COUNT(*)
at present.
This will get executed several thousand times against the same table with different values passed in to compare to as part of a larger query where the input parameters change and the row counts in the overall table change by several hundred thousand or even a million rows as the table increases in size.
There may be several (4-5) of the same queries running against this same table at the same time but with different parameter values. The compared value column is indexed and is a single primary key in some tables but in others it may be part of a compound primary key. The returned count will in most cases vary from 500,000 to 1,000,000.
Is there any faster way to get and use count values from a changing range?
!Important, this is NOT for the entire table but for a range within the table thus things like sp_spaceused
are not possible.
SELECT
@newOUT = COUNT(*) ,
@RightValueOUT = COUNT(*)
FROM mydatabase.myschema.myidcolumn
WHERE
myidcolumn >= @MinRowValue
AND myidcolumn <= @MaxRowValue