Question about select count(*)

  • Hi,

    I got a question about select (*),

    when someone is executing a transaction that inserts huge number of records, and before it has been committed, will select count(*) get the keep changed record numbers even if (nolock) hint is on?

    Thanks,

    V

  • what i believe that u r asking something like this,

    that transaction that is inserting these huge records has not been committed yet.

    and in the mean time You run your Select Count(*) query,,,,

    If this is the case, then Select will only going to record the COMMITTIED RECORDS FROM THE TABLE. Hence, NO, it will not going to COUNT the UNCOMMITTIED TRANSACTIONS RECORDS.

  • Hi, Guess what, it did count the uncommitted records...

    I've done a test,

    Step 1: run "select count(*) from table1;" -- return 3

    Step 2: begin a transaction to insert 3 records into table 1, without end/rollback the transaction

    Step 3: open another session and run "select count(*) from table1;" there.-- which returned 6

    Step 4: I rollback the transaction;

    Step 5: run "select count(*) from table1;" again. --which returned 3.

    Thanks all for help.

    V

  • sqlnes (9/26/2013)


    Hi, Guess what, it did count the uncommitted records...

    I've done a test,

    Step 1: run "select count(*) from table1;" -- return 3

    Step 2: begin a transaction to insert 3 records into table 1, without end/rollback the transaction

    Step 3: open another session and run "select count(*) from table1;" there.-- which returned 6

    Step 4: I rollback the transaction;

    Step 5: run "select count(*) from table1;" again. --which returned 3.

    Thanks all for help.

    V

    Did you mean WITH(NOLOCK) on step 3?


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Sorry, and yes, I meant with (nolock).

    V

  • sqlnes (9/26/2013)


    will select count(*) get the keep changed record numbers even if (nolock) hint is on?

    Yes . it WILL count the dirty reads .

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • ... will select count(*) get the keep changed record numbers even if (nolock) hint is on?

    Actually, it will count uncommitted records if and only if (nolock) or the equivalent is set on.

    With normal READ COMMITTED isolation level, the count(*) query will wait for the locks to clear before completing.

    Edit: corrected typo.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

Viewing 7 posts - 1 through 6 (of 6 total)

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