Skip to main content

NGram BloomFilter Index

Note:

Performance tables, charts, timings, and capacity heuristics on this page are retained from upstream Apache Doris material. They were not measured on VeloDB Cloud. Use them as starting points and validate recall, latency, throughput, and memory on your own workload.

The NGram BloomFilter index is a Skip Index specifically designed to accelerate LIKE '%pattern%' fuzzy queries on string columns. Built on top of the BloomFilter index, it introduces N-Gram tokenization to split text into overlapping groups of adjacent characters before writing them into the BloomFilter, thereby supporting fast filtering for fuzzy matching.

The index can reduce scan I/O when the pattern is selective. The usage example retains upstream measurements for illustration. It does not establish a VeloDB Cloud speedup.

Index principle​

Ngram Bloomfilter

N-Gram tokenization produces overlapping groups of consecutive characters, rather than groups of words. For example, with N = 3, 'ngram' produces 'ngr', 'gra', and 'ram'.

Key differences between the NGram BloomFilter index and the regular BloomFilter index:

ComparisonBloomFilter IndexNGram BloomFilter Index
Written contentThe original column valuesEach character group obtained by N-Gram tokenization of the original text
Accelerated query typeEquality queries (=, IN)LIKE '%pattern%' fuzzy queries
Applicable column typeMultiple data typesString columns only

Query acceleration process:

  1. Apply N-Gram tokenization to the pattern in LIKE '%pattern%'.
  2. Check one by one whether each tokenized character group exists in the BloomFilter.
  3. If a character group does not exist in the BloomFilter, the corresponding data block cannot satisfy the LIKE condition and can be skipped.
  4. Skipping data blocks that do not match reduces IO and accelerates the query.

Use cases​

The NGram BloomFilter index is suitable for the following scenarios:

  • Acceleration of LIKE '%pattern%' fuzzy queries on string columns.
  • The number of consecutive characters in the LIKE pattern is greater than or equal to the gram_size defined for the index.

Note:

  • Only string columns are supported, and only LIKE queries can be accelerated.
  • The NGram BloomFilter index and the regular BloomFilter index are mutually exclusive. Only one of them can be chosen for the same column.
  • The method for analyzing index effectiveness is the same as for the BloomFilter index.

Manage indexes​

Create an index​

Create at table creation​

Add the index definition after the COLUMN definition in the table creation statement:

INDEX `idx_column_name` (`column_name`) USING NGRAM_BF
PROPERTIES("gram_size"="3", "bf_size"="1024")
COMMENT 'username ngram_bf index'

Syntax description​

ParameterRequiredDescription
idx_column_name (column_name)Requiredcolumn_name is the name of the column on which the index is built and must be defined earlier; idx_column_name is the index name and must be unique within the table
USING NGRAM_BFRequiredSpecifies the index type as NGram BloomFilter index
PROPERTIESOptionalSpecifies additional properties for the NGram BloomFilter index. See the table below for details
COMMENTOptionalSpecifies a comment for the index

Tip:

It is recommended to prefix index names with idx_, for example idx_review_body.

Properties parameters​

PropertyDescription
gram_sizeThe N in N-Gram, specifying that every N consecutive characters form a character group. For example, 'ngram' produces three overlapping character trigrams when N = 3. It is recommended to set this to the minimum length of the LIKE query string, but not below 2.
bf_sizeThe size of the BloomFilter, in bytes. It determines the index size for each data block. A larger value consumes more storage space but reduces the probability of hash collisions.

Recommended configuration: It is generally recommended to start with "gram_size"="3" and "bf_size"="1024", then tune based on the Query Profile.

View indexes​

-- Method 1: In the INDEX section of the table schema, USING NGRAM_BF marks an NGram BloomFilter index
SHOW CREATE TABLE table_name;

-- Method 2: An IndexType of NGRAM_BF marks an NGram BloomFilter index
SHOW INDEX FROM table_name;

Drop an index​

ALTER TABLE table_ngrambf DROP INDEX idx_ngrambf;

Modify an index​

The NGram BloomFilter index can be added to an existing table in the following two ways:

-- Method 1: Use CREATE INDEX
CREATE INDEX idx_column_name2 ON table_ngrambf(column_name2)
USING NGRAM_BF
PROPERTIES("gram_size"="3", "bf_size"="1024")
COMMENT 'username ngram_bf index';

-- Method 2: Use ALTER TABLE ADD INDEX
ALTER TABLE table_ngrambf
ADD INDEX idx_column_name2(column_name2) USING NGRAM_BF
PROPERTIES("gram_size"="3", "bf_size"="1024")
COMMENT 'username ngram_bf index';

Use the index​

Enable function pushdown​

Check the effective session setting with SHOW VARIABLES LIKE 'enable_function_pushdown'. The Core 26.1.3 source initializes it to false, but a warehouse can use a different effective setting. Enable it for the current session before testing index pruning:

SET enable_function_pushdown = true;

Trigger index acceleration​

The NGram BloomFilter index is automatically used to accelerate LIKE queries, for example:

SELECT count() FROM table1 WHERE message LIKE '%error%';

Index effectiveness analysis​

The acceleration effect of the BloomFilter index (including NGram BloomFilter) can be analyzed using the following metrics in the Query Profile:

MetricMeaning
RowsBloomFilterFilteredNumber of rows filtered out by the BloomFilter index, which can be compared with other Rows metrics to analyze the filtering effect
BlockConditionsFilteredBloomFilterTimeTime consumed by the BloomFilter index

