Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

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  );
like image 211
E-Bat Avatar asked Mar 21 '23 05:03

E-Bat


1 Answers

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.
like image 106
Jian Huang Avatar answered Apr 25 '23 20:04

Jian Huang