- Storage overhead: JSON is a semi-structured type that requires storing the structure information of each row, leading to high storage usage and low compression efficiency.
- Query complexity: Queries need to detect data structures based on runtime data, making it difficult to achieve vectorized execution optimization.
- Redundant data: Queries need to read the entire JSON data, which includes many redundant fields.
- This feature is available from version 3.3.0. It is disabled by default before v4.0 and needs to be enabled manually. From v4.0 onwards, it is enabled by default.
What is Flat JSON
The core principle of Flat JSON is to detect JSON data during load and extract common fields from JSON data for storage as standard type data. When querying JSON, these common fields optimize the query speed of JSON. Example data:a and b are present in most JSON data and have similar data types (both INT). Therefore, the data of fields a and b can be extracted from JSON and stored separately as two INT columns. When these two columns are used in queries, their data can be directly read without needing to process additional JSON fields, reducing the computational overhead of handling JSON structures.
Enable Flat JSON
Flat JSON is enabled globally by default from v4.0 onwards. For versions earlier than v4.0, you must manually enable it. From v4.0, this feature can be configured on the table level.Enable for versions earlier than v4.0
-
Modify BE configuration:
enable_json_flat, which defaults tofalsebefore v4.0. For modification methods, refer to Configure BE parameters. -
Enable FE pruning feature:
Enable Flat JSON Feature on table level
Setting Flat JSON-related properties on table level is supported from v4.0 onwards.-
When creating the table, you can set
flat_json.enableand other Flat JSON-related properties. For detailed instructions, see CREATE TABLE. Alternatively, you can set these properties using ALTER TABLE. Example: -
Enable FE pruning feature:
Verify if Flat JSON is Effective
After loading data, you can query the extracted sub-columns of the corresponding column:PushdownAccessPaths: The number of sub-field paths pushed down to storage.AccessPathHits: The number of times Flat JSON sub-fields are hit, with detailed information on the specific JSON hit.AccessPathUnhits: The number of times Flat JSON sub-fields are not hit, with detailed information on the specific JSON not hit.JsonFlattern: The time taken to extract sub-columns on-site when Flat JSON is not hit.
Usage Example
-
Method 1: Configure Flat JSON properties when creating a table with JSON columns. Supported from v4.0 onwards.
- Method 2: Enable Flat JSON before creating the table.
- Enable the feature (refer to other sections)
-
Create a table with JSON columns. In this example, use INSERT INTO to load JSON data into the table.
-
View the extracted sub-columns for the
k2column. -
Execute data queries.
-
View Flat JSON-related metrics in the Query Profile
Related variables and configurations
Session variables
cbo_json_v2_rewrite(default: true): Enables JSON v2 path rewrite so that expressions likeget_json_*can be rewritten to direct access of Flat JSON subcolumns, unlocking predicate pushdown and column pruning.cbo_json_v2_dict_opt(default: true): Enables low-cardinality dictionary optimization for Flat JSON extended string subcolumns produced by the rewrite, which can speed up string expressions, GROUP BY, and JOIN.
BE Configurations
- json_flat_null_factor
- json_flat_column_max
- json_flat_sparsity_factor
- enable_compaction_flat_json
- enable_lazy_dynamic_flat_json
Feature Limitations
- All table types in the database support Flat JSON.
- Compatible with historical data without needing re-import. Historical data will coexist with data flattened by Flat JSON.
- Historical data will not automatically apply Flat JSON optimization unless new data is loaded or Compaction occurs.
- Enabling Flat JSON will increase the time taken to load JSON. The more JSON extracted, the longer it takes.
- Flat JSON can only support materializing common keys in JSON Objects, not keys in JSON Arrays.
- Flat JSON does not change the data sorting method, so query performance and data compression rate will still be affected by data sorting. To achieve optimal performance, further adjustments to data sorting may be necessary.
Version Notes
The database shared-nothing clusters support Flat JSON starting from v3.3.0, and shared-data clusters support it from v3.3.3. In versions v3.3.0, v3.3.1, and v3.3.2:- When loading data, it supports extracting common fields and storing them separately as JSON types, without type inference.
- Both the extracted columns and the original JSON data are stored. The extracted data will be deleted along with the original data.
- The results extracted by Flat JSON are divided into common columns and reserved field columns. When all JSON Schemas are consistent, no reserved field columns are generated.
- Flat JSON only stores common field columns and reserved field columns, without additionally storing the original JSON data.
- When loading data, common fields will automatically infer types as BIGINT/LARGEINT/DOUBLE/STRING. Unrecognized types will be inferred as JSON types, and reserved field columns will be stored as JSON types.