All Products
Search
Document Center

Simple Log Service:Improve query performance on large-scale data with materialized views

Last Updated:Mar 02, 2026

Simple Log Service (SLS) provides materialized views to accelerate SQL analysis on large-scale datasets. It automatically extracts and stores reusable subresults—such as aggregations, filters, and projections—from a target SQL statement. For subsequent queries, SLS automatically rewrites the SQL to use these precomputed results, significantly improving performance.

Overview

Why use materialized views

SLS enables real-time data analysis using SQL. Each SQL query analyzes all data within a specified time range. For large datasets, the standard execution mode may return inaccurate results, time out, or exceed concurrency limits.

High-performance fully accurate query and analysis (Dedicated SQL) significantly increases computing power. However, this mode has a data size limit and may result in long execution times for very large datasets.

For scenarios with strict requirements on SQL execution time—such as dashboard refresh—consider using materialized views. Materialized views incrementally pre-compute and save intermediate results of SQL sub-statements. When you refresh a report, the system automatically uses these pre-computed results to significantly improve performance.

How it works

Component

Description

Source logstore

The logstore that contains the raw logs. Materialized views do not affect the source logstore.

Materialized database

A database automatically created to store materialized view results. Data is processed as a stream when written. Even if raw logs are out of order by time, the scheduled materialization results will neither duplicate nor miss any data.

Scheduled background computation

The system automatically extracts materialization patterns from the input SQL and periodically computes intermediate results from the source logstore. To create a materialized view, provide only the SQL query to accelerate. You do not need to manage materialization details.

Transparent rewrite

When running an SQL query, the SLS execution engine intelligently analyzes the SQL structure, automatically matches the query with a corresponding materialized view, and transparently rewrites the query. You query the source logstore directly, and the compute engine finds and uses the appropriate materialized view to rewrite the query. This rewrite process is transparent to you.

Latest data visibility

The compute engine reads data from the materialized view for the data range already materialized. For new data not yet materialized, the engine reads and computes data in real time from the source logstore. The engine then automatically merges the two result sets to return a complete result. This provides performance benefits while ensuring the latest data is visible in real time.

Prerequisites

Before you begin, ensure the following requirements are met:

Requirement

Description

Logstore type

Standard logstore only. Query logstores, Metricstores, and StoreViews are not supported.

Management method

API only. The console does not yet support materialized view operations.

SDK version

Java SDK 0.6.138 or later

Permissions

log:CreateMaterializedView, log:GetMaterializedView, log:ListMaterializedViews, log:DeleteMaterializedView (RAM users only)

Quick start

Create a materialized view in three steps:

// 1. Import the SDK
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.request.CreateMaterializedViewRequest;

// 2. Initialize the client
Client client = new Client("cn-hangzhou.log.aliyuncs.com", accessId, accessKey);

// 3. Create a materialized view
CreateMaterializedViewRequest request = new CreateMaterializedViewRequest(
    "your-project",           // Project name
    "my_mv",                   // Materialized view name
    "your-logstore",           // Source logstore
    "* | select count(*) as cnt", // SQL to accelerate
    60,                        // Computation interval (minutes)
    startTime,                 // Start time (Unix timestamp)
    0                          // TTL (0 = same as source)
);
client.createMaterializedView(request);

For complete examples, see the detailed instructions below.

Use materialized views

This section provides step-by-step instructions using Java as an example.

Step 1: Configure permissions

An Alibaba Cloud account has all permissions by default and requires no extra authorization. If you are a Resource Access Management (RAM) user, ensure you have the following permissions:

Required permissions for materialized views

Request the AliyunLogFullAccess system policy for SLS from the Alibaba Cloud account owner. For fine-grained control, create a custom permission policy with the permissions in the following table. For more information, see Create custom policies.

Operation

Permission Action

Create a materialized view

log:CreateMaterializedView

Update a materialized view

log:UpdateMaterializedView

Get details of a materialized view

log:GetMaterializedView

Delete a materialized view

log:DeleteMaterializedView

List materialized views

log:ListMaterializedViews

Step 2: Import the SDK

Import the following version of the SLS SDK for Java:

<dependency>
    <groupId>com.aliyun.openservices</groupId>
    <artifactId>aliyun-log</artifactId>
    <version>0.6.138</version>
</dependency>

Step 3: Create, list, get, and delete materialized views

The following example shows how to create, list, retrieve details of, and delete materialized views. Modify and run the code as needed.

Obtain parameters:

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

  • To obtain the host endpoint:

    1. Log on to the Simple Log Service console.

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

    3. Click the icon next to the project name to go to the project overview page. In the Endpoint section, 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 SLS endpoint. This example uses Hangzhou. Change it to your actual region.
     */
    static String host = "cn-hangzhou.log.aliyuncs.com";
    /**
     * Create an SLS 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 {
        // The SQL query to accelerate with the materialized view.
        String originalSql = "* | select count(l1) as cnt, l2 from stability group by l2";
        // The scheduled materialization period in minutes.
        int aggIntervalMins = 60;
        // The start time for materialization. Based on the write time of raw data.
        // Only raw data after this time is materialized.
        int startTime = dateStrToSecond("2025-07-30 00:00:00");
        // The TTL of the materialization logstore.
        // A value of 0 means it is the same as the source logstore's TTL.
        // The TTL of the materialization logstore cannot be less than the source logstore's TTL.
        int ttl = 0;
        // The number of materialized views in a single project cannot exceed 100.
        CreateMaterializedViewRequest request = new CreateMaterializedViewRequest(
            projectName, materializedViewName, logstoreName, originalSql, aggIntervalMins, startTime, ttl);
        client.createMaterializedView(request);
        System.out.println("create materialized view " + materializedViewName);
    }

    static void listMv() throws LogException
    {
        // 0 and 10 are parameters for paginated queries.
        ListMaterializedViewsRequest request = new ListMaterializedViewsRequest(projectName, "", 0, 10);
        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.

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

  • Common aggregate functions are supported. For a detailed list, see the following table:

Supported aggregate functions

Syntax

Aggregate functions

count(*), count(1), count(x), count_if(boolean expression), max(x), max(x, n), min(x), min(x, n), sum(x), arbitrary(x)

Approximate functions

approx_distinct(x), approx_distinct(x, e), approx_percentile(x, percentage)

Billing

Billing for materialized views is based on the volume of data written to the logstore that stores the materialized view. This data volume depends on the size of the results from the scheduled SQL queries. The data volume is typically much smaller than that of the source logstore. Billing is based on the Pay-by-ingested-data model for Standard logstores.