jai*_*me_ 5 .net kql azure-data-explorer kusto-explorer
场景:kusto 表中的数据在 5 小时后更新。任务:从 .net API 调用查询 在查询中,创建一个子查询并使用该子查询对更大的表执行联接
let table1=materialize(
Customer|where CustomerId=="cust-reg-aabb-cc232"|distinct CustomerId,City);
CustomerPurchase
|where CustomerId=="cust-reg-aabb-cc232"
//perform join with table1 and other things
Run Code Online (Sandbox Code Playgroud)
或者
let table1=view(){
Customer|where CustomerId=="cust-reg-aabb-cc232"|distinct CustomerId,City};
CustomerPurchase
|where CustomerId=="cust-reg-aabb-cc232"
//perform join with table1 and CustomerPurchase
Run Code Online (Sandbox Code Playgroud)
CustomerPurchase 和客户数据每 5 小时更新一次(添加新行)。更优化的是:创建视图或使用materialize方法。我浏览了文档,但无法理解两者之间的区别。
另外,由于我正在实现 API,是否可以使用物化视图而不是 table1?
该文档非常清楚:
允许在查询执行期间缓存子查询结果,以便其他子查询可以引用部分结果。
视图是基于 Kusto 查询语言查询的结果集的虚拟表。就像真正的表一样,视图包含行和列。与真实的表不同,视图不拥有自己的数据存储。
视图是通过用户定义的函数定义的,具有以下要求:
- 函数的结果必须是表格形式(例如,它不能是标量值)。
- 该函数不得带任何参数。
视图关键字
默认情况下,支持通配符语法来指定表名称的运算符不会引用视图,即使视图的名称与通配符匹配。此类运算符的一个示例是并集运算符。在这种情况下,请使用 view 关键字来包含视图。
物化视图公开对源表或另一个物化视图的聚合查询。
顾名思义,聚合结果被物化,意思是——存储。
结果不断更新,数据不断被摄取。
就您而言,似乎没有理由使用materialize()或view关键字。
使用如下所示的物化视图(在 CustomerId 上使用过滤器)代替table1.
.create materialized-view Customer_mv on table Customer
{
Customer
| summarize by CustomerId, City
}
Run Code Online (Sandbox Code Playgroud)
这里有几个例子展示了Materialize()的好处
let t = print x = rand(1000);
union t, t, t
Run Code Online (Sandbox Code Playgroud)
| X |
|---|
| 第337章 |
| 998 |
| 第242章 |
let t = materialize(print x = rand(1000));
union t, t, t
Run Code Online (Sandbox Code Playgroud)
| X |
|---|
| 第722章 |
| 第722章 |
| 第722章 |
let t1 = range i from 1 to 100000000 step 1 | summarize count() by i = i%2;
let t2 = t1;
t1
| join kind=inner t2 on i
Run Code Online (Sandbox Code Playgroud)
| 我 | 数数_ | i1 | 计数_1 |
|---|---|---|---|
| 1 | 50000000 | 1 | 50000000 |
| 0 | 50000000 | 0 | 50000000 |
执行时间:4.4375515
let t1 = materialize(range i from 1 to 100000000 step 1 | summarize count() by i = i%2);
let t2 = t1;
t1
| join kind=inner t2 on i
Run Code Online (Sandbox Code Playgroud)
| 我 | 数数_ | i1 | 计数_1 |
|---|---|---|---|
| 1 | 50000000 | 1 | 50000000 |
| 0 | 50000000 | 0 | 50000000 |
执行时间:2.5313002
这是一个演示视图好处的示例
.create-or-alter function StormEvents_top_5_deaths_v ()
{
cluster("help").database("Samples").StormEvents
| project Duration = EndTime - StartTime
,Distance = round(geo_distance_2points(BeginLon, BeginLat, EndLon, EndLat))
,TotalDeath = DeathsDirect + DeathsIndirect
,TotalInjuries = InjuriesIndirect + InjuriesIndirect
,TotalDamage = DamageCrops + DamageProperty
| where TotalDeath > 0
| top 5 by TotalDeath
}
Run Code Online (Sandbox Code Playgroud)
StormEvents_top_5_deaths_v
Run Code Online (Sandbox Code Playgroud)
| 期间 | 距离 | 总死亡人数 | 总伤害 | 总伤害 |
|---|---|---|---|---|
| 20.07:00:00 | 0 | 14 | 0 | 0 |
| 00:18:00 | 20609 | 13 | 0 | 46000000 |
| 01:02:00 | 26080 | 11 | 0 | 250000000 |
| 2.20:00:00 | 0 | 10 | 0 | 0 |
| 18.09:00:00 | 0 | 10 | 0 | 0 |
| 归档时间: |
|
| 查看次数: |
3693 次 |
| 最近记录: |