Usage example​

The row counts, output, and elapsed times below come from the upstream Apache Doris example. Its full hardware and release configuration is not specified. Re-run the workload on your warehouse to evaluate performance. Loading this dataset consumes compute and storage. Use a test database and drop the example table when finished.

The following uses the Amazon product user review dataset amazon_reviews as an example to demonstrate how to use the NGram BloomFilter index and its acceleration effect.

Step 1: Create the table​

CREATE TABLE `amazon_reviews` (
`review_date` int(11) NULL,
`marketplace` varchar(20) NULL,
`customer_id` bigint(20) NULL,
`review_id` varchar(40) NULL,
`product_id` varchar(10) NULL,
`product_parent` bigint(20) NULL,
`product_title` varchar(500) NULL,
`product_category` varchar(50) NULL,
`star_rating` smallint(6) NULL,
`helpful_votes` int(11) NULL,
`total_votes` int(11) NULL,
`vine` boolean NULL,
`verified_purchase` boolean NULL,
`review_headline` varchar(500) NULL,
`review_body` string NULL
) ENGINE=OLAP
DUPLICATE KEY(`review_date`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`review_date`) BUCKETS 16
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"compression" = "ZSTD"
);

Step 2: Load data​

1. Download the dataset

Use wget or another tool to download the dataset from the following URLs:

https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2010.snappy.parquet
https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2011.snappy.parquet
https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2012.snappy.parquet
https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2013.snappy.parquet
https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2014.snappy.parquet
https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2015.snappy.parquet

2. Load via Stream Load

Use the warehouse's HTTPS Stream Load endpoint from Connections, a SQL user with load privileges, and the database you created for the test. Replace <warehouse-http-endpoint>, <database-name>, and <sql-user> below. curl prompts for the password. Repeat for each downloaded file.

curl --location-trusted --user '<sql-user>' \
--upload-file amazon_reviews_2010.snappy.parquet \
--header 'format:parquet' \
'https://<warehouse-http-endpoint>/api/<database-name>/amazon_reviews/_stream_load'

Use only the warehouse endpoint and trusted redirect destinations, because --location-trusted forwards authentication on redirects.

Note:

Large files can exceed the effective Stream Load request limit. Split the input or use a suitable bulk-loading method. Contact VeloDB Cloud support if a managed limit must change. Users do not edit be.conf or call compute-node configuration APIs.

3. Verify the data load

Run count() via SQL to confirm that the data was loaded successfully:

SELECT COUNT(*) FROM amazon_reviews;
+-----------+
| count(*) |
+-----------+
| 135589433 |
+-----------+

Step 3: Performance comparison​

Scenario 1: Query without an index (takes 7.60s)

SELECT
product_id,
any_value(product_title),
AVG(star_rating) AS rating,
COUNT() AS count
FROM
amazon_reviews
WHERE
review_body LIKE '%is super awesome%'
GROUP BY
product_id
ORDER BY
count DESC,
rating DESC,
product_id
LIMIT 5;

Query result:

+------------+------------------------------------------+--------------------+-------+
| product_id | any_value(product_title) | rating | count |
+------------+------------------------------------------+--------------------+-------+
| B00992CF6W | Minecraft | 4.8235294117647056 | 17 |
| B009UX2YAC | Subway Surfers | 4.7777777777777777 | 9 |
| B00DJFIMW6 | Minion Rush: Despicable Me Official Game | 4.875 | 8 |
| B0086700CM | Temple Run | 5 | 6 |
| B00KWVZ750 | Angry Birds Epic RPG | 5 | 6 |
+------------+------------------------------------------+--------------------+-------+
5 rows in set (7.60 sec)

Scenario 2: After adding the NGram BloomFilter index (takes 0.93s)

Add the index:

ALTER TABLE amazon_reviews
ADD INDEX review_body_ngram_idx(review_body) USING NGRAM_BF
PROPERTIES("gram_size"="10", "bf_size"="10240");

Wait for the schema change to finish, then run the same query again. Check progress with SHOW ALTER TABLE COLUMN WHERE TableName = 'amazon_reviews':

+------------+------------------------------------------+--------------------+-------+
| product_id | any_value(product_title) | rating | count |
+------------+------------------------------------------+--------------------+-------+
| B00992CF6W | Minecraft | 4.8235294117647056 | 17 |
| B009UX2YAC | Subway Surfers | 4.7777777777777777 | 9 |
| B00DJFIMW6 | Minion Rush: Despicable Me Official Game | 4.875 | 8 |
| B0086700CM | Temple Run | 5 | 6 |
| B00KWVZ750 | Angry Birds Epic RPG | 5 | 6 |
+------------+------------------------------------------+--------------------+-------+
5 rows in set (0.93 sec)

Performance comparison:

ScenarioTimePerformance Gain
Without index7.60 sBaseline
With NGram BloomFilter index added0.93 sAbout 8x

Index limitations for escaped LIKE patterns​

Planned for Core 26.1.4, LIKE queries with a custom ESCAPE or a backslash in the pattern do not use the NGram BloomFilter index for pruning, to avoid incorrect filtering. These queries remain supported through normal scanning. Confirm warehouse availability before relying on this planned behavior. LIKE patterns without escapes can still use the index when the existing conditions are met.

Ask AI