Overview
A cardinality-preserving join ensures that the cardinality and duplication factor of the output rows remain the same as those of one of the input tables in the join. Consider the following examples:-
Inner Join:
In this case,
A.fk(Foreign Key) is NOT NULL and referencesB.pk(Primary Key). Each row inAmatches exactly one row inB, so the cardinality and duplication factor of the output match those ofA. -
Left Join:
Here,
A.fkreferencesB.pk, butA.fkcan contain NULL values. Each row inAmatches at most one row inB. As a result, the cardinality and duplication factor of the output remain aligned withA.
In these types of joins, if the final output columns only depend on columns from table
A, and the columns from table B are not used, table B can be pruned from the join. Since v3.1, the database supports table pruning in cardinality-preserving joins, which can occur in Common Table Expressions (CTEs), logical views, and subqueries.
Use case: Real-time feature selection in risk control
The table pruning feature for cardinality-preserving join is particularly useful in scenarios like real-time feature selection for risk control. In this context, users need to select data from a large number of tables, often dealing with a combinatorial explosion of columns and tables. The following characteristics are common in the risk control domain:- Numerous features are spread across many independently updated tables.
- Fresh data must be visible and queryable in real time.
- A flat logical view is used to simplify the data model, making SQL for column extraction more succinct and productive.
Feature support
The table pruning feature supports multi-table joins in both star schema and snowflake schema. Multi-table joins can appear in CTEs, logical views, and subqueries, allowing for more efficient query execution. Currently, the table pruning feature is only supported on OLAP tables and Cloud-native tables. External tables in multi-join can not be pruned.Usage
The following examples use the TPC-H dataset.Prerequisites
To use the table pruning feature, the following conditions must be met:- Enable table pruning
- Set key constraints
Enable table pruning
By default, table pruning is disabled. You need to enable the feature by configuring the following session variables:Set key constraints
Tables to be pruned must have Unique Key or Primary Key constraints at least in LEFT or RIGHT Joins. To prune tables in INNER JOIN, you must define Foreign Key constraints in addition to Unique Key or Primary Key constraints. Primary Key tables and Unique Key tables have their implicit Primary Key or Unique Key constraints naturally built into them. However, for Duplicate Key tables, you must manually define Unique Key constraints, and ensure that no duplicate rows exist. Note that the database does not enforce Unique Key constraints on Duplicate Key tables. Instead, it treats them as optimization hints for more aggressive query planning. Example:Table pruning in LEFT/RIGHT JOINs based on Unique or Primary Keys
Table pruning in LEFT or RIGHT JOINs does not require that the retained side of the join has a Foreign Key referencing the pruned side. This makes pruning more flexible and robust, even if reference integrity cannot be guaranteed. Pruning in LEFT/RIGHT JOINs based on Unique or Primary keys has less strict requirements compared to INNER JOIN pruning based on Foreign Keys. The conditions for pruning are:- Pruned Side The pruned table must be the right side in a LEFT JOIN or the left side in a RIGHT JOIN.
-
Join Conditions
The join must use equality conditions (
=) only, and the joined columns of the pruned side must be a superset of the Unique or Primary keys. - Output Columns Only the columns from the retained side should be output, and the result should maintain the same cardinality and duplication factor as the retained side.
- NULL/Default Values The joined columns in the retained side can contain NULL or other default values that do not match the pruned side.
-
Create tables and insert data.
-
View the logical execution plans of the queries.
Table Pruning in INNER/LEFT/RIGHT JOINs Based on Foreign Keys
Table pruning in INNER JOINs is more restrictive because the retained side must have a Foreign Key that references the pruned side, and reference integrity must be ensured. Currently, the database does not enforce consistency checks on Foreign Key constraints. Although table pruning based on Foreign Keys is more strict, it is also more powerful. It allows the optimizer to leverage column equivalence inference in INNER JOINs, making pruning possible in more complex scenarios. The conditions for pruning are as follows:-
Pruned Side
- In a LEFT JOIN, the pruned table must be on the right side. In a RIGHT JOIN, it must be on the left side.
- In an INNER JOIN, the pruned table must have a Unique Key constraint on the joined columns, and each row in the retained side must match exactly one row in the pruned side. If unmatched rows exist in the retained side, pruning cannot occur.
-
Join Conditions
The join must use equality conditions (
=) only. The joined columns in the retained side must be Foreign Keys, while the joined columns in the pruned side must be Primary Keys or Unique Keys. These columns must align according to the Foreign Key constraints. -
Output Columns
- Only the columns from the retained side should be output, and the result should maintain the same cardinality and duplication factor as the retained side.
- For INNER JOINs, joined columns from the pruned side can be substituted by their equivalent columns in the retained side. If all output columns from the pruned side are joined columns, pruning can also occur.
- NULL/Default Values: Joined columns in the retained side cannot contain NULL or other default values that do not match the pruned side.
-
Create tables, define the Foreign Key, and insert data.
-
View the logical execution plans of the queries.