All Products
Search
Document Center

Simple Log Service:Accelerate queries with materialized views

Last Updated:Jun 22, 2026

Materialized views incrementally pre-compute SQL sub-statements and persist intermediate results. When you run a query, the system automatically rewrites the SQL to read pre-computed data, reducing dashboard and report query latency from minutes to seconds.

Choose an acceleration strategy

Simple Log Service (SLS) supports real-time SQL analysis, where each query processes the full dataset within the selected time range. For large datasets, you can improve performance with the following methods:

Acceleration method

Principle

Use cases

Limitations

High-performance, fully accurate query and analysis (Dedicated SQL)

Increases computing resources to improve the parallelism of a single query.

Large datasets with frequently changing SQL queries.

Data scale has an upper limit, and execution time can be long for massive datasets.

Materialized view (this topic)

Performs incremental pre-computation on sub-statements of a fixed SQL query. At query time, the query is automatically rewritten to read pre-computed results.

Reporting and dashboard scenarios with relatively fixed SQL queries and massive datasets.

Only Standard Logstores are supported. Up to 100 materialized views can be created per project.

How it works

  • Source Logstore: The Logstore containing the raw log data. Materialized views do not affect the Source Logstore.

  • materialized store: When you create a materialized view, the system automatically creates a corresponding materialized store to hold the pre-computed results.

    Materialization processes ingested data as a stream. Even if raw data arrives with out-of-order log times, scheduled materialization does not produce duplicates or omissions.
  • Scheduled background computation: The system extracts the materialization pattern from the input SQL and periodically computes intermediate results from the Source Logstore. You only need to provide the target SQL when creating a materialized view; the system handles the materialization logic automatically.

  • transparent rewrite: When you run an SQL query, the SLS execution engine analyzes the SQL structure, matches it to an existing materialized view, and rewrites the query automatically. You can continue querying the Source Logstore without modifying your SQL or specifying a materialized view.

  • Automatic merge for data integrity: For time ranges already materialized, the engine reads directly from the materialized view. For the latest unmaterialized data, it computes from the Source Logstore in real time. The two result sets are merged automatically to return a complete, up-to-date result.

Note

Transparent rewrite is the core mechanism of a materialized view. Unlike systems that require you to manually query a materialized result table, SLS automates SQL matching, rewriting, and result merging, requiring no changes to your existing queries. After you create a materialized view, acceleration is not immediate because the system must asynchronously pre-compute historical data from the start time to the current time. This build process typically takes 1 to 12 hours, depending on data volume and computation complexity. Acceleration takes effect only after the pre-computation covers the query's time range.

Scope and limitations

  • Only Standard Logstores are supported. Query Logstores, MetricStores, and StoreViews are not supported.

  • You can create up to 100 materialized views per project.

  • You can create and manage materialized views in the console or via the API.

Permission requirements

An Alibaba Cloud account has all required permissions by default. A RAM user must be granted the following permissions:

Required permissions for materialized views

Request the AliyunLogFullAccess system policy for SLS from your Alibaba Cloud account administrator, or see Create a custom permission policy to implement fine-grained access control as described in the following table.

Actions

Permission action

Create a materialized view

log:CreateMaterializedView

Update a materialized view

log:UpdateMaterializedView

Get materialized view details

log:GetMaterializedView

Delete a materialized view

log:DeleteMaterializedView

List materialized views

log:ListMaterializedViews

Use materialized views

Create from the query page

After you run an SQL query on the query and analysis page, you can create a materialized view directly from that query.

  1. Log on to the Simple Log Service console.

  2. In the Projects list, click the target project, and then click the target Standard Logstore.

  3. On the query and analysis page, enter the SQL statement that you want to accelerate, and click Search & Analyze.

  4. In the statistics chart area of the query results, click Create Materialized View.

  5. In the creation wizard, complete the configuration. The SQL statement is automatically populated with the current query. The other parameters are the same as those for creating a view from the materialized views panel.

