Optimize Table Indexes
Overview
Indexes reduce the rows or data pages that a query needs to inspect. Choose an index for the filter operators and value distribution in your workload, then verify its effect in the query profile.
VeloDB Cloud currently supports two categories of indexes:
| Index category | Included types | Characteristics |
|---|---|---|
| Built-in indexes | Prefix index, ZoneMap index | Automatically generated at table creation, no extra maintenance |
| Secondary indexes | Inverted index, Bloomfilter index, N-Gram Bloomfilter index, Vector index | Created by users on demand, can be managed independently |
An index also adds storage and write or build work. A selective filter is more likely to benefit than a filter that matches most rows.
For detailed introductions to each index type, refer to the Table Index chapter. This article starts from real cases and introduces index usage tips and optimization recommendations for several typical scenarios.
Review the prerequisites
- You understand the current table's Key column definitions and order.
- You have identified high-frequency filter fields in the business.
- You have evaluated whether you can rebuild the table or only append secondary indexes.
- You have grasped the applicable scenarios of different index types.
The following t1 definitions are alternatives, not successive statements against the same table. Use a test database with create and query privileges, and drop or rename the first table before trying the second definition. Remove test tables after comparison.
Case 1: Adjust the leading sort key for prefix filtering
In Optimizing Table Schema Design, the article introduced how to choose appropriate fields as Key fields and use the Key column sorting feature of VeloDB Cloud to accelerate queries. This case further extends that scenario.
Background
The built-in prefix index is a sparse index on leading sort key columns. Default selection is bounded by three columns and 36 bytes, and stops after a VARCHAR column. Filters that match a leading prefix can narrow the scan. See Prefix Index for encoding and selection rules.
Example 1: Identify a mismatch between the key and filter
The original CREATE TABLE statement is as follows:
CREATE TABLE `t1` (
`c1` VARCHAR(10) NULL,
`c2` VARCHAR(10) NULL
) ENGINE=OLAP
DUPLICATE KEY(`c1`)
DISTRIBUTED BY HASH(`c2`) BUCKETS 10;
The corresponding business SQL patterns are as follows:
select * from t1 where t1.c2 = '1';
select * from t1 where t1.c2 in ('1', '2', '3');
In this schema, c1 is the sort key, but the queries filter on c2. Those filters do not match the leading sort key and cannot use its prefix to narrow the scan.
Put the filtered column first
Place the c2 column in the first field position so that the prefix index covers the business filter condition:
CREATE TABLE `t1` (
`c2` VARCHAR(10) NULL,
`c1` VARCHAR(10) NULL
) ENGINE=OLAP
DUPLICATE KEY(`c2`)
DISTRIBUTED BY HASH(`c2`) BUCKETS 10;
Tip:
When defining the schema column order, refer to the high-frequency, high-priority columns used in business query filters to fully leverage the acceleration capability of the VeloDB Cloud prefix index.
Case 2: Use inverted indexes
Applicable filters
VeloDB Cloud supports inverted indexes as secondary indexes, used to accelerate the following business scenarios:
- Full-text retrieval on text-type fields.
- Equality queries on string, numeric, or datetime fields.
- Range queries on string, numeric, or datetime fields.
Build and storage considerations
You can add an inverted index without changing the table's sort key or re-importing data. Creating the index and building it for existing data are separate operations. Use BUILD INDEX ON table_name for historical data in VeloDB Cloud, and check build status before measuring the result. Index builds consume compute and storage.
For typical use cases, syntax, and examples, refer to Inverted Index. This section does not repeat them.
Tip:
When the original table structure and Key definitions are inconvenient to optimize, or when the cost of re-importing data is high, inverted indexes provide a flexible acceleration option for optimizing business execution performance.
Index selection comparison
| Index type | Applicable queries | Requires table rebuild | Requires data re-import | Typical field types |
|---|---|---|---|---|
| Prefix index | Equality, range, prefix matching | Yes (adjust Key) | Yes | Key columns sorted in front |
| ZoneMap index | Range filtering | No (automatic) | No | All columns |
| Inverted index | Full-text retrieval, equality, range | No | No | String, numeric, datetime |
| Bloomfilter index | High-cardinality equality filter | No | No (takes effect incrementally) | String, numeric |
| N-Gram Bloomfilter | LIKE fuzzy matching | No | No (takes effect incrementally) | String |
The checked Core implementation does not accept BITMAP as a new index type. Use an inverted index for new equality-filter indexes instead of copying a legacy bitmap-index recipe.
FAQ and common issues
Q1: Why are my queries still slow after creating an index?
Possible reasons:
- The query condition does not hit any indexed column.
- The Key column order does not match the filter condition, so the prefix index does not take effect.
- The data volume is small, and the index does not bring noticeable benefit.
- The index has not yet taken effect on historical data (some secondary indexes only take effect immediately on newly written data).
Q2: Does the prefix index need to be created manually?
No. The query engine selects a leading sort key prefix automatically, bounded by three columns and 36 bytes by default and ending after a VARCHAR column. Put frequently filtered columns first when the table model's key semantics allow it.
Q3: How to choose between an inverted index and a Bloomfilter index?
- Full-text retrieval and supported equality or range filters: consider inverted indexes. Check the tokenizer and operator requirements. Arbitrary
LIKEpatterns do not automatically use a tokenized inverted index. - High-cardinality equality filters: compare Bloomfilter and inverted indexes on representative queries. The best choice depends on selectivity and index storage or write costs.
Q4: Does adjusting the key column order require rebuilding the table?
Use a replacement table to redesign the sort key, then reload or copy the data and validate the result. Reordering displayed columns does not by itself change the stored sort order.
Summary
In schema tuning, index optimization is as important as table-level schema optimization. VeloDB Cloud provides multiple index types:
- Built-in indexes: prefix index, ZoneMap index.
- Secondary indexes: inverted index, Bloomfilter, N-Gram Bloomfilter, and vector indexes.
Using these indexes appropriately can significantly improve business query and analysis speed across multiple scenarios. It is recommended to first evaluate the high-frequency business filter fields, then choose the appropriate index type based on factors such as whether the table can be rebuilt and the data volume.