简体   繁体   English

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

[英]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): 版本1(string.Contains):

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

Version 2 ( SqlFunctions.PatIndex ): 版本2(SqlFunctions.PatIndex):

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

I believe version 1 runs faster theoretically. 我相信版本1在理论上运行得更快。

Reasons: 原因:

  1. Both versions translate into SQL queries eventually. 两个版本最终都会转换为SQL查询。
  2. Version 1 translates into a query with where clause having 'like' operator 版本1转换为where子句具有“like”运算符的查询
  3. Version 2 translate into a query with where clause having 'PatIndex' function. 版本2转换为具有'PatIndex'函数的where子句的查询。
  4. We know function in SQL will take longer time to return results when comparing with pure 'like' operator in large data set. 我们知道在与大型数据集中的纯'like'运算符进行比较时,SQL中的函数将返回结果需要更长的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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