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: Tuesday, July 14, 2015 10:23 PM
Points: 32, Visits: 330
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
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Wednesday, August 19, 2015 5:17 AM
Points: 609, Visits: 827
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: Tuesday, July 14, 2015 10:23 PM
Points: 32, Visits: 330
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: 2 days ago @ 3:05 AM
Points: 3,972, Visits: 6,365
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
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: Tuesday, July 14, 2015 10:23 PM
Points: 32, Visits: 330
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: Friday, August 14, 2015 3:35 AM
Points: 2,849, Visits: 4,039
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
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: Today @ 10:14 AM
Points: 3,051, Visits: 4,677
... 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)

"If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them." James Blackburn, in closing argument in the "Fatal Vision" murders trial
Post #1499478
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse