Metadata Cache Table accelerates queries on OSS external tables

Updated at:

MaxCompute supports using Metadata Cache Table to cache the metadata of data files in OSS external tables. This provides file statistics for external tables and accelerates Splitting, improving Splitting performance when a large number of small files exist.

Background information

Metadata Cache acceleration for external tables stores metadata about the data files of an OSS external table in a Delta Table. It provides file statistics for the external table and accelerates Splitting, addressing poor Splitting performance when a conventional OSS external table contains a large number of small files.

The following example shows the details of an MCT-accelerated external table. Each row contains the metadata of a data file in the external table location. To synchronize the metadata of data files in the OSS external table to the MCT, manually run the refresh command.

key

size

type

last_modified

storage_class

etag

1.parquet

125829154

Normal

2025-12-01 08:30:15

Standard

"xxx"

2.parquet

98340864

Normal

2025-12-01 12:45:22

Standard

"yyy"

3.parquet

536870912

Normal

2025-11-30 23:59:59

Standard

"zzz"

Limits

  • Schema support

    To create an MCT-accelerated external table, the MaxCompute project must have the schema feature enabled. Schema syntax must also be enabled.

  • Read-only table

    An MCT-accelerated external table is read-only. DML statements such as INSERT INTO and INSERT OVERWRITE cannot be used to insert or overwrite data. To write data, use another table that points to the same OSS path.

  • Internal cache table

    A Metadata Cache Table is an internal cache table that is not visible to users. Direct queries on its content are not supported.

  • Refresh required for data visibility

    Unlike a regular external table, an MCT-accelerated external table can access data files in OSS only after the refresh command is run. If the table is queried before it is refreshed, no data is returned.

  • Only Hive-compatible external tables are supported

    Metadata Cache is supported only for Hive-compatible OSS external tables in the CSV/TSV, TEXTFILE, Parquet, ORC, or JSON format.

Syntax

Create an MCT-accelerated external table

The syntax is the same as that for a regular Hive-compatible OSS external table. In TBLPROPERTIES, add the metadata.cache.mode parameter. When the external table is created, the associated Metadata Cache is also created. For the CREATE TABLE syntax of each format, see OSS external tables.

CREATE EXTERNAL TABLE [IF NOT EXISTS] <mc_oss_extable_name>
(
<col_name> <data_type>,
...
)
[PARTITIONED BY (<col_name> <data_type>, ...)]
[ROW FORMAT SERDE 'serde_class'
  [WITH SERDEPROPERTIES (
    ['<property_name>'='<property_value>',...])
  ]
]
STORED AS file_format 
LOCATION 'oss://<oss_hdfs_endpoint>/<bucket>/<path_data>' 
TBLPROPERTIES (
    'metadata.cache.mode' = ['manual'|'periodic'|'crontab'],
    'metadata.staleness.seconds' = <integer>,
    '...'
);

Refresh the file metadata of an MCT-accelerated external table

Run the following command to synchronize the metadata of data files in the OSS external table to the Metadata Cache Table.

ALTER TABLE <mc_oss_extable_name> refresh metadata;
Important

A refresh reads the metadata of the external table and writes the metadata to the Metadata Cache Table. Therefore, the refresh incurs fees.

Parameters

  • For common parameters, see Basic syntax parameters.

  • The following table describes the parameters specific to Metadata Cache.

    property_name

    Required

    Description

    metadata.cache.mode

    Yes

    Specifies the cache refresh mode.

    • manual: Manually refreshes the cache at the required time.

    • periodic: Periodically refreshes the cache. This value must be used together with metadata.staleness.seconds.

    • crontab: Refreshes the cache on a schedule. This value must be used together with metadata.crontab.expression.

    metadata.staleness.seconds

    No

    The refresh interval. Valid values: [60, 604800], corresponding to 60 seconds to 1 week. The specified interval is not strictly guaranteed. The scheduler runs the refresh task at the specified interval whenever possible.

    If metadata.cache.mode is set to periodic, this parameter is required.

    metadata.crontab.expression

    No

    The expression for the scheduled task, in the cron expression format. For example, 0 0 14 * * ? triggers the task at 2:00 PM every day: 0 for seconds, 0 for minutes, 14 for hours (14:00, or 2:00 PM), * for every day, * for every month, and ? for no specific day of the week. The day-of-week and day-of-month fields are mutually exclusive to prevent conflicts.

    If metadata.cache.mode is set to crontab, this parameter is required.

Example: Accelerate Splitting for an ORC external table

