Update encountered a problem

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.