简体   繁体   中英

Linq: SqlFunctions.PatIndex vs string.Contains for string comparisson

Which of the following versions for the same query will perform better:

Version 1 (string.Contains):

var query = db.Products
    .Where( p => p.Description.Contains( description ) );

Version 2 ( SqlFunctions.PatIndex ):

var query = db.Products
    .Where( p => SqlFunctions.PatIndex("%" + description + "%",p.Description) > 0  );

I believe version 1 runs faster theoretically.

Reasons:

  1. Both versions translate into SQL queries eventually.
  2. Version 1 translates into a query with where clause having 'like' operator
  3. Version 2 translate into a query with where clause having 'PatIndex' function.
  4. We know function in SQL will take longer time to return results when comparing with pure 'like' operator in large data set.

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