基于另一表值的两表全外连接SQL查询需求(Teradata/Oracle)
SQL Query to Join CONTS and VALID Tables (Teradata/Oracle Compatible, Scalable for Millions of Rows)
Looking at your requirements, we need to pair records from the CONTS table based on the mappings defined in VALID (where each ID in VALID maps a source SRC_ID to a target VALID_ID), and return all combinations of values—with NULLs where a matching value doesn't exist in one of the source/target sets.
Approach
The core idea is to:
- Extract the source-target
SRC_IDmappings fromVALID - Isolate the relevant records from
CONTSfor both source and targetSRC_IDs - Use a
FULL OUTER JOINto pair records byIDandVAL, ensuring we don't miss any values present in either set
Scalable SQL Query
This query works efficiently for large datasets (millions of rows) and is compatible with both Teradata and Oracle:
WITH valid_mappings AS ( SELECT ID, SRC_ID AS src_id1, VALID_ID AS src_id2 FROM VALID ), conts_src1 AS ( SELECT ID, SRC_ID, VAL FROM CONTS WHERE EXISTS ( SELECT 1 FROM valid_mappings vm WHERE vm.ID = CONTS.ID AND vm.src_id1 = CONTS.SRC_ID ) ), conts_src2 AS ( SELECT ID, SRC_ID, VAL FROM CONTS WHERE EXISTS ( SELECT 1 FROM valid_mappings vm WHERE vm.ID = CONTS.ID AND vm.src_id2 = CONTS.SRC_ID ) ) SELECT COALESCE(c1.ID, c2.ID) AS ID, c1.SRC_ID AS SRC_ID1, c1.VAL AS VAL1, c2.SRC_ID AS SRC_ID2, c2.VAL AS VAL2 FROM conts_src1 c1 FULL OUTER JOIN conts_src2 c2 ON c1.ID = c2.ID AND c1.VAL = c2.VAL ORDER BY COALESCE(c1.ID, c2.ID), COALESCE(c1.VAL, c2.VAL);
Breakdown of the Query
valid_mappings: Converts theVALIDtable into a clear source-target mapping for eachID(sinceIDis the primary key, eachIDhas exactly one mapping)conts_src1/conts_src2: FiltersCONTSto only include records that are part of a valid mapping, reducing the dataset size before the join (critical for performance with large tables)FULL OUTER JOIN: Ensures we capture everyVALvalue that exists in either the source or targetSRC_IDgroup for eachID—missing values are filled withNULLCOALESCE: Handles cases where one side of the join has no matching record, ensuring we always get a validIDand consistent sort order
Performance Optimization Tips
To ensure this query runs efficiently on millions of rows:
- Indexing: Create a composite index on
CONTSfor(ID, SRC_ID, VAL)—this speeds up the filtered subqueries and the join operation:-- Teradata/Oracle compatible index creation CREATE INDEX idx_conts_id_src_val ON CONTS(ID, SRC_ID, VAL); - Statistics: In Teradata, run
COLLECT STATISTICS ON CONTS COLUMN (ID, SRC_ID, VAL);to help the optimizer generate an optimal execution plan. In Oracle, useDBMS_STATS.GATHER_TABLE_STATSfor the same purpose. - Join Hints (Oracle): For extremely large datasets, you can add a hash join hint to prioritize hash-based joining (more efficient for big data):
SELECT /*+ USE_HASH(c1 c2) */ COALESCE(c1.ID, c2.ID) AS ID, -- rest of the select columns
Example Output
When run against your sample data, this query will produce exactly the result set you specified.
内容的提问来源于stack exchange,提问作者DBUser
相关产品推荐
相关产品推荐

