简体   繁体   中英

How to Improve Linq query perfomance regarding Trim()

Our company tables were created with fields with padded spaces.

I don't have access/permissions to make changes to the DB.

However, I noticed that when I create LINQ queries using the Trim() function, the performance decreases quite a bit.

A query as simple as this shows that performance decrease:

Companies
.Where(c => c.CompanyName.Equals("Apple"))
.Select(c => new {
  Tick = c.Ticker.Trim(),
  Address = c.Address.Trim()
});

Is there a way to change the query so that there is no loss in performance?

Or does this rest solely with my DBA?

Quick solution is to pad your company name before giving it to the query. For example, if the column is char(50) :

var paddedName = "Apple".PadRight(50);
var result = Companies
 .Where(c => c.CompanyName.Equals(paddedName))
 .Select(c => new {
     Tick = c.Ticker.Trim(),
     Address = c.Address.Trim()
 });

However, you should consider correcting the database to avoid further issues.

I haven't try the performance if we use "Like" statement to do first round filter, and make it .ToList(), second round only internally do equal check without call Database.

var result = (Companies
            .Where(c => c.CompanyName.StartsWith("Apple"))
            .Select(c => new
            {
                Tick = c.Ticker.Trim(),
                Address = c.Address.Trim()
            })).ToList();

 var result1=result
            .Where(c=>c.CompanyName.Trim().Equals("Apple")) 
            .Select(c => c);

Other than Entity Framework, linq-to-sql can sometimes switch to linq-to-objects under the hood when it encounters method calls that can't be translated to SQL. So if you do

....
.Select(c => new {
  Tick = c.Ticker.TrimEnd().TrimStart(),
  Address = c.Address.TrimEnd().TrimStart()

you will notice that the generated SQL no longer contains LTRIM(RTRIM()) , but only the field name and that the trims are executed in client memory. Apparently, somehow the LTRIM(RTRIM()) causes a less efficient query plan (surprisingly).

Maybe only TrimEnd() suffices if there are no leading spaces.

Further, I fully agree with pswg that you should go out of your way to try and clean up the database in stead of fixing bad data in queries. If you can't do this job, find the right persons and twist their arms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM