SELECT COUNT WITH CONDITION IN OPTMISE WAY

  • Hello All

    I just want to fetch the count of records in a table with a condition in optmised manner.

    Thanks you all

  • A filtered index might do the trick, but only for the condition defined to filter the index. With that, you simply query sys.dm_db_partition_stats, something like this:

    SELECT SUM (row_count)

    FROM sys.dm_db_partition_stats s

    JOIN sys.indexes i ON i.object_id = s.object_id AND i.index_id = s.index_id

    WHERE i.name = 'Index_Name';

    Otherwise, the only option is to use a COUNT(*). A proper indexed table shouldn't have a problem with a query like that.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply