开发者

Are there any better search methods instead of string.Contains("keyword") using asp.net?

For example, the users of our app wa开发者_如何学Pythonnt 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.


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.


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 + '%'


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...


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