Use Cases
-
Scenario one: Suppose you need to perform exact deduplication on massive data (such as retail or delivery orders). However, the column for deduplication is of STRING type, which may lead to suboptimal performance during counting. For instance, in the
orderstable, theorder_uuidcolumn representing order ID is of STRING type, typically 32 to 36 bytes in size, generated byUUID()or similar functions. In this case, COUNT(DISTINCT) on the STRING columnorder_uuidwith a query likeSELECT count(DISTINCT order_uuid) FROM orders WHERE create_date >= CURDATE();might not deliver satisfactory performance. Using an INTEGER column for exact deduplication would significantly enhance performance. -
Scenario two: Suppose you want to accelerate exact deduplication in multi-dimensional analysis using bitmap functions. The
bitmap_count()function requires INTEGER input, but if the column to be deduplicated is of STRING type, you need to use thebitmap_hash()function. This may result in approximate, slightly lower deduplication counts and could also reduce query performance and increase storage requirements, as the INTEGER values generated by bitmap_hash() are more dispersed compared to sequentially assigned INTEGER values. -
Scenario three: Suppose you need to query the number of orders with a short time span between placing and paying for the order, where the order placement and payment times might be stored in different tables maintained by different business teams. You might need to join these tables based on the order ID and then deduplicate the orders. For example:
However, using a STRING type
order_uuidcolumn for joins is less efficient than using an INTEGER column.
Optimization Approach
To address the the issues in the above scenarios, the optimization approach involves loading order data into a target table and establishing a mapping between STRING and INTEGER values. Subsequent query analysis will be based on the INTEGER column. This approach can be divided into the following stages:- Stage 1: Create a global dictionary and establish a mapping between STRING and INTEGER values. In this dictionary, the key column is of STRING type, and the value column is an AUTO INCREMENT INTEGER type. When data is loaded, the system automatically generates a unique ID for each STRING value, creating a mapping between STRING and INTEGER values.
- Stage 2: Load the mapping relationship between order data and the global dictionary into the target table.
- Stage 3: Use the INTEGER column from the target table for exact deduplication or joins during subsequent query analysis, which can significantly improve performance.
- Stage 4: For further performance optimization, you can use bitmap functions on the INTEGER column to accelerate exact deduplication.
Solution
Before v3.2.5, Stage 2 could be implemented through two methods:- Using an external table or an internal table as intermediate table to join with the dictionary table to obtain the corresponding dictionary ID before loading.
- Using a Primary Key table for data loading, and then using an UPDATE statement with a JOIN operation to update the dictionary ID. However, this data loading process can be inconvenient and comes with many constraints.
dict_mapping() function, allowing you to define the dictionary ID column in the target table as a generated column using the dict_mapping() expression. Subsequent data loading tasks are handled like regular data loading, without needing UPDATE statements with JOIN operations to write dictionary IDs. During data loading, the system automatically associates the original table with the dictionary table and inserts the corresponding dictionary ID, greatly simplifying the data loading process with a global dictionary table, regardless of table types and supporting various loading methods.
Business Scenario
The following example uses two example CSV files,batch1.csv and batch2.csv, each containing two columns: id and order_uuid.
-
batch1.csv -
batch2.csv
Process
Stage one
Create a global dictionary table and load the order ID column values from the CSV files to establish a mapping between STRING and INTEGER values.-
Create a Primary Key table to serve as the global dictionary. Define the Primary Key,
order_uuid(of STRING type), and the value column,order_id_int(of AUTO INCREMENT INTEGER type). Info Thedict_mappingfunction requires the global dictionary table to be a Primary Key table. -
Use Stream Load to batch load the
order_uuidcolumn from the two CSV files into theorder_uuidcolumn of the dictionary tabledict. Make sure you have used the Partial Update in column mode.
NOTE If new data is added to the data source before proceeding to the next stage, all new data must be loaded into the dictionary table to ensure the mapping exists.
Stage two
Create a target table that includes a dictionary ID column with thedict_mapping attribute. When order data is loaded into the target table, the system will automatically associate it with the dictionary table and insert the corresponding dictionary ID.
-
Create a table
dest_tablethat includes all columns from the CSV files. You also need to define an INTEGER columnorder_id_int(typically BIGINT) to map with the STRING typeorder_uuidcolumn and has thedict_mappingcolumn attribute. Future query analysis will be based on thisorder_id_intcolumn. -
Load data into the target table with Stream Load or any other available methods. Since the
order_id_intcolumn has thedict_mappingattribute, the system will automatically fetch the dictionary ID fromdictduring loading.
Stage three
During query analysis, you can perform exact deduplication or joins on the INTEGER columnorder_id_int, significantly enhancing performance compared to using the STRING column order_uuid.
Use bitmap functions to accelerate exact deduplication
To further accelerate the calculation, you can insert the INTEGER column values of the dictionary table directly into a bitmap column after creating a global dictionary. Subsequently, you can use bitmap functions on this bitmap column for exact deduplication.Method one
If you have built a global dictionary and already imported the order data intodest_table, follow these steps:
-
Create an Aggregate table
dest_table_bitmapwith two columns: a BITMAP typeorder_id_bitmapcolumn for aggregation using thebitmap_union()function, and an INTEGER type columnid. This table does not include the original STRING column, otherwise, each bitmap would contain only one value, negating the acceleration benefit. -
Insert data into
dest_table_bitmap. Insert the data from theidcolumn ofdest_tableinto theidcolumn, and insert the INTEGER columnorder_id_intdata from the dictionary tabledict(processed with theto_bitmap()function) into theorder_id_bitmapcolumn. -
Use the
BITMAP_UNION_COUNT()function on the BITMAP column for exact deduplication.
Method two
If you do not need to retain specific order data after creating a global dictionary, and you want to load data directly into thedest_table_bitmap table, follow these steps:
-
Create an Aggregate table
dest_table_bitmapwith two columns: a BITMAP typeorder_id_bitmapcolumn for aggregation using thebitmap_union()function, and an INTEGER type columnid. This table does not include the original STRING column, otherwise, each bitmap would contain only one value, negating the acceleration benefit. -
Insert data into the Aggregate table. Insert the data from the
idcolumn of the CSV files into theidcolumn and the INTEGER columnorder_id_intdata from the dictionary tabledict(processed with theto_bitmap()function) into theorder_id_bitmapcolumn. -
Use the
BITMAP_UNION_COUNT()function on the BITMAP column for exact deduplication.