简体   繁体   English

在ASP.NET MVc Web应用程序中如何使用.contains进行搜索和自动完成搜索

[英]How scalable is using .Contains for searching and auto-complete search in asp.net MVc web applications

i have found a lot of tutorials and books that implements the auto-complete search in MVC web applications as in :- 我发现很多教程和书籍可以在MVC Web应用程序中实现自动完成搜索,如:-

public ActionResult ArtistSearch(string q)
{
var artists = GetArtists(q);
return PartialView(artists);
}
private List<Artist> GetArtists(string searchString)
{
return storeDB.Artists
.Where(a => a.Name.Contains(searchString))
.ToList();
}

but this raised a question of how much is this approach scalable in real applications that might have thousands of records ???,, so will using Contains() scale well Or there is a much better approach?? 但这提出了一个问题,即该方法在可能具有成千上万条记录的实际应用程序中可扩展多少???因此,使用Contains()可以很好地扩展吗?还是有更好的方法? BR BR

If I remember correctly string.Contains() is translated into a LIKE Query with a wildcard on each side of the query string. 如果我没记错的话, string.Contains()会转换为LIKE查询,在查询字符串的每一侧都带有通配符。 This makes it very difficult/impossible to use an index, so you can expect your performance to be O(n) on your dataset since SQL Server does a full table scan (see Does SQL Server optimize LIKE ('%%') query? ). 这使使用索引非常困难/不可能,因此由于SQL Server进行了全表扫描,因此可以期望数据集的性能为O(n)(请参阅SQL Server是否优化LIKE('%%')查询? )。

To optmize your query, you might want to take a look at the Full-Text Indexing Capabilities, more info here: SQL Server: how to optimize "like" queries? 要优化查询,您可能想看看全文索引功能,更多信息请参见: SQL Server:如何优化“喜欢”查询? ). )。

If you could use .StartsWith instead of .Contains you will have a LIKE query with a wildcard at the end, and you can use an Index on the queried column to get fast lookups (be sure to check the query execution plan!). 如果可以使用.StartsWith而不是.Contains,则将有一个LIKE查询,该查询的末尾有一个通配符,并且可以在查询列上使用Index来快速查找(一定要检查查询执行计划!)。

I guess you will have much better perceived performance if you try to focus on the UX of your auto-complete feature: Start auto-complete search after a short lock period (when the user stops typing), and make sure it doesn't block (happens in the background). 我想,如果您专注于自动完成功能的用户体验,您会获得更好的性能:在较短的锁定时间(用户停止键入时)之后开始自动完成搜索,并确保它不会被阻止(发生在后台)。

It depends on the data your serving. 这取决于您提供的数据。 In a "real world" application, you need to think about noise/stop words ("and", "the"), abbreviations ("st" for "street"), etc and difficult languages. 在“现实世界”应用程序中,您需要考虑噪音/停用词(“ and”,“ the”),缩写词(“ street”的“ st”)等,以及困难的语言。

In these scenarios, .Contains won't fit the bill, and you'll need to employ a full-text search indexing engine, such as Lucene.NET or SQL Server Full Text Search. 在这些情况下, .Contains不合适,因此您需要使用全文搜索索引引擎,例如Lucene.NET或SQL Server全文搜索。

Substring queries cannot use index seeks. 子字符串查询不能使用索引查找。 But they still can use indexes. 但是他们仍然可以使用索引。 Scanning a few thousand records is really nothing if you don't do it too often. 如果您不经常扫描几千条记录,那实际上是没有用的。

So I recommend that you create an index on Name and you'll be fine for an awful lot of data. 因此,我建议您在Name上创建一个索引,这样可以处理大量数据。

Looks like you are using LINQ with the Entity Framework. 看起来您正在将LINQ与Entity Framework一起使用。 LINQ gets converted into SQL and the call to contains gets converted into a LIKE WHERE clause, so you can simply run SELECT * FROM Artists WHERE Name LIKE '%whatever%' to get an idea about performance. LINQ被转换为SQL,并且对contains的调用被转换为LIKE WHERE子句,因此您可以简单地运行SELECT * FROM Artists WHERE Name LIKE '%whatever%'以获得有关性能的信息。

Note there are a couple of things you can do to reduce the impact. 请注意,您可以采取几种措施来减少影响。 One you can limit the number of results .Take(20) . 您可以限制一个结果数.Take(20) Also you can wait until the user takes at least a couple of characters before triggering auto complete. 您也可以等到用户至少输入几个字符后再触发自动完成。 Finally you can 'throttle' the call to auto complete, so that you don't call auto complete every time they type a character, instead of wait until they go say a half a second without typing an additional character. 最后,您可以“限制”自动完成功能的调用,这样您就不必在每次输入字符时都调用自动完成功能,而不必等到他们说半秒钟而不输入其他字符。

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

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