Cont () and Where (). Count () sometimes have such a big performance difference!

Source: Internet
Author: User
Tags what sql
Today, when modifying the management List of background users, we found the list loading slow bug! Tens of seconds! Data is a little more than other lists, but there are more than 4000 records. It was good before. Why is it so slow?

Today, when modifying the management List of background users, we found the list loading slow bug! Tens of seconds! Data is a little more than other lists, but there are more than 4000 records. It was good before. Why is it so slow?

I previously added a field to this list to help me prompt the administrator if the product list has a Status field such as modification, so I can conclude that this field is added.

First, let's take a look at how I previously wrote this prompt Status field, add ContentStatus to the object, and then directly add ContentStatus = Product_Maintain.Count (C => C. companyID = company. ID & C. isDeleted = 0 & (C. auditStatus = 0 | C. auditStatus = 4)> 0? "Product updates ":"". At this time, I think it should be added to the three-element operation. During the SQL conversion of linq, there are too many cases, when, then statements, and the addition of the three-element operation will affect the query performance, so after I removed it, I ran to view the page. It was still very slow and I couldn't feel much faster.

Now, I think of LinqPad to see what SQL statements are generated by the conversion. The SQL code generated using Count (condition) is as follows:
The Code is as follows:
Select count (*) AS [value]
FROM (
SELECT
(CASE
WHEN ([t1]. [CompanyID] = ([t0]. [ID]) AND ([t1]. [IsDeleted] = @ p0) AND ([t1]. [AuditStatus] = @ p1) OR ([t1]. [AuditStatus] = @ p2) THEN 1
When not ([t1]. [CompanyID] = ([t0]. [ID]) AND ([t1]. [IsDeleted] = @ p0) AND ([t1]. [AuditStatus] = @ p1) OR ([t1]. [AuditStatus] = @ p2) THEN 0
ELSE NULL
END) AS [value]
FROM [Company_Product_Maintain] AS [t1]
) AS [t2]
WHERE [t2]. [value] = 1


At this time, I found a very simple Count SQL statement, which became so complex after the linq conversion. I ran this Code directly in SQL server and found that the query was still very slow, so I directly put ContentStatus = Product_Maintain.Where (C => C. companyID = company. ID & C. isDeleted = 0 & (C. auditStatus = 0 | C. auditStatus = 4 )). the SQL statement generated by Count () is:

Code
The Code is as follows:
Select count (*) AS [value]
FROM [GasSNS_Company_Equipment_Maintain] AS [t1]
WHERE ([t1]. [CompanyID] = ([t0]. [ID]) AND ([t1]. [IsDeleted] = @ p0) AND ([t1]. [AuditStatus] = @ p1) OR ([t1]. [AuditStatus] = @ p2 ))

It's an order of magnitude faster!
The query result speed of the background list is greatly improved as shown in the following figure ():

The result is Count. It took 35 seconds. Wow!

The result is Where (condition). Count (). The same data takes only 4 seconds, 10 times worse!

Then, for the value, I add the Three-element operation ContentStatus = Product_Maintain.Where (C => C. companyID = company. ID & C. isDeleted = 0 & (C. auditStatus = 0 | C. auditStatus = 4 )). count ()> 0? "Product updates ":"". The result is as follows:

It's really the difference between Count () and Where (). Isn't that big difference possible? So I write Product_Maintain.Where (C => C. isDeleted = 0 & (C. auditStatus = 0 | C. auditStatus = 4 )). count () and Product_Maintain.Count (C => C. isDeleted = 0 & (C. auditStatus = 0 | C. auditStatus = 4) The generated code is the same as the speed.
The Code is as follows:
Select count (*) AS [value]
FROM [GasSNS_Company_Equipment_Maintain] AS [t0]
WHERE ([t0]. [IsDeleted] = @ p0) AND ([t0]. [AuditStatus] = @ p1) OR ([t0]. [AuditStatus] = @ p2 ))

It turns out that if I retrieve the number of tables in the Select statement and use a variable after the from statement in the condition, Count (condition) and Where (condition) are used ). only when Count () is generated can the query speed be different by an order of magnitude.
Code
The Code is as follows:
// Low efficiency version:
From company in Company
Select new
{
Contacter = v. ContacterID,
Count = Product_Maintain.Count (C => C. companyID = company. ID & C. isDeleted = 0 & (C. auditStatus = 0 | C. auditStatus = 4 ))
}

And
The Code is as follows:
// High Efficiency version:
From company in Company
Select new
{
Contacter = v. ContacterID,
Count = Product_Maintain.Where (C => C. companyID = company. ID & C. isDeleted = 0 & (C. auditStatus = 0 | C. auditStatus = 4 )). count ()
}

Otherwise, the SQL statements generated by Count () and Where (). Count () are the same and the efficiency is the same. To sum up, we hope you will pay attention to it later! I entered the park for two years, the first time on the home page, please read the official advice! All the above query charts are the result of the LinqPad query. As for the LinqPad query time in about 4 seconds, the SQL statement generated by Linq cannot be executed within 1 second in SQL Server. The following is an explanation:

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.