Create and manage in the console

Create a materialized view

  1. Log on to the Simple Log Service console.

  2. In the Projects list, click the target project.

  3. In the left-side navigation pane, click the Materialized View icon.

  4. On the materialized views panel, click Create Now.

  5. In the creation wizard, configure the following settings.

    1. Source Logstore: Configure the data source for the materialized view.

      Parameter

      Description

      Project

      Automatically populated with the current project and cannot be changed.

      Source type

      Select Logstore. Currently, only Standard Logstores are supported.

      Logstore

      Select the target Logstore from the drop-down list.

      After you complete the configuration, click Next.

    2. Materialized view configuration: Configure the name, SQL, and other parameters for the materialized view.

      Parameter

      Description

      Materialized view name

      Enter a name for the materialized view. The system automatically adds the mv_ prefix. You do not need to add it manually.

      SQL code

      Enter the SQL statement to accelerate. The editor supports syntax highlighting and autocompletion. After you enter the SQL statement, click Preview to validate the execution result.

      Note

      The column names used in the SQL statement must be indexed. Otherwise, an error occurs during the preview.

      Start time

      Select the start time for data materialization. This time is based on the write time of the raw data. The system materializes only data written after this time.

      Materialization interval

      Set the interval (in minutes) at which the view is recomputed. A shorter interval keeps the materialized result closer to real time but increases computation costs.

  6. After you complete the configuration, click Save.

    After the materialized view is created, the system automatically redirects you to the materialized view query page.

    Note

    The data retention period of a materialized view is the same as its associated Source Logstore. You can create a maximum of 100 materialized views per project.

View and manage materialized views

After creating a materialized view, you can view and manage it in the console.

  • View the list of materialized views

    In the left-side navigation pane of the target project, click the Materialized View icon. The side panel that appears displays all materialized views in the current project.

  • View materialized view details

    In the materialized view list, click the name of a materialized view to open its details page. The details page displays the following configuration information:

    • Materialized View Name: The full name of the materialized view, including the mv_ prefix.

    • Start At: The start time for data materialization.

    • View Update Interval: The interval at which the view is recomputed.

    • Data Retention Period: The same as the data retention period of the Source Logstore.

    • SQL Code: The SQL statement corresponding to the materialized view.

  • Delete a materialized view

    On the materialized view details page, click Delete, and then click OK in the confirmation dialog box.

    Warning

    A deleted materialized view cannot be recovered. The corresponding materialized store is also deleted.

Dashboard query acceleration

If a dashboard chart has high query latency, you can create a materialized view to accelerate it. For more information, see dashboard query acceleration.

Use an SDK

