Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
partition 运算符根据键列中的值将其输入表的记录分区为多个子表。 该运算符对每个子表运行一个子查询,并生成单个输出表,该表是所有子查询结果的并集。
当需要仅对属于同一分区键的行的子集执行子查询,而不是整个数据集的查询时,分区运算符非常有用。 这些子查询可以包括聚合函数、窗口函数、top N 函数等。
partition 运算符支持多种子查询操作策略:
Syntax
T|partition [ hint.strategy=策略 ] [ 提示 ] by列(转换SubQuery)
T|partition [ hint.strategy=legacy ] [ 提示 ] byColumn{SubQueryWithSource}
详细了解语法约定。
Parameters
| Name | 类型 | Required | Description |
|---|---|---|---|
| T | string |
✔️ | 输入表格源。 |
| Strategy | string |
值 legacy、shuffle 或 native。 此提示定义 partition 运算符的执行策略。
如果未指定任何策略,则使用 legacy 策略。 有关详细信息,请参阅策略。 |
|
| Column | string |
✔️ | T 中的一列名称,其值确定如何对输入表格源进行分区。 |
| TransformationSubQuery | string |
✔️ | 表格转换表达式。 源为通过对 T 的记录进行分区而隐式生成的子表。每个子表在列的值上都是同源的。
表达式必须仅提供一个表格结果,并且不应具有其他类型的语句,例如 let 语句。 |
| SubQueryWithSource | string |
✔️ | 包含其自己的表格源(例如表引用)的表格表达式。 仅 旧版策略支持此语法。 子查询只能引用 T 中的键列 Column。若要引用列,请使用语法 toscalar(Column)。
表达式必须仅提供一个表格结果,并且不应具有其他类型的语句,例如 let 语句。 |
| Hints | string |
零个或零个以上(以空格分隔)HintName Value 形式的参数,用于控制运算符的行为。= 请参阅每个策略类型 支持的提示。 |
Supported hints
| Hint name | 类型 | Strategy | Description |
|---|---|---|---|
hint.shufflekey |
string |
shuffle | 用于使用 shuffle 策略运行分区运算符的分区键。 |
hint.materialized |
bool |
legacy | 如果设置为 true,则具体化 partition 运算符的源。 默认值是 false。 |
hint.concurrency |
int |
legacy | 决定要并行运行的分区数量。 默认值是 16。 |
hint.spread |
int |
legacy | 决定如何在群集节点之间分配分区。 默认值是 1。
例如,如果存在 N 分区,并且分布提示设置为 P,则 N 分区由 P 不同的群集节点以并行/顺序处理,具体取决于并发提示。 |
Returns
此运算符返回各个子查询的结果的并集。
Strategies
partition 运算符支持多种子查询操作策略:本机、随机,和旧版。
Note
native 和 shuffle 策略之间的区别使调用方能够指示子查询的基数和执行策略。 此选项可能会影响子查询完成所需的时间,但不会更改最终结果。
Native strategy
当分区键的相异值数量不大(以千计)时,应该应用此策略。
子查询必须为表格转换,它不指定表格源。 源是隐式的,将根据子表分区进行分配。 子查询中只能使用某些 受支持的运算符。 分区数量没有限制。
若要使用此策略,请指定 hint.strategy=native。
Shuffle strategy
当分区键的相异值数量很大(以百万计)时,应该应用此策略。
子查询必须为表格转换,它不指定表格源。 源是隐式的,将根据子表分区进行分配。 子查询中只能使用某些 受支持的运算符。 分区数量没有限制。
若要使用此策略,请指定 hint.strategy=shuffle。 有关随机策略和性能的详细信息,请参阅随机执行查询。
本机和随机策略支持的运算符
以下运算符列表可用于具有本机或随机策略的子查询:
- count
- distinct
- extend
- make-series (部分受支持,请参阅注释)
- mv-apply
- mv-expand
- parse
- parse-where
- project
- project-away
- project-keep
- project-rename
- project-reorder
- reduce
- sample
- sample-distinct
- scan
- search
- serialize
- sort
- summarize
- take
- top
- top-hitters
- top-nested
- where
Note
- 引用子表分区以外的表源的运算符与
native和shuffle策略不兼容。 例如,联接、联合、externaldata 和 评估(插件)。 对于此类情况,请采用 旧策略。 - 任何策略类型都不支持分支运算符,因为子查询必须返回单个表格结果。
- 在运算符中
partition by仅部分支持 make-series 运算符。 仅当同时指定和from参数时to,才支持它。
Legacy strategy
出于历史原因,策略 legacy 是默认策略。 但是,我们建议使用 本机 策略或 随机 策略,因为 legacy 方法仅限于 64 个分区,并且效率较低。
在某些情况下,legacy 策略可能是必需的,因为它支持在子查询中包含表格源。 在这种情况下,子查询只能从输入表格源 T 引用键列 Column。若要引用列,请使用语法 toscalar(Column)。
如果子查询是没有表格源的表格转换,则源是隐式的,并且基于子表分区。
若要使用此策略,请指定 hint.strategy=legacy 或省略任何其他策略指示。
Note
如果分区列 列包含超过 64 个非重复值,则会发生错误。
Examples
本节中的示例演示如何使用语法帮助你入门。
本文中的示例使用 帮助群集中的公开可用表,例如
StormEvents数据库中的 表。
查找顶部值
在某些情况下,使用 partition 运算符编写查询比使用 top-nested 运算符更高效且更方便。 以下查询针对每个以 summarize 开头的 top 运行计算 State 和 W 的子查询:"WYOMING"、"WASHINGTON"、"WEST VIRGINIA" 和 "WISCONSIN"。
StormEvents
| where State startswith 'W'
| partition hint.strategy=native by State
(
summarize Events=count(), Injuries=sum(InjuriesDirect) by EventType, State
| top 3 by Events
)
Output
| EventType | State | Events | Injuries |
|---|---|---|---|
| Hail | WYOMING | 108 | 0 |
| High Wind | WYOMING | 81 | 5 |
| Winter Storm | WYOMING | 72 | 0 |
| Heavy Snow | WASHINGTON | 82 | 0 |
| High Wind | WASHINGTON | 58 | 13 |
| Wildfire | WASHINGTON | 29 | 0 |
| Thunderstorm Wind | WEST VIRGINIA | 180 | 1 |
| Hail | WEST VIRGINIA | 103 | 0 |
| Winter Weather | WEST VIRGINIA | 88 | 0 |
| Thunderstorm Wind | WISCONSIN | 416 | 1 |
| Winter Storm | WISCONSIN | 310 | 0 |
| Hail | WISCONSIN | 303 | 1 |
Native strategy
以下查询返回按 EventType 的以“W”开头的每个 TotalInjuries 中的前 2 个 State 值:
StormEvents
| where State startswith 'W'
| partition hint.strategy = native by State
(
summarize TotalInjueries = sum(InjuriesDirect) by EventType
| top 2 by TotalInjueries
)
Output
| EventType | TotalInjueries |
|---|---|
| Tornado | 4 |
| Hail | 1 |
| Thunderstorm Wind | 1 |
| Excessive Heat | 0 |
| High Wind | 13 |
| Lightning | 5 |
| High Wind | 5 |
| Avalanche | 3 |
Shuffle strategy
以下查询返回前 3 个 DamagedProperty 值 foreach EpisodeId 和列 EpisodeId 和 State。
StormEvents
| partition hint.strategy=shuffle by EpisodeId
(
top 3 by DamageProperty
| project EpisodeId, State, DamageProperty
)
| count
Output
| Count |
|---|
| 22345 |
使用显式源的 Legacy 策略
以下查询运行两个子查询:
- 当
x == 1时,查询从StormEvents返回所有具有InjuriesIndirect == 1的行。 - 当
x == 2时,查询从StormEvents返回所有具有InjuriesIndirect == 2的行。
最终结果是这两个子查询的并集。
range x from 1 to 2 step 1
| partition hint.strategy=legacy by x {StormEvents | where x == InjuriesIndirect}
| count
Output
| Count |
|---|
| 113 |
Partition reference
以下示例演示如何使用 as 运算符为每个数据分区指定“名称”,然后在子查询中重复使用该名称。 此方法只与 legacy 策略相关。
T
| partition by Dim
(
as Partition
| extend MetricPct = Metric * 100.0 / toscalar(Partition | summarize sum(Metric))
)