counting rows in a table

  • I have a table A with a non-clustered index on it.

    when I use "select rows from sysindexes where id=object_id('table A') and indid<2" it got a number for row count. Then I used "select count(*) from table A (nolock)" and I got a different number (a few hundred less).

    Is it supposed to do that? Has anybody got such different results.

  • Sysindexes contains an approximate row count and is accurate right after statistics are updated.  If you need to know EXACTLY how many rows are in a table count() is the answer.


    And then again, I might be wrong ...
    David Webb

  • yep you need to rebuild indexs and recompute stats if you want to use the no in sysindexes. Whats wrong with using count?

    www.sql-library.com[/url]

  • even after updating statistics the numbers are not matching. The table has 75 columns and row size is 842 bytes. there are around 1,000,000 rows in the table. I have no problem using count(*) to get row count. I am just curious as to why the other way is not working for me.

  • Stab in the dark here :

    Have you tried it with this column instead : rowcnt

  • both fields have the same number.

  • How about this if you're not on a production server :

    sp_updatestats

    DBCC DROPCLEANBUFFERS

    DBCC FREEPROCCACHE

    Rerun both queries.

  • i dont have permission to execute dropcleanbuffers or freeproccache

  • Well I'm out of ideas then...

    BTW, why can't you run that command on a dev server??

  • my login does not have permission to run those commands on the server. I am not a member of the sysadmin role. thanks for the help. in the future, i will use count(*) rather than sysindexes just to be safe.

  • When you are using the NOLOCK hint, you will often see consistency errors. Using NOLOCK you can, under some circumstances, read the same row multiple times, miss rows completly etc. When you use NOLOCK, in effect you are telling SQL Server that you aren't that concerned with the accuracy of the results.

    This may not be the problem, but it's worth keeping in mind.

    SQL guy and Houston Magician

Viewing 11 posts - 1 through 10 (of 10 total)

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