Indexing Principles

The NGram BloomFilter index, similar to the BloomFilter index, is a skip list index based on BloomFilter.

Unlike the BloomFilter index, the NGram BloomFilter index is used to accelerate text LIKE queries. Instead of storing the original text values, it tokenizes the text using NGram and stores each token in the BloomFilter. For LIKE queries, the pattern in LIKE ‘%pattern%’ is also tokenized using NGram. Each token is checked against the BloomFilter, and if any token is not found, the corresponding data block does not meet the LIKE condition and can be skipped, reducing IO and accelerating the query.

Use Cases

The NGram BloomFilter index can only accelerate string LIKE queries, and the number of consecutive characters in the LIKE pattern must be greater than or equal to the N defined in the NGram index.

N-Gram BloomFilter Index - 图1tip

  • NGram BloomFilter only supports string columns and can only accelerate LIKE queries.
  • NGram BloomFilter index and BloomFilter index are mutually exclusive, meaning a column can only have one or the other.
  • The performance analysis of the NGram BloomFilter index is similar to that of the BloomFilter index.

Syntax

Creating an NGram BloomFilter Index

The index definition follows the COLUMN definition in the CREATE TABLE statement:

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

Explanation of the syntax:

  1. idx_column_name(column_name) is mandatory. column_name is the column to be indexed and must appear in the column definitions above. idx_column_name is the index name, which must be unique at the table level. It is recommended to name it with a prefix idx_ followed by the column name.

  2. USING NGRAM_BF is mandatory and specifies that the index type is an NGram BloomFilter index.

  3. PROPERTIES is optional and is used to specify additional properties for the NGram BloomFilter index. The supported properties are:

    • gram_size: The N in NGram, specifying the number of consecutive characters to form a token. For example, ‘an ngram example’ with N = 3 would be tokenized into ‘an ‘, ‘n n’, ‘ ng’, ‘ngr’, ‘gra’, ‘ram’ (6 tokens).
    • bf_size: The size of the BloomFilter in bits. bf_size determines the size of the index corresponding to each data block. The larger this value, the more storage space it occupies, but the lower the probability of hash collisions.

    It is recommended to set gram_size to the minimum length of the string in LIKE queries but not less than 2. Generally, “gram_size”=”3”, “bf_size”=”1024” is recommended, then adjust based on the Query Profile.

  4. COMMENT is optional and specifies an index comment.

Viewing NGram BloomFilter Index

  1. SHOW CREATE TABLE table_ngrambf;

Deleting an NGram BloomFilter Index

  1. ALTER TABLE table_ngrambf DROP INDEX idx_ngrambf;

Modifying an NGram BloomFilter Index

  1. CREATE INDEX idx_column_name2(column_name2) ON table_ngrambf USING NGRAM_BF PROPERTIES("gram_size"="3", "bf_size"="1024") COMMENT 'username ngram_bf index';
  2. 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';

Usage Example

This section demonstrates the usage and effectiveness of the NGram BloomFilter index using a dataset of Amazon product reviews, amazon_reviews.

Table Creation

  1. CREATE TABLE `amazon_reviews` (
  2. `review_date` int(11) NULL,
  3. `marketplace` varchar(20) NULL,
  4. `customer_id` bigint(20) NULL,
  5. `review_id` varchar(40) NULL,
  6. `product_id` varchar(10) NULL,
  7. `product_parent` bigint(20) NULL,
  8. `product_title` varchar(500) NULL,
  9. `product_category` varchar(50) NULL,
  10. `star_rating` smallint(6) NULL,
  11. `helpful_votes` int(11) NULL,
  12. `total_votes` int(11) NULL,
  13. `vine` boolean NULL,
  14. `verified_purchase` boolean NULL,
  15. `review_headline` varchar(500) NULL,
  16. `review_body` string NULL
  17. ) ENGINE=OLAP
  18. DUPLICATE KEY(`review_date`)
  19. COMMENT 'OLAP'
  20. DISTRIBUTED BY HASH(`review_date`) BUCKETS 16
  21. PROPERTIES (
  22. "replication_allocation" = "tag.location.default: 1",
  23. "compression" = "ZSTD"
  24. );

Data Import

Download the dataset using wget or other tools from the following URLs:

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

Import the data using stream load:

  1. curl --location-trusted -u root: -T amazon_reviews_2010.snappy.parquet -H "format:parquet" http://127.0.0.1:8030/api/${DB}/amazon_reviews/_stream_load
  2. curl --location-trusted -u root: -T amazon_reviews_2011.snappy.parquet -H "format:parquet" http://127.0.0.1:8030/api/${DB}/amazon_reviews/_stream_load
  3. curl --location-trusted -u root: -T amazon_reviews_2012.snappy.parquet -H "format:parquet" http://127.0.0.1:8030/api/${DB}/amazon_reviews/_stream_load
  4. curl --location-trusted -u root: -T amazon_reviews_2013.snappy.parquet -H "format:parquet" http://127.0.0.1:8030/api/${DB}/amazon_reviews/_stream_load
  5. curl --location-trusted -u root: -T amazon_reviews_2014.snappy.parquet -H "format:parquet" http://127.0.0.1:8030/api/${DB}/amazon_reviews/_stream_load
  6. curl --location-trusted -u root: -T amazon_reviews_2015.snappy.parquet -H "format:parquet" http://127.0.0.1:8030/api/${DB}/amazon_reviews/_stream_load

Run a count query to confirm successful data import:

  1. mysql> SELECT COUNT(*) FROM amazon_reviews;
  2. +-----------+
  3. | count(*) |
  4. +-----------+
  5. | 135589433 |
  6. +-----------+

Querying

First, run the query without any index. The WHERE clause contains a LIKE condition, and the query takes 7.60 seconds:

  1. SELECT
  2. product_id,
  3. any(product_title),
  4. AVG(star_rating) AS rating,
  5. COUNT(*) AS count
  6. FROM
  7. amazon_reviews
  8. WHERE
  9. review_body LIKE '%is super awesome%'
  10. GROUP BY
  11. product_id
  12. ORDER BY
  13. count DESC,
  14. rating DESC,
  15. product_id
  16. LIMIT 5;

Results:

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

Next, add an NGram BloomFilter index and run the same query again. The query takes 0.93 seconds, an 8x performance improvement:

  1. ALTER TABLE amazon_reviews ADD INDEX review_body_ngram_idx(review_body) USING NGRAM_BF PROPERTIES("gram_size"="10", "bf_size"="10240");
  1. +------------+------------------------------------------+--------------------+-------+
  2. | product_id | any_value(product_title) | rating | count |
  3. +------------+------------------------------------------+--------------------+-------+
  4. | B00992CF6W | Minecraft | 4.8235294117647056 | 17 |
  5. | B009UX2YAC | Subway Surfers | 4.7777777777777777 | 9 |
  6. | B00DJFIMW6 | Minion Rush: Despicable Me Official Game | 4.875 | 8 |
  7. | B0086700CM | Temple Run | 5 | 6 |
  8. | B00KWVZ750 | Angry Birds Epic RPG | 5 | 6 |
  9. +------------+------------------------------------------+--------------------+-------+
  10. 5 rows in set (0.93 sec)