9

For example, the users of our app want to search products by entering a keyword of productName.

The Products table of the sql server database contains about 10 million records.

Are there any better and higher-performance search methods to implement instead of productName.Contains("keyword") using asp.net C# ?

I'm using stored procedures now, but linq to sql or entity framework are also a possibility.

w4ik
  • 1,276
  • 2
  • 19
  • 33
Mike108
  • 2,089
  • 7
  • 34
  • 45

4 Answers4

5

If you want better performance then you could look into a full text search. Note that this is currently not supported by LINQ to SQL but you can write the SQL in a stored procedure and call the stored procedure from your application. The accepted answer for this StackOverflow post recommends reading this article.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
3

Well you could achieve this from the db side using a LIKE

LIKE (Transact-SQL)

Something like

DECLARE @Table TABLE(
        Val VARCHAR(50)
)

INSERT INTO @Table SELECT 'asdf'
INSERT INTO @Table SELECT 'tada'
INSERT INTO @Table SELECT 'foo'
INSERT INTO @Table SELECT 'bar'
INSERT INTO @Table SELECT 'test'
INSERT INTO @Table SELECT 'test foo'

DECLARE @Lookup VARCHAR(50)
SELECT @Lookup = 'foo'

SELECT  *
FROM    @Table
WHERE   Val LIKE '%' + @Lookup + '%'
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
1

Might be overkill but check out lucene.net, I believe it is what stack overflow uses for its searching

Daniel Powell
  • 8,143
  • 11
  • 61
  • 108
0

You should never fetch more data from your database than you need. So it is best for you to restrict your rows inside the data layer (ie. your stored procedure). You can do this by only returning matching rows:

SELECT * FROM Products WHERE ProductName LIKE '%' + @keyword + '%'

Where @keyword is a parameter to your store procedure.

But be aware that performance using LIKE can be slow, since it must read the entire table. You can better performance more by matching products that starts with the keyword:

SELECT * FROM Products WHERE ProductName LIKE @keyword + '%'

Note I removed the '%' before @keyword. This enables SQL Server to use an index on the ProductName column to find the rows much quicker!

Fore further help, please post your stored procedure...

Alex
  • 2,011
  • 3
  • 21
  • 27