Actually I am a little ashamed to be saying this, but I stand corrected about a rather old note I posted on 17-Jul-2007 06:44.
Using SQL_CALC_FOUND_ROWS and FOUND_ROWS( ) will NOT trigger a race condition on MySQL, as that would pretty much defy their entire purpose.
The results for their usage is actually unique per connection session as it is impossible for processes to share anything in PHP. As far as PHP is concerned, each request represents a new connection to MySQL as each request is isolated to its own process.
To simulate this, create the following script:
<?php
$Handle = mysql_connect( "localhost" , "root" , "" );
mysql_select_db( "lls" );
if( isset( $_GET[ 'Sleep' ] ) ) {
mysql_query( "SELECT SQL_CALC_FOUND_ROWS `bid` From `blocks` Limit 1" );
} else {
mysql_query( "SELECT SQL_CALC_FOUND_ROWS `aid` From `access` Limit 1" );
}
if( isset( $_GET[ 'Sleep' ] ) ) {
sleep( 10 ); // Simulate another HTTP request coming in.
$Result = mysql_query( "SELECT FOUND_ROWS( )" );
print_r( mysql_fetch_array( $Result ) );
}
mysql_close( );
?>
Set the connection and query information for something that matches your environment.
Run the script once with the Sleep query string and once again without it. Its important to run them both at the same time. Use Apache ab or something similar, or even easier, just open two browser tabs. For example:
http://localhost/Script.php?Sleep=10
http://localhost/Script.php
If a race condition existed, the results of the first instance of the script would equal the results of the second instance of the script.
For example, the second instance of the script will execute the following SQL query:
<?php
mysql_query( "SELECT SQL_CALC_FOUND_ROWS `aid` From `access` Limit 1" );
?>
This happens while the first instance of the script is sleeping. If a race condition existed, when the first instance of the script wakes up, the result of the FOUND_ROWS( ) it executes should be the number of rows in the SQL query the second instance of the script executed.
But when you run them, this is not the case. The first instance of the script returns the number of rows of its OWN query, which is:
<?php
mysql_query( "SELECT SQL_CALC_FOUND_ROWS `bid` From `blocks` Limit 1" );
?>
So it turns out NO race condition exists, and every solution presented to combat this "issue" are pretty much not needed.
Good Luck,