This example compares the read performance of a regular ORC external table and an ORC external table that uses Metadata Cache for the same amount of data.

  1. Prepare the data. Assume that OSS contains more than 300,000 ORC files. Metadata Cache provides performance benefits only for large amounts of data and may degrade performance for small amounts of data.

  2. Create a regular ORC external table, read data from the table, and check the Splitting duration.

    CREATE EXTERNAL TABLE mc_ext_orc_test_30w (
        ss_sold_date_sk BIGINT COMMENT 'sales date SK',
        ss_sold_time_sk BIGINT COMMENT 'sales time SK',
        ss_item_sk BIGINT COMMENT 'item SK',
        ss_customer_sk BIGINT COMMENT 'customer SK',
        ss_cdemo_sk BIGINT COMMENT 'customer demographics SK',
        ss_hdemo_sk BIGINT COMMENT 'household demographics SK',
        ss_addr_sk BIGINT COMMENT 'address SK',
        ss_store_sk BIGINT COMMENT 'store SK',
        ss_promo_sk BIGINT COMMENT 'promotion SK',
        ss_ticket_number BIGINT COMMENT 'ticket number',
        ss_quantity BIGINT COMMENT 'quantity',
        ss_wholesale_cost DOUBLE COMMENT 'wholesale cost',
        ss_list_price DOUBLE COMMENT 'list price',
        ss_sales_price DOUBLE COMMENT 'sales price',
        ss_ext_discount_amt DOUBLE COMMENT 'discount amount',
        ss_ext_sales_price DOUBLE COMMENT 'extended sales price',
        ss_ext_wholesale_cost DOUBLE COMMENT 'extended wholesale cost',
        ss_ext_list_price DOUBLE COMMENT 'extended list price',
        ss_ext_tax DOUBLE COMMENT 'tax',
        ss_coupon_amt DOUBLE COMMENT 'coupon amount',
        ss_net_paid DOUBLE COMMENT 'net amount paid',
        ss_net_paid_inc_tax DOUBLE COMMENT 'net amount paid including tax',
        ss_net_profit DOUBLE COMMENT 'net profit'
    )
    comment 'An ET that uses Metadata Cache'
    ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde'
    with serdeproperties (
      'odps.properties.rolearn'='acs:ram::<uid>:role/aliyunodpsdefaultrole'
    )
    STORED AS orc
    location 'oss://oss-<region>-internal.aliyuncs.com/external_table/tpcds_data/orc/mc_ext_orc_test_30w/'
    ;
    
    -- Query data
    SET odps.job.workers.limit = 10;
    SELECT 
        CASE 
            WHEN ss_sold_time_sk BETWEEN 0 AND 21600 THEN '00:00-06:00'
            WHEN ss_sold_time_sk BETWEEN 21601 AND 43200 THEN '06:00-12:00'
            WHEN ss_sold_time_sk BETWEEN 43201 AND 64800 THEN '12:00-18:00'
            ELSE '18:00-24:00'
        END AS time_period,
        COUNT(*) AS sales_count,
        SUM(ss_ext_sales_price) AS total_sales
    FROM mc_ext_orc_test_30w
    GROUP BY 
        CASE 
            WHEN ss_sold_time_sk BETWEEN 0 AND 21600 THEN '00:00-06:00'
            WHEN ss_sold_time_sk BETWEEN 21601 AND 43200 THEN '06:00-12:00'
            WHEN ss_sold_time_sk BETWEEN 43201 AND 64800 THEN '12:00-18:00'
            ELSE '18:00-24:00'
        END
    ORDER BY total_sales DESC;
  3. Create an ORC external table that uses Metadata Cache. Manually refresh the file metadata, read data from the table, and check the Splitting duration.

    CREATE EXTERNAL TABLE mc_ext_orc_test_30w_mct (
        ss_sold_date_sk BIGINT COMMENT 'sales date SK',
        ss_sold_time_sk BIGINT COMMENT 'sales time SK',
        ss_item_sk BIGINT COMMENT 'item SK',
        ss_customer_sk BIGINT COMMENT 'customer SK',
        ss_cdemo_sk BIGINT COMMENT 'customer demographics SK',
        ss_hdemo_sk BIGINT COMMENT 'household demographics SK',
        ss_addr_sk BIGINT COMMENT 'address SK',
        ss_store_sk BIGINT COMMENT 'store SK',
        ss_promo_sk BIGINT COMMENT 'promotion SK',
        ss_ticket_number BIGINT COMMENT 'ticket number',
        ss_quantity BIGINT COMMENT 'quantity',
        ss_wholesale_cost DOUBLE COMMENT 'wholesale cost',
        ss_list_price DOUBLE COMMENT 'list price',
        ss_sales_price DOUBLE COMMENT 'sales price',
        ss_ext_discount_amt DOUBLE COMMENT 'discount amount',
        ss_ext_sales_price DOUBLE COMMENT 'extended sales price',
        ss_ext_wholesale_cost DOUBLE COMMENT 'extended wholesale cost',
        ss_ext_list_price DOUBLE COMMENT 'extended list price',
        ss_ext_tax DOUBLE COMMENT 'tax',
        ss_coupon_amt DOUBLE COMMENT 'coupon amount',
        ss_net_paid DOUBLE COMMENT 'net amount paid',
        ss_net_paid_inc_tax DOUBLE COMMENT 'net amount paid including tax',
        ss_net_profit DOUBLE COMMENT 'net profit'
    )
    comment 'An ET that uses Metadata Cache'
    ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde'
    with serdeproperties (
      'odps.properties.rolearn'='acs:ram::<uid>:role/aliyunodpsdefaultrole'
    )
    STORED AS orc
    location 'oss://oss-<region>-internal.aliyuncs.com/external_table/tpcds_data/orc/mc_ext_orc_test_30w/'
    tblproperties (
      'metadata.cache.mode' = 'manual');
    
    -- Manually refresh the metadata of the external table.
    ALTER TABLE mc_ext_orc_test_30w_mct REFRESH METADATA;
    
    -- Query data
    SET odps.job.workers.limit = 10;
    SELECT 
        CASE 
            WHEN ss_sold_time_sk BETWEEN 0 AND 21600 THEN '00:00-06:00'
            WHEN ss_sold_time_sk BETWEEN 21601 AND 43200 THEN '06:00-12:00'
            WHEN ss_sold_time_sk BETWEEN 43201 AND 64800 THEN '12:00-18:00'
            ELSE '18:00-24:00'
        END AS time_period,
        COUNT(*) AS sales_count,
        SUM(ss_ext_sales_price) AS total_sales
    FROM mc_ext_orc_test_30w_mct
    GROUP BY 
        CASE 
            WHEN ss_sold_time_sk BETWEEN 0 AND 21600 THEN '00:00-06:00'
            WHEN ss_sold_time_sk BETWEEN 21601 AND 43200 THEN '06:00-12:00'
            WHEN ss_sold_time_sk BETWEEN 43201 AND 64800 THEN '12:00-18:00'
            ELSE '18:00-24:00'
        END
    ORDER BY total_sales DESC;
  4. Compare the results. In this example, Splitting performance is expected to improve by 10 times (10×). In the test, when the same SQL statement reads the same OSS directory, Splitting takes 18 seconds for the regular external table and less than 1 second for the external table that uses Metadata Cache. The result meets expectations.

    image

