练习 - 使用 materialize 函数优化查询

已完成

在本练习中,你将使用 materialize 函数来优化查询。

回想一下,materialize 函数在运行时缓存子查询的结果,以便查询的其他部分可以引用部分结果。

使用 materialize 函数

在我们的零售公司场景中,你的销售团队要求你提供收按入排名前 10 位的州/省/自治区/直辖市中排名靠前的客户及其在州/省/自治区/直辖市销售额中所占百分比。

若要提供这些信息,需要将查询分解为多个阶段,以便可以查看每个阶段的结果,如下所示:

  1. 运行以下查询,按收入获取排名靠前的州/省/自治区/直辖市。 使用 let 语句将子查询分配给变量 USCustomersUSCustomerSales

    运行查询

    let USCustomers = Customers | where RegionCountryName == 'United States';
    let USCustomerSales =
        SalesFact
        | summarize USTotalCustomerSales = sum(SalesAmount) by CustomerKey
        | join kind=inner USCustomers on CustomerKey;
    USCustomerSales
    | summarize USTotalStateSales = round(sum(USTotalCustomerSales)) by StateProvinceName
    

    查看生成的列表。 请注意,表包含 StateProvinceName 和 USTotalStateSales 的列。 你需要在结果中显示省份名称,因此需要将 Customers 表联接到结果。

    Screenshot of the `materialize` function, showing the first block of the query.

  2. 运行以下查询,将省/自治区/直辖市和各州/省/自治区/直辖市中排名靠前的客户的详细信息添加到结果中。 通过将 USCustomerSales 子查询联接到上一个查询的结果来添加此数据。

    现在,可通过在 USCustomerSaleslet 语句中使用 materialize 函数来优化查询。 这样做只会运行一次分配的子查询,并缓存结果。

    运行查询

    let USCustomers = Customers | where RegionCountryName == 'United States';
    let USCustomerSales = materialize(
        SalesFact
        | summarize USTotalCustomerSales = sum(SalesAmount) by CustomerKey
        | join kind=inner USCustomers on CustomerKey);
    USCustomerSales
    | summarize USTotalStateSales = round(sum(USTotalCustomerSales)) by StateProvinceName
    | lookup (
        USCustomerSales
        | summarize arg_max(USTotalCustomerSales, *) by StateProvinceName
        )
        on StateProvinceName
    | top 10 by USTotalStateSales
    

    结果应如下图所示:

    Screenshot of the materialize function, showing the second block of the query.

  3. 运行以下查询来输出报表的列,并计算排名靠前的客户对其所在州/省/自治区/直辖市销售额贡献的百分比。

    运行查询

    let Pcent = (portion: real, total: real) { round(100 * portion / total, 2) };
    let USCustomers = Customers | where RegionCountryName == 'United States';
    let USCustomerSales = materialize(
        SalesFact
        | summarize USTotalCustomerSales = sum(SalesAmount) by CustomerKey
        | join kind=inner USCustomers on CustomerKey);
    USCustomerSales
    | summarize USTotalStateSales = round(sum(USTotalCustomerSales)) by StateProvinceName
    | lookup (
        USCustomerSales
        | summarize arg_max(USTotalCustomerSales, *) by StateProvinceName
        )
        on StateProvinceName
    | top 10 by USTotalStateSales
    | project
        StateProvinceName,
        StateSales = USTotalStateSales,
        TopCustomerFirstName = FirstName,
        TopCustomerLastName = LastName,
        TopCustomerSales = round(USTotalCustomerSales),
        TopCustomerPercentage = Pcent(USTotalCustomerSales, USTotalStateSales)
    

    结果应如下图所示:

    Screenshot of the materialize function, showing the entire query.