I recently found a strange problem when I made a statistical function. Code :
Update Pv6
Set Pv0 = ( Select Count ( 0 )
From Clubreply With (Nolock)
Where Rreplydate > = Convert ( Char ( 10 ), Getdate (), 121 ))
Where Title = 1
Select Count ( 0 )
From Clubreply With (Nolock)
Where Rreplydate > = Convert ( Char ( 10 ), Getdate (), 121 ))
The above update will be executed for 2 minutes, and the following will only take 10 seconds!
Note:
Where title = 1: Only one row is found, that is, update only updates one row.
Clubreply is a super large table with tens of millions of rows and rreplaydate is not indexed.
Pv6 is a super small table with three rows and two columns, and this table is rarely queried, so no locks will occur.
I don't understand why I made the following method: Declare @ PV Int ;
Set @ PV = ( Select Count ( * )
From Clubreply With (Nolock)
Where Rreplydate > = Convert ( Char ( 10 ), Getdate (), 121 ));
Update Pv6 Set Pv0 = @ PV Where Title = 1
We found that the task was executed for 2 minutes.
So we changed the implementation of this method. Declare @ Count Int
Select @ Count = Count ( 0 )
From Clubreply With (Nolock)
Where Rreplydate > = Convert ( Char ( 10 ), Getdate (), 121 )
Update Pv6 Set Pv0 = @ Count Where Title = 1
Finally, the execution took 10 seconds.
However, this is all strange. The biggest efficiency problem in this statement seems to be convert (char (10), getdate (), 121). To prove this, optimize the two statements that are executed for 2 minutes. Declare @ Date Datetime ;
Set @ Date = Convert ( Char ( 10 ), Getdate (), 121 );
Update Pv6 Set Pv0 = ( Select Count ( 0 )
From Clubreply With (Nolock)
Where Rreplydate > = @ Date )
Where Title = 1 Declare @ PV Int ;
Declare @ Date Datetime ;
Set @ Date = Convert ( Char ( 10 ), Getdate (), 121 );
Set @ PV = ( Select Count ( * )
From Clubreply With (Nolock)
Where Rreplydate > = @ Date );
Update Pv6 Set Pv0 = @ PV Where Title = 1
The execution time of the preceding two statements is 10 seconds.
Actually, convert is playing tricks.
The final conclusion is as follows: when the SELECT statement is assigned as the result set, the conditions are calculated according to the number of select statements.