简体   繁体   English

LINQ查询到Azure SQL数据库超时

[英]LINQ query to Azure SQL database timing out

I'm querying my sql database which is in Azure (actually my web app is on Azure as well). 我正在查询位于Azure中的sql数据库(实际上我的Web应用程序也位于Azure上)。

Every time I perform this particular query, there are ever changing errors (eg sometimes timeout occurs, sometimes it works perfectly, sometimes it takes extremely long to load). 每次执行此特定查询时,错误都会不断变化(例如,有时会发生超时,有时会运行良好,有时会花费非常长的时间)。

I have noted that I am using the ToList method here to enumerate the query but I suspect that's why it is degrading. 我已经注意到我在这里使用ToList方法来枚举查询,但是我怀疑这就是它降级的原因。

Is there anyway I can fix this or make it better.... or maybe just use native SQL to execute my query? 无论如何,我可以解决这个问题或使其变得更好...., 或者仅使用本机SQL执行查询吗? .

I should also note in my webconfig my Database connection timeout is set to 30 seconds. 我还应该注意,在我的webconfig中,我的数据库连接超时设置为30秒。 Would this have any performance benefit? 这会对性能有好处吗?

I'm putting the suspect code here: 我将可疑代码放在这里:

case null:    
lstQueryEvents = db.vwTimelines.Where(s => s.UserID == UserId)
                    .Where(s => s.blnHide == false)
                    .Where(s => s.strEmailAddress.Contains(strSearch) || s.strDisplayName.Contains(strSearch) || s.strSubject.Contains(strSearch))
                    .OrderByDescending(s => s.LatestEventTime)
                    .Take(intNumRecords)
                    .ToList();
                    break;

It's basically querying for the 50 records...I don't understand why it's timing out sometimes. 它基本上是在查询50条记录...我不明白为什么有时会超时。

Here are some tips: 这里有一些提示:

Make sure that your SQL data types matches types in your model 确保您的SQL数据类型与模型中的类型匹配

Judging by your code, types should be something like this: 从您的代码来看,类型应该是这样的:

  • UserID should be int (cannot tell for sure by looking at code); UserID应该为int (无法通过查看代码确定);
  • blnHide should be bit ; blnHide应该bit ;
  • strEmailAddress should be nvarchar ; strEmailAddress应该为nvarchar
  • strDisplayName should be nvarchar ; strDisplayName应该为nvarchar
  • strSubject should be nvarchar ; strSubject应该是nvarchar ;

Make use of indexes 利用索引

You should create Non-Clustered Indexes on columns that you use to filter and order data. 您应该在用于过滤和排序数据的列上创建非聚集索引。

In order of importance: 按重要性顺序:

  • LatestEventTime as you order ALL data by this column; 您通过此列对所有数据进行排序时的LatestEventTime
  • UserID as you filter out most of data by this column; 您通过此列过滤掉大部分数据的UserID
  • blnHide as you filter out part of data by this column; 当您通过此列过滤掉部分数据时, blnHide ;

Make use of indexes for text lookup 利用索引进行文本查找

You could make use of indexes for text lookup if you change your filter behaviour slightly and search text only in the start of column value. 如果稍微更改过滤器行为并仅在列值的开头搜索文本,则可以使用索引进行文本查找。

To achieve that: 为实现这一目标:

  • change .Contains() with .StartsWith() as it would allow index to be used. .StartsWith()更改.Contains() ,因为它将允许使用索引。
  • create Non-Clustered Indexes on strEmailAddress column: strEmailAddress列上创建非聚集索引:
  • create Non-Clustered Indexes on strDisplayName column: strDisplayName列上创建非聚集索引:
  • create Non-Clustered Indexes on strSubject column: strSubject列上创建非聚集索引:

Try out free text search 试用免费文本搜索

Microsoft only recently have introduced full text search in Azure SQL. Microsoft仅在最近才在Azure SQL中引入了全文搜索。 You can use that to find rows matching by partial string. 您可以使用它来查找按部分字符串匹配的行。 This is a bit complicated to achieve using EF, but it is certainly doable. 使用EF来实现这有点复杂,但是肯定是可行的。

Here are some links to get you started: 以下是一些帮助您入门的链接:

Entity Framework, Code First and Full Text Search https://azure.microsoft.com/en-us/blog/full-text-search-is-now-available-for-preview-in-azure-sql-database/ 实体框架,代码优先和全文搜索 https://azure.microsoft.com/zh-cn/blog/full-text-search-is-now-available-for-preview-in-azure-sql-database/

string.Contains(...) converted to WHERE ... LIKE ... sql-statement. string.Contains(...)转换为WHERE ... LIKE ... sql语句。 Which is very expensive. 这是非常昂贵的。 Try to reform your query to avoid it. 尝试改革您的查询以避免它。 Plus, Azure SQL has it's own limitations (5 sec as far as I remember, but better check SLA) for query run, so it would generally ignore your web.config settings if they are longer. 另外,Azure SQL有它自己的限制(据我所知,只有5秒,但最好检查SLA)以进行查询运行,因此,如果较长,则通常会忽略web.config设置。

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

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