Common syntax for columnar tables
This topic describes how to create and manage columnar tables, including core operations such as setting order keys and converting table formats, and how to analyze execution plans to optimize query performance.
Create columnar tables
This section describes how to create a columnar table, including basic creation, how to set an order key to optimize performance, and how storage policies work.
Create a non-partitioned table
Create a basic columnar table. In the CREATE TABLE statement, you need only to specify ENGINE=XEngine and TABLE_FORMAT=COLUMN.
-- Create a columnar table to store sales records
CREATE TABLE sales_records (
sale_id INT PRIMARY KEY AUTO_INCREMENT,
product_id INT NOT NULL,
region VARCHAR(50),
sale_date DATE,
amount DECIMAL(10, 2)
) ENGINE=XEngine, TABLE_FORMAT=COLUMN;Use an order key to optimize query performance
Pre-sorting data significantly improves the performance of specific queries. The order key (order key) is the core optimization method for columnar tables. It determines the physical storage order of data. A well-designed order key can improve the performance of filtering, aggregation, and sorting operations several times over.
Design recommendations
High-frequency filter columns: Use the columns most frequently used in
WHEREconditions for range or equality queries as the order key, especially time columns such assale_date.High-frequency grouping columns: Include the dimension columns frequently used in
GROUP BYin the order key.High-cardinality dimension columns: Select columns with high cardinality (a large number of unique values) as the prefix of the order key to improve data filtering efficiency.
Procedure: In the CREATE TABLE statement, use the ORDER KEY clause to specify the order key.
order_key must not exceed 3,072 bytes in total length across all of its columns.
-- Create a columnar table with an order key
-- Queries frequently filter by date range and region, so use them as the order key
CREATE TABLE sales_records_sorted (
sale_id INT PRIMARY KEY AUTO_INCREMENT,
product_id INT NOT NULL,
region VARCHAR(50),
sale_date DATE,
amount DECIMAL(10, 2),
-- Define an order key named idx_date_region
ORDER KEY idx_date_region(sale_date, region)
) ENGINE=XEngine, TABLE_FORMAT=COLUMN;Specify a storage policy (storage_policy) for hot-cold data tiering
storage_policy specifies the storage medium for data. It is designed to separate hot data from cold data by storing infrequently accessed data in lower-cost Object Storage Service (OSS).
HOT: Data is stored in high-performance PolarStore. This is the default setting.COLD: Data is stored in low-cost OSS.
storage_policy=cold syntax is supported. However, in the current version, all data (whether HOT or COLD) is temporarily stored in PolarStore.
-- Syntax example: create a columnar table with the COLD storage policy
CREATE TABLE archive_logs (
log_id BIGINT PRIMARY KEY,
log_time TIMESTAMP,
message TEXT
) ENGINE=XEngine, TABLE_FORMAT=COLUMN, STORAGE_POLICY=COLD;Create partitioned tables
The syntax for creating a columnar partitioned table is the same as that for a non-partitioned table. In the CREATE TABLE statement, specify ENGINE=XEngine and TABLE_FORMAT=COLUMN.
To create an XEngine partitioned table (which archives the partitioned table as a hybrid partitioned table), the following conditions must be met:
MySQL 8.0.2with minor version 8.0.2.2.34.1 or later, set the
loose_polar_allow_create_hybrid_partitionparameter toON.MySQL 8.0.2with minor version earlier than 8.0.2.2.34.1, upgrade to the required minor version.
-- Create a columnar partitioned table
CREATE TABLE t1(a1 INT, a2 INT, a3 INT, a4 INT) ENGINE=XEngine TABLE_FORMAT=COLUMN
PARTITION BY RANGE(a1)
(
PARTITION p1 VALUES LESS THAN (20),
PARTITION p2 VALUES LESS THAN (40),
PARTITION p3 VALUES LESS THAN (60),
PARTITION p4 VALUES LESS THAN MAXVALUE
);
-- Create a columnar partitioned table and store the data in OSS
CREATE TABLE t2(a1 INT, a2 INT, a3 INT, a4 INT) ENGINE=XEngine TABLE_FORMAT=COLUMN STORAGE_POLICY=COLD
PARTITION BY RANGE(a1)
(
PARTITION p1 VALUES LESS THAN (20),
PARTITION p2 VALUES LESS THAN (40),
PARTITION p3 VALUES LESS THAN (60),
PARTITION p4 VALUES LESS THAN MAXVALUE
);Manage columnar tables
This section describes how to manage existing tables, including converting between row-based and columnar formats, modifying table schemas, and changing partition formats.
Convert table storage formats (row-based and columnar)
Convert an existing row-based table to a columnar table to accelerate analytics, or convert a columnar table back to a row-based table.
// Convert a row-based table to an XEngine columnar table
ALTER TABLE sales_records ENGINE=XEngine, TABLE_FORMAT=COLUMN;
// Convert a columnar table to an XEngine row-based table
ALTER TABLE sales_records ENGINE=XEngine TABLE_FORMAT=ROW;
//Convert a columnar table to an InnoDB row-based table
ALTER TABLE sales_records ENGINE=innodb;Modify table schemas (columns and indexes)
Columnar tables support Online DDL. You can add, modify, or drop columns or indexes without blocking DML operations. The syntax is the same as for row-based tables.
-- Add a new column online
ALTER TABLE sales_records ADD COLUMN salesperson_id INT;
-- Drop a column online
ALTER TABLE sales_records DROP COLUMN salesperson_id;
-- Add a regular index online
ALTER TABLE sales_records ADD INDEX idx_product(product_id);
-- Drop an index online
ALTER TABLE sales_records DROP INDEX idx_product;In the current version, the ADD COLUMN operation does not support Instant DDL, but it does support Online DDL. This means that adding a column requires a full copy of the table data, but DML operations are not blocked during DDL execution.
Modify order keys
Adjust or drop the order key of a columnar table as your query patterns change.
-- Add an order key to an existing columnar table
ALTER TABLE sales_records ADD ORDER KEY idx_region(region), ALGORITHM=INPLACE;
-- Modify the order key definition (drop it first and then add a new one)
ALTER TABLE sales_records_sorted DROP ORDER KEY idx_date_region, ADD ORDER KEY idx_product(product_id), ALGORITHM=INPLACE;
-- Drop an order key
ALTER TABLE sales_records_sorted DROP ORDER KEY idx_date_region, ALGORITHM=INPLACE;Change partition storage formats
In a partitioned table, convert some historical partitions to the columnar format to achieve hybrid row-column storage. Keep the partitions that hold recently active data in the row-based format to ensure OLTP performance, and convert the partitions that hold historical archive data to the columnar format to accelerate analytics.
-- Step 1: Create an InnoDB partitioned table
CREATE TABLE historical_orders (
order_id INT NOT NULL,
order_date DATE NOT NULL,
amount DECIMAL(10, 2)
) ENGINE=InnoDB
PARTITION BY RANGE(TO_DAYS(order_date)) (
PARTITION p2023 VALUES LESS THAN (TO_DAYS('2024-01-01')),
PARTITION p2024 VALUES LESS THAN (TO_DAYS('2025-01-01'))
);
-- Step 2: Convert the 2023 historical data partition to the columnar format
ALTER TABLE historical_orders CHANGE PARTITION p2023 ENGINE=XEngine TABLE_FORMAT=COLUMN;Analyze and optimize query performance
Use the EXPLAIN command to determine whether a query successfully uses the columnar engine and to understand how the query is executed. A columnar execution plan is displayed as a horizontal tree structure, which differs significantly from the format of a traditional row-based plan.
Example
The following example uses the TPC-H Q5 benchmark query to compare columnar and row-based execution plans.
Columnar execution plan example
+----+-------------------------------------------+----------+----------------------------------------------------------------------------------------------------+
| ID | Operator | Name | Extra Info |
+----+-------------------------------------------+----------+----------------------------------------------------------------------------------------------------+
| 1 | Select Statement | | IMCI Execution Plan (max_dop = 32, max_query_mem = unlimited) |
| 2 | └─Sort | | Sort Key: revenue DESC |
| 3 | └─Hash Groupby | | Group Key: nation.n_name |
| 4 | └─Hash Join | | Join Cond: (lineitem.l_suppkey, customer.c_nationkey) = (supplier.s_suppkey, supplier.s_nationkey) |
| 5 | ├─Hash Join | | Join Cond: orders.o_orderkey = lineitem.l_orderkey |
| 6 | │ ├─Hash Join | | Join Cond: (customer.c_nationkey, region.r_regionkey) = (nation.n_nationkey, nation.n_regionkey) |
| 7 | │ │ ├─Hash Join | | Join Cond: orders.o_custkey = customer.c_custkey |
| 8 | │ │ │ ├─Cartesian Product | | |
| 9 | │ │ │ │ ├─Table Scan | region | Cond: (r_name = "ASIA") |
| 10 | │ │ │ │ └─Table Scan | orders | Cond: ((o_orderdate >= 01/01/1994) AND (o_orderdate < 01/01/1995)) |
| 11 | │ │ │ └─Table Scan | customer | |
| 12 | │ │ └─Table Scan | nation | |
| 13 | │ └─Table Scan | lineitem | |
| 14 | └─Table Scan | supplier | |
+----+-------------------------------------------+----------+----------------------------------------------------------------------------------------------------+How to read the plan:
Confirm columnar execution: If the
Extra Infocolumn displaysIMCI Execution Plan, the query is processed by the columnar engine.Check the degree of parallelism:
max_dop = 32indicates that the maximum number of parallel threads for the query inside the columnar engine is 32.Understand the core operators:
Table Scan: represents a scan of a columnar table. TheCondsection shows condition pushdown: theWHEREconditions are pushed down to the storage layer for execution, which is the key to performance optimization.Hash Join/Hash Groupby: efficient join and aggregation algorithms used by the columnar engine, suitable for processing large-scale data.Sort: the sorting operation. Performance is significantly better when the order key matches theORDER BYcolumns.
Row-based execution plan comparison
+----+-------------+----------+------------+--------+---------------+---------+---------+---------------------------+---------+----------+----------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+--------+---------------+---------+---------+---------------------------+---------+----------+----------------------------------------------+
| 1 | SIMPLE | region | NULL | ALL | PRIMARY | NULL | NULL | NULL | 5 | 20.00 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | orders | NULL | ALL | PRIMARY | NULL | NULL | NULL | 1500000 | 11.11 | Using where; Using join buffer (hash join) |
| 1 | SIMPLE | customer | NULL | eq_ref | PRIMARY | PRIMARY | 8 | tpch.orders.o_custkey | 1 | 100.00 | NULL |
| 1 | SIMPLE | nation | NULL | eq_ref | PRIMARY | PRIMARY | 8 | tpch.customer.c_nationkey | 1 | 10.00 | Using where |
| 1 | SIMPLE | lineitem | NULL | ref | PRIMARY | PRIMARY | 8 | tpch.orders.o_orderkey | 2 | 100.00 | NULL |
| 1 | SIMPLE | supplier | NULL | eq_ref | PRIMARY | PRIMARY | 8 | tpch.lineitem.l_suppkey | 1 | 10.00 | Using where |
+----+-------------+----------+------------+--------+---------------+---------+---------+---------------------------+---------+----------+----------------------------------------------+A row-based plan usually contains markers such as Using temporary; Using filesort, which indicate that temporary tables must be created and sorting is performed on disk. These are the main performance bottlenecks in analytical queries. A columnar plan avoids these inefficient operations through in-memory parallel computation.