Take vs. Limit vs. Top, Sort vs. Order By

Take vs. Limit vs. Top, Sort vs. Order By

长期使用传统的SQL,习惯order bylimittop是MS自己的“极限”但更直观。

这里两个 Kusto 查询共享相同的条件和顺序(排序,对吧?),唯一的区别是 return 有多少,20 对 200。结果令人惊讶:

AzureDiagnostics | where Category contains "postgresql" | take 20  | order by TimeGenerated desc

AzureDiagnostics | where Category contains "postgresql" | take 200  | order by TimeGenerated desc

top更一致

AzureDiagnostics | where Category contains "postgresql" | top 2  by TimeGenerated desc 
AzureDiagnostics | where Category contains "postgresql" | top 20  by TimeGenerated desc 

更新,感谢@Yoni L,这里是摘要:

  1. 每个 | 部分的顺序很重要。例如,|take 10 | order by x 将在 take 之后排序,这是随机的。
  2. take = limit 用法
  3. top 除了必须和 sort by x [asc/desc] 一起使用,或者简称 top 10 by x desc.
  4. 一样

top 不同,对于 takelimit(它们是别名),无法保证返回哪些记录,除非对源数据进行排序。

以下是等价的 - top 20 by x descorder by x desc | take 20,但它具有不同的语义:take 20 | order by x desc - 它仅对(最多)20 条记录进行排序 taken