Performance comparison

A Metadata Cache Table reduces the Splitting duration, query latency, and resource consumption. The following test results are based on different numbers of files.

  1. Prepare the data. Upload 100, 1,000, 10,000, 100,000, 330,000, 1 million, 3.3 million, and 10 million JSON files to different OSS directories. All files contain {"id": 1, "dt": "xxx"}.

  2. Create a regular JSON external table and an MCT-accelerated JSON external table. Then, refresh the file metadata of the MCT-accelerated table.

    -- Create an external table
    CREATE EXTERNAL TABLE
    three_tier_model.mct.et_test_100(id BIGINT, dt STRING) 
    STORED AS `json` LOCATION 'oss://oss-<region>.aliyuncs.com/<path>';
    
    -- Create an external table and enable MCT acceleration.
    CREATE EXTERNAL TABLE
    three_tier_model.mct.mct_test_100(id BIGINT, dt STRING) 
    STORED AS `json` LOCATION 'oss://oss-<region>.aliyuncs.com/<path>'
    tblproperties('metadata.cache.mode'='manual');
    
    -- refresh
    ALTER TABLE mct_test_100 refresh metadata;
    
    -- Create tables for 1k/10k/100k/330k/1m/3.3m/10m files in the same way.
  3. View the test results.

    Number of files

    Regular external table

    MCT-accelerated external table

    100

    0.1s

    0.1s

    1000

    0.1s

    0.1s

    10000 (10k)

    1.1s

    0.1s

    100000 (100k)

    16.9s

    0.7s

    330000 (330k)

    53.0s

    4.0s

    1000000 (1m)

    2min29s

    9.9s

    3300000 (3.3m)

    31min16s

    43.5s

    10000000 (10m)

    1h26min

    4min40s