为 Dataverse 撰写优化查询对于确保应用程序提供快速、响应迅速且可靠的体验至关重要。 了解在使用 RetrieveMultiple 消息或包含从 QueryBase 类继承的参数的消息时,请了解应避免的查询反模式。
使用 OData 针对记录集合发送 GET 请求时,本指南也适用。
Note
本文中的指南可能不适用于 Elastic 表 或使用 Dataverse 搜索时。
最小化所选列数
不要在查询中包含不需要的列。 返回所有列或包含大量列的查询可能会因数据集的大小或查询的复杂性而遇到性能问题。
最小化所选逻辑列数
避免请求过多列,尤其是 逻辑列。 逻辑列包含存储在不同数据库表中的值。 AttributeMetadata.IsLogical 属性告知列是否为逻辑列。 包含许多逻辑列的查询速度较慢,因为 Dataverse 需要合并来自其他数据库表的数据。
避免在筛选条件中使用前导通配符
将条件与前导通配符(显式使用,或借助类似 ends-with 这样的运算符隐式使用)一起使用的查询可能会导致性能不佳。 当查询使用前导通配符时,Dataverse 不能利用数据库索引,这会强制 SQL 扫描整个表。 即使还有其他限制结果集的非前导通配符查询,表扫描也会发生。
以下示例是使用前导通配符的 FetchXml 条件元素 :
<condition attribute='accountnumber'
operator='like'
value='%234' />
当查询超时并检测到此模式时,Dataverse 将返回唯一错误,以帮助识别使用此模式的查询:
名称:
LeadingWildcardCauseTimeout
代码:0x80048573
编号:-2147187341
消息:The database operation timed out; this may be due to a leading wildcard value being used in a filter condition. Please consider removing filter conditions on leading wildcard values, as these filter conditions are expensive and may cause timeouts.
Dataverse 严重限制 前导通配符查询,这些查询被识别为对环境运行状况构成风险,以帮助防止中断。 当查询因限流而失败且检测到此模式时,Dataverse 会返回一个独特的错误:
名称:
DataEngineLeadingWildcardQueryThrottling
代码:0x80048644
编号:-2147187132
消息:This query cannot be executed because it conflicts with Query Throttling; the query uses a leading wildcard value in a filter condition, which will cause the query to be throttled more aggressively.
或者
This query is throttled as it negatively impacts the database health; the query uses a leading wildcard value in a filter condition, which will cause the query to be throttled more aggressively.
如果使用前导通配符查询,请考虑以下选项:
- 请改用 Dataverse 搜索 。
- 更改数据模型以帮助人们避免使用前导通配符值。
其他通配符
如在字符串值的条件中使用通配符所述,除百分号 (%) 字符之外的其他字符也可能充当通配符。 以下查询字符串的作用也类似于前导通配符:
_234%[^a]234%
对于以这些其他开头通配符特殊字符开头的搜索字符串,Dataverse 会对查询实施严格限流。
连字符和撇号(单引号)字符
数据库排序规则中的 Unicode 排序规则会导致某些以连字符 (-) 或撇号 (') 开头的搜索字符串表现得像以通配符开头的搜索。 如果搜索字符串在字符串中出现 % 字符之前不包含非wildcard 字符,则以连字符或撇号开头的搜索字符串不能利用数据库索引。 例如,-%、-%234、'%751和'%无法有效地使用数据库索引,而-234%和'751%可以有效地使用数据库索引。 Dataverse 严重限制以连字符或撇号开头的低效搜索字符串。 若要详细了解数据库排序规则中有关连字符和撇号的 Unicode 排序规则,请参阅SQL Server 排序规则。
避免在筛选条件中使用公式或计算列
公式和计算列值会在检索时实时计算。 使用这些列上的筛选器的查询会强制 Dataverse 计算可返回的每个可能记录的值,以便应用筛选器。 查询速度较慢,因为 Dataverse 无法使用 SQL 提高这些查询的性能。
当查询超时并检测到此模式时,Dataverse 将返回唯一错误,以帮助识别使用此模式的查询:
名称:
ComputedColumnCauseTimeout
代码:0x80048574
编号:-2147187340
消息:The database operation timed out; this may be due to a computed column being used in a filter condition. Please consider removing filter conditions on computed columns, as these filter conditions are expensive and may cause timeouts.
Dataverse 会严重限制 对已识别为环境运行状况风险的计算列的筛选器的查询,以帮助防止中断。 当查询因限流而失败且检测到此模式时,Dataverse 会返回一个独特的错误:
名称:
DataEngineComputedColumnQueryThrottling
代码:0x80048744
编号:-2147186876
消息:This query cannot be executed because it conflicts with Query Throttling; the query uses a computed column in a filter condition, which will cause the query to be throttled more aggressively.
或者
This query is throttled as it negatively impacts the database health; the query uses a computed column in a filter condition, which will cause the query to be throttled more aggressively.
避免按选项列排序
使用 FetchXml 或 QueryExpression 时,按选择列对查询结果进行排序时,将按每个选项的本地化标签对结果进行排序。 按数据库中存储的数字值进行排序不会在应用程序中提供良好的体验。 对选择列进行排序需要更多计算资源,以便按本地化标签值联接和排序行。 此额外工作会使查询变慢。 如果可能,请尝试避免按选择列值对结果进行排序。
Note
OData 则不同。 通过使用 Dataverse Web API,$orderby 会根据选择列的整数值对行进行排序,而不是根据本地化标签。
避免按相关表中的列排序
按相关表的列排序会使查询速度变慢,因为增加了复杂性。
仅在需要时才能按相关表排序,如以下部分所述:
避免对大型文本列使用条件
Dataverse 有两种类型的列,可以存储大型文本字符串:
- StringAttributeMetadata 最多可以存储 4,000 个字符。
- MemoAttributeMetadata 可以存储更大的数值。
这两列的限制由 MaxLength 属性指定。
对 MaxLength 长度设置为少于 850 个字符的字符串列使用条件。
所有备忘录列或 MaxLength 长度大于 850 的字符串列在 Dataverse 中均被定义为大文本列。 大型文本列太大,无法有效地编制索引,这会导致将其包含在筛选条件中时性能不佳。
Dataverse 搜索 是查询这些列中的数据的更好选择。
反模式导致的查询超时的 Dataverse 错误
当查询超时并使用此页上详述的反模式之一时,Dataverse 将返回以下唯一错误。 此错误可帮助你确定查询使用的反模式:
名称:
PerformanceValidationIssuesCauseTimeout
代码:0x80048575
编号:-2147187339
消息:The database operation timed out; this may be due to the query performance issues identified in a query executed on this request. Please optimize the query by addressing the following identified performance issues: {0}. Please reference this document for guidance: https://go.microsoft.com/fwlink/?linkid=2300520
{0}异常消息的一部分列出了查询正在使用的反模式。 如果查询使用了多个反模式,则会用逗号分隔。 例如,如果查询正在筛选大型文本列,并且还选择大量列,则异常消息将包含字符串 PerformanceLargeColumnSearch,LargeAmountOfAttributes。 下表列出了包含说明链接的反模式:
| 反模式标识符 | 说明链接 |
|---|---|
PerformanceLeadingWildCard |
避免在筛选条件中使用前导通配符 |
PerformanceLargeColumnSearch |
避免对大文本列使用条件 |
OrderOnEnumAttribute |
避免按选择列排序 |
OrderOnPropertiesFromJoinedTables |
避免按相关表中的列排序 |
LargeAmountOfAttributes |
最小化所选列数 |
LargeAmountOfLogicalAttributes |
最小化所选逻辑列数 |
FilteringOnCalculatedColumns |
避免在筛选条件中使用公式或计算列 |
使用此页上的指南了解反模式。 修改查询以避免使用这些反模式。
Note
如果查询包含 PerformanceLeadingWildCard 或 FilteringOnCalculatedColumns 反模式之一,Dataverse 将引发不同的错误。 使用 PerformanceLeadingWildCard 反模式的查询将引发此页上提到的 LeadingWildcardCauseTimeout 错误。 使用 FilteringOnCalculatedColumns 反模式的查询将引发本页提到的 ComputedColumnCauseTimeout 错误。
LeadingWildcardCauseTimeout 和 ComputedColumnCauseTimeout 错误早于 PerformanceValidationIssuesCauseTimeout 错误。 为了保持向后兼容性,Dataverse 继续抛出 LeadingWildcardCauseTimeout 和 ComputedColumnCauseTimeout。
相关文章
查询限制
在字符串值的条件中使用通配符
使用 FetchXml 查询数据
使用 QueryExpression 查询数据
使用 OData 查询数据