You can also create and manage materialized views programmatically using an SDK. The following example uses the Java language.

  1. Import the SLS SDK for Java.

    <dependency>
        <groupId>com.aliyun.openservices</groupId>
        <artifactId>aliyun-log</artifactId>
        <version>0.6.138</version>
    </dependency>
  2. The following example shows how to create, list, get, and delete materialized views. Modify and run the code as needed.

    • Obtain the parameters in the code as follows:

      • To obtain an AccessKey ID and AccessKey secret, see Obtain an AccessKey pair.

      • To obtain the endpoint:

        1. Log on to the Simple Log Service console, and in the projects list, click the target project.

        2. Click the image icon next to the project name to open the Project Overview page, and then copy the public endpoint.

      import com.aliyun.openservices.log.Client;
      import com.aliyun.openservices.log.exception.LogException;
      import com.aliyun.openservices.log.request.CreateMaterializedViewRequest;
      import com.aliyun.openservices.log.request.ListMaterializedViewsRequest;
      import com.aliyun.openservices.log.response.GetMaterializedViewResponse;
      import com.aliyun.openservices.log.response.ListMaterializedViewsResponse;
      
      import java.text.SimpleDateFormat;
      import java.util.concurrent.TimeUnit;
      
      public class MvDemo
      {
          static String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
          static String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
          /**
           * The endpoint for Simple Log Service. This example uses Hangzhou. Change it to your actual region.
           */
          static String host = "cn-hangzhou.log.aliyuncs.com";
          /**
           * Create a Simple Log Service client.
           */
          static Client client = new Client(host, accessId, accessKey);
          /**
           * The project name.
           */
          static String projectName = "xxx";
          /**
           * The Logstore name.
           */
          static String logstoreName = "xxx";
      
          public static void main(String[] args) throws Exception {
              String materializedViewName = "test_mv";
      
              createMv(materializedViewName); // Create a materialized view with the specified name.
      
              listMv(); // Get the list of materialized views in the current project.
      
              getMv(materializedViewName); // Get information about the current materialized view.
      
              // deleteMv(materializedViewName); // Delete the materialized view.
          }
      
          static int dateStrToSecond(String dateStr) throws Exception {
              SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              return (int)TimeUnit.MILLISECONDS.toSeconds(simpleDateFormat.parse(dateStr).getTime());
          }
      
          static void createMv(String materializedViewName) throws Exception {
              String originalSql = "* | select count(l1) as cnt, l2 from stability group by l2"; // Modify this to the SQL query you want to accelerate.
              int aggInternalMins = 60; // The materialization interval, in minutes.
              int startTime = dateStrToSecond("2025-07-30 00:00:00"); // The start time for materialization, based on the write time of raw data. Only data written after this time is materialized.
              int ttl = 0; // The TTL of the materialized store. A value of 0 means the TTL matches that of the Source Logstore. This TTL cannot be shorter than the Source Logstore's TTL.
              // The number of materialized views per project cannot exceed 100.
              CreateMaterializedViewRequest request = new CreateMaterializedViewRequest(projectName, materializedViewName, logstoreName, originalSql, aggInternalMins, startTime, ttl);
              client.createMaterializedView(request);
              System.out.println("create materialized view " + materializedViewName);
          }
      
          static void listMv() throws LogException
          {
              ListMaterializedViewsRequest request = new ListMaterializedViewsRequest(projectName, "", 0, 10); // 0 and 10 are pagination parameters.
              ListMaterializedViewsResponse response = client.listMaterializedViews(request);
              System.out.println("total materialized view count: " + response.getTotal());
              for (String materializedView : response.getMaterializedViews()) {
                  System.out.println(materializedView);
              }
          }
      
          static void getMv(String materializedViewName) throws Exception
          {
              GetMaterializedViewResponse response = client.getMaterializedView(projectName, materializedViewName);
              System.out.println("get materialized view detail, name: " + materializedViewName);
              System.out.println("originalSql: " + response.getOriginalSql());
              System.out.println("startTime: " + response.getStartTime());
              System.out.println("ttl: " + response.getTtl());
          }
      
          static void deleteMv(String materializedViewName ) throws LogException {
              client.deleteMaterializedView(projectName, materializedViewName);
              System.out.println("delete materialized view " + materializedViewName);
          }
      
      }

Supported syntax

  • All scalar functions and expressions are supported.

  • Clauses such as WHERE, GROUP BY, LIMIT, and TOP-N are supported.

  • Common aggregate functions are supported, as listed in the following table:

Aggregate functions

Syntax

aggregate functions

count(*)

count(1)

count(x)

count_if(boolean expression)

max(x)

max(xn)

min(x)

min(xn)

sum(x)

arbitrary(x)

Approximation functions

approx_distinct(x)

approx_distinct(xe)

approx_percentile(xpercentage)

Billing

Billing for a materialized view is based on the data volume written to the materialized store. This volume depends on the result size of the scheduled SQL computation and is typically much smaller than the Source Logstore data volume. Billing follows the Standard Logstore's pay-by-ingested-data model.

Note

To reduce materialization costs, avoid using a high-cardinality field such as a timestamp or user ID in the GROUP BY clause. Choose a materialization interval that matches your business refresh frequency.