Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Question about select count(*) Expand / Collapse
Author
Message
Posted Thursday, September 26, 2013 9:57 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 10:00 PM
Points: 32, Visits: 309
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
Post #1499161
Posted Thursday, September 26, 2013 10:18 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, October 17, 2014 1:33 AM
Points: 485, Visits: 625
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.
Post #1499168
Posted Thursday, September 26, 2013 11:49 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 10:00 PM
Points: 32, Visits: 309
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.[color=#FF0000]-- which returned 6[/color]
Step 4: I rollback the transaction;
Step 5: run "select count(*) from table1;" again. --which returned 3.

Thanks all for help.
V
Post #1499192
Posted Friday, September 27, 2013 12:08 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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.[color=#FF0000]-- which returned 6[/color]
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1499194
Posted Friday, September 27, 2013 5:08 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 10:00 PM
Points: 32, Visits: 309
Sorry, and yes, I meant with (nolock).

V
Post #1499312
Posted Friday, September 27, 2013 5:16 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 6, 2014 5:46 AM
Points: 2,840, Visits: 3,969
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
Post #1499313
Posted Friday, September 27, 2013 9:47 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:18 PM
Points: 2,192, Visits: 3,299
... 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)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #1499478
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse