用 left join 替换 not in
Replacing not in with left join
我希望更正 mySQL 数据库中左连接的语法。我的查询运行但速度很慢,我不确定我的左连接是否正确。我需要 select consumer_id 来对抗之前的成功。为了检查以前的成功,我必须加入记录 table 和映射 table 到 link 到消费者 ID 的映射 ID 仅存在于 event_queue table。非常感谢您的帮助。
第一个查询:
SELECT
CEQ.consumer_ID
, F.client_license_ID
, F.footprint_ID
, F.event_token_ID
, EM.event_ID
, EM.mapping_ID em_ID
, EM.export_value campaign_number
, EM.export_value_2 sequence_number
, EM.export_value_3 campaign_number_2
, EM.export_value_4 sequence_number_2
, EM.export_value_5 ffs_event_id
, EM.export_value_6
, EM.export_value_7
, EM.export_value_8
, EM.export_value_9
, EM.export_value_10
FROM data_transfer.Event_Mappings EM
JOIN data_transfer.Event_Queue CEQ ON CEQ.event_ID = EM.event_ID
JOIN Footprints F ON CEQ.consumer_ID = F.consumer_ID
LEFT JOIN efn_data_transfer.CRM_Records LCR ON LCR.consumer_ID = CEQ.consumer_ID
WHERE EM.data_transfer_ID = 24
AND EM.mode = 'production'
AND EM.active_flag = 1
AND F.sample_flag = 0
AND CEQ.modify_DTM > (SELECT last_transfer FROM (SELECT CAST(DATE_SUB(start,INTERVAL 3 DAY) AS CHAR) last_transfer
FROM data_transfer.DT_Runs DTR
WHERE DTR.data_transfer_ID = 24
AND DTR.result = 1
AND DTR.mode = 'production'
ORDER BY DTR.dt_run_ID DESC
LIMIT 1)as D
)
GROUP BY
CEQ.consumer_ID
, F.client_license_ID
, F.footprint_ID
, F.event_token_ID
, EM.event_ID
, EM.mapping_ID
, EM.export_value
, EM.export_value_2
, EM.export_value_3
, EM.export_value_4
, EM.export_value_5
, EM.export_value_6
, EM.export_value_7
, EM.export_value_8
, EM.export_value_9
, EM.export_value_10
ORDER BY F.client_license_ID, CEQ.consumer_ID, EM.mapping_ID, CEQ.modify_DTM;
组合查询:
SELECT DISTINCT CAST(CEQ.consumer_ID AS CHAR) AS consumer_ID_1
FROM data_transfer.Event_Queue CEQ
WHERE 0=0
AND CEQ.consumer_ID NOT IN (SELECT RR.consumer_ID FROM data_transfer.Records RR
JOIN data_transfer.Event_Mappings EM ON RR.event_mapping_ID = EM.event_mapping_ID
WHERE 0=0
AND RR.failure_code = 0
AND RR.mode = 'production'
AND RR.`ignore` = 0
AND RR.response_code = 'SUCCESS'
AND RR.data_transfer_ID = 24
AND RR.consumer_ID = ?
AND EM.event_ID = ?
)
AND CEQ.consumer_ID = ?
尝试查询:
SELECT
DISTINCT
EM.event_ID
, F.footprint_ID
, F.event_token_ID
FROM data_transfer.Event_Mappings EM
JOIN data_transfer.Event_Queue CEQ ON CEQ.event_ID = EM.event_ID
JOIN Footprints F ON CEQ.consumer_ID = F.consumer_ID
LEFT JOIN efn_data_transfer.CRM_Records LCR ON LCR.consumer_ID = CEQ.consumer_ID
LEFT JOIN (
SELECT DISTINCT CEQ.consumer_ID AS consumer_ID_1
FROM data_transfer.Event_Queue CEQ, data_transfer.Records RR
LEFT JOIN data_transfer.Mappings EM ON EM.mapping_id = RR.mapping_id
WHERE RR.consumer_id = CEQ.consumer_id
AND RR.failure_code = 0
AND RR.mode = 'production'
AND RR.`ignore` = 0
AND RR.response_code = 'SUCCESS'
AND RR.data_transfer_ID = 24
) AS RECORDS ON CEQ.consumer_id
WHERE EM.data_transfer_ID = 24
AND EM.mode = 'production'
AND EM.active_flag = 1
AND F.sample_flag = 0
AND CEQ.modify_DTM > (SELECT last_transfer FROM (SELECT CAST(DATE_SUB(start,INTERVAL 3 DAY) AS CHAR) last_transfer
FROM data_transfer.DT_Runs DTR
WHERE DTR.data_transfer_ID = 24
AND DTR.result = 1
AND DTR.mode = 'production'
ORDER BY DTR.dt_run_ID DESC
LIMIT 1) AS D
)
GROUP BY
CEQ.consumer_ID
, F.client_license_ID
, F.footprint_ID
, F.event_token_ID
, EM.event_ID
, EM.mapping_ID
, EM.export_value
, EM.export_value_2
, EM.export_value_3
, EM.export_value_4
, EM.export_value_5
, EM.export_value_6
, EM.export_value_7
, EM.export_value_8
, EM.export_value_9
, EM.export_value_10
ORDER BY F.client_license_ID, CEQ.consumer_ID, EM.mapping_ID, CEQ.modify_DTM;
您的子查询不依赖于任何特定的东西,可以是它自己的查询,作为 "FROM" 子句的一部分。由于它仅通过限制返回 1 行,因此您不需要将其加入任何内容。对于查询的其余部分,这只会产生一次日期计算值。
对于您的表,我提供了以下建议的索引,以帮助针对相应的 WHERE 和 JOIN 标准进行优化。
Table Index
Event_Mappings ( data_transfer_id, active_flag, mode, Event_id )
Event_Queue ( Event_id, consumer_ID, modify_DTM )
Footprints ( consumer_ID, sample_flag, client_license_ID )
DT_Runs ( data_transfer_id, result, mode, dt_run_id, start )
Records ( data_transfer_id, failure_code, mode, ignore, response_code )
SELECT DISTINCT
CEQ.consumer_ID,
F.client_license_ID,
F.footprint_ID,
F.event_token_ID,
EM.event_ID,
EM.mapping_ID em_ID,
EM.export_value campaign_number,
EM.export_value_2 sequence_number,
EM.export_value_3 campaign_number_2,
EM.export_value_4 sequence_number_2,
EM.export_value_5 ffs_event_id,
EM.export_value_6,
EM.export_value_7,
EM.export_value_8,
EM.export_value_9,
EM.export_value_10
FROM
data_transfer.Event_Mappings EM
JOIN data_transfer.Event_Queue CEQ
ON EM.event_ID = CEQ.event_ID
JOIN Footprints F
ON CEQ.consumer_ID = F.consumer_ID
AND F.sample_flag = 0
LEFT JOIN efn_data_transfer.CRM_Records LCR
ON CEQ.consumer_ID = LCR.consumer_ID
LEFT JOIN ( SELECT DISTINCT
CEQ.consumer_ID AS consumer_ID_1
FROM
data_transfer.Records RR
LEFT JOIN data_transfer.Mappings EM
ON RR.mapping_id = EM.mapping_id
JOIN data_transfer.Event_Queue CEQ
ON RR.consumer_id = CEQ.consumer_id
WHERE
RR.data_transfer_ID = 24
AND RR.failure_code = 0
AND RR.mode = 'production'
AND RR.`ignore` = 0
AND RR.response_code = 'SUCCESS' ) AS RECORDS
ON CEQ.consumer_id = RECORDS.consumer_id_1,
( SELECT
CAST(DATE_SUB(start,INTERVAL 3 DAY) AS CHAR) last_transfer
FROM
data_transfer.DT_Runs DTR
WHERE
DTR.data_transfer_ID = 24
AND DTR.result = 1
AND DTR.mode = 'production'
ORDER BY
DTR.dt_run_ID DESC
LIMIT 1 ) as LT
WHERE
EM.data_transfer_ID = 24
AND EM.mode = 'production'
AND EM.active_flag = 1
AND CEQ.modify_DTM > LT.Last_Transfer
AND RECORDS.consumer_id_1 IS NULL
ORDER BY
F.client_license_ID,
CEQ.consumer_ID,
EM.mapping_ID,
CEQ.modify_DTM;
修改为包含您的 RECORDS 别名左连接...然后,查找 RECORDS "consumer_id_1" 列为 NULL(因此 NOT IN)
我希望更正 mySQL 数据库中左连接的语法。我的查询运行但速度很慢,我不确定我的左连接是否正确。我需要 select consumer_id 来对抗之前的成功。为了检查以前的成功,我必须加入记录 table 和映射 table 到 link 到消费者 ID 的映射 ID 仅存在于 event_queue table。非常感谢您的帮助。
第一个查询:
SELECT
CEQ.consumer_ID
, F.client_license_ID
, F.footprint_ID
, F.event_token_ID
, EM.event_ID
, EM.mapping_ID em_ID
, EM.export_value campaign_number
, EM.export_value_2 sequence_number
, EM.export_value_3 campaign_number_2
, EM.export_value_4 sequence_number_2
, EM.export_value_5 ffs_event_id
, EM.export_value_6
, EM.export_value_7
, EM.export_value_8
, EM.export_value_9
, EM.export_value_10
FROM data_transfer.Event_Mappings EM
JOIN data_transfer.Event_Queue CEQ ON CEQ.event_ID = EM.event_ID
JOIN Footprints F ON CEQ.consumer_ID = F.consumer_ID
LEFT JOIN efn_data_transfer.CRM_Records LCR ON LCR.consumer_ID = CEQ.consumer_ID
WHERE EM.data_transfer_ID = 24
AND EM.mode = 'production'
AND EM.active_flag = 1
AND F.sample_flag = 0
AND CEQ.modify_DTM > (SELECT last_transfer FROM (SELECT CAST(DATE_SUB(start,INTERVAL 3 DAY) AS CHAR) last_transfer
FROM data_transfer.DT_Runs DTR
WHERE DTR.data_transfer_ID = 24
AND DTR.result = 1
AND DTR.mode = 'production'
ORDER BY DTR.dt_run_ID DESC
LIMIT 1)as D
)
GROUP BY
CEQ.consumer_ID
, F.client_license_ID
, F.footprint_ID
, F.event_token_ID
, EM.event_ID
, EM.mapping_ID
, EM.export_value
, EM.export_value_2
, EM.export_value_3
, EM.export_value_4
, EM.export_value_5
, EM.export_value_6
, EM.export_value_7
, EM.export_value_8
, EM.export_value_9
, EM.export_value_10
ORDER BY F.client_license_ID, CEQ.consumer_ID, EM.mapping_ID, CEQ.modify_DTM;
组合查询:
SELECT DISTINCT CAST(CEQ.consumer_ID AS CHAR) AS consumer_ID_1
FROM data_transfer.Event_Queue CEQ
WHERE 0=0
AND CEQ.consumer_ID NOT IN (SELECT RR.consumer_ID FROM data_transfer.Records RR
JOIN data_transfer.Event_Mappings EM ON RR.event_mapping_ID = EM.event_mapping_ID
WHERE 0=0
AND RR.failure_code = 0
AND RR.mode = 'production'
AND RR.`ignore` = 0
AND RR.response_code = 'SUCCESS'
AND RR.data_transfer_ID = 24
AND RR.consumer_ID = ?
AND EM.event_ID = ?
)
AND CEQ.consumer_ID = ?
尝试查询:
SELECT
DISTINCT
EM.event_ID
, F.footprint_ID
, F.event_token_ID
FROM data_transfer.Event_Mappings EM
JOIN data_transfer.Event_Queue CEQ ON CEQ.event_ID = EM.event_ID
JOIN Footprints F ON CEQ.consumer_ID = F.consumer_ID
LEFT JOIN efn_data_transfer.CRM_Records LCR ON LCR.consumer_ID = CEQ.consumer_ID
LEFT JOIN (
SELECT DISTINCT CEQ.consumer_ID AS consumer_ID_1
FROM data_transfer.Event_Queue CEQ, data_transfer.Records RR
LEFT JOIN data_transfer.Mappings EM ON EM.mapping_id = RR.mapping_id
WHERE RR.consumer_id = CEQ.consumer_id
AND RR.failure_code = 0
AND RR.mode = 'production'
AND RR.`ignore` = 0
AND RR.response_code = 'SUCCESS'
AND RR.data_transfer_ID = 24
) AS RECORDS ON CEQ.consumer_id
WHERE EM.data_transfer_ID = 24
AND EM.mode = 'production'
AND EM.active_flag = 1
AND F.sample_flag = 0
AND CEQ.modify_DTM > (SELECT last_transfer FROM (SELECT CAST(DATE_SUB(start,INTERVAL 3 DAY) AS CHAR) last_transfer
FROM data_transfer.DT_Runs DTR
WHERE DTR.data_transfer_ID = 24
AND DTR.result = 1
AND DTR.mode = 'production'
ORDER BY DTR.dt_run_ID DESC
LIMIT 1) AS D
)
GROUP BY
CEQ.consumer_ID
, F.client_license_ID
, F.footprint_ID
, F.event_token_ID
, EM.event_ID
, EM.mapping_ID
, EM.export_value
, EM.export_value_2
, EM.export_value_3
, EM.export_value_4
, EM.export_value_5
, EM.export_value_6
, EM.export_value_7
, EM.export_value_8
, EM.export_value_9
, EM.export_value_10
ORDER BY F.client_license_ID, CEQ.consumer_ID, EM.mapping_ID, CEQ.modify_DTM;
您的子查询不依赖于任何特定的东西,可以是它自己的查询,作为 "FROM" 子句的一部分。由于它仅通过限制返回 1 行,因此您不需要将其加入任何内容。对于查询的其余部分,这只会产生一次日期计算值。
对于您的表,我提供了以下建议的索引,以帮助针对相应的 WHERE 和 JOIN 标准进行优化。
Table Index
Event_Mappings ( data_transfer_id, active_flag, mode, Event_id )
Event_Queue ( Event_id, consumer_ID, modify_DTM )
Footprints ( consumer_ID, sample_flag, client_license_ID )
DT_Runs ( data_transfer_id, result, mode, dt_run_id, start )
Records ( data_transfer_id, failure_code, mode, ignore, response_code )
SELECT DISTINCT
CEQ.consumer_ID,
F.client_license_ID,
F.footprint_ID,
F.event_token_ID,
EM.event_ID,
EM.mapping_ID em_ID,
EM.export_value campaign_number,
EM.export_value_2 sequence_number,
EM.export_value_3 campaign_number_2,
EM.export_value_4 sequence_number_2,
EM.export_value_5 ffs_event_id,
EM.export_value_6,
EM.export_value_7,
EM.export_value_8,
EM.export_value_9,
EM.export_value_10
FROM
data_transfer.Event_Mappings EM
JOIN data_transfer.Event_Queue CEQ
ON EM.event_ID = CEQ.event_ID
JOIN Footprints F
ON CEQ.consumer_ID = F.consumer_ID
AND F.sample_flag = 0
LEFT JOIN efn_data_transfer.CRM_Records LCR
ON CEQ.consumer_ID = LCR.consumer_ID
LEFT JOIN ( SELECT DISTINCT
CEQ.consumer_ID AS consumer_ID_1
FROM
data_transfer.Records RR
LEFT JOIN data_transfer.Mappings EM
ON RR.mapping_id = EM.mapping_id
JOIN data_transfer.Event_Queue CEQ
ON RR.consumer_id = CEQ.consumer_id
WHERE
RR.data_transfer_ID = 24
AND RR.failure_code = 0
AND RR.mode = 'production'
AND RR.`ignore` = 0
AND RR.response_code = 'SUCCESS' ) AS RECORDS
ON CEQ.consumer_id = RECORDS.consumer_id_1,
( SELECT
CAST(DATE_SUB(start,INTERVAL 3 DAY) AS CHAR) last_transfer
FROM
data_transfer.DT_Runs DTR
WHERE
DTR.data_transfer_ID = 24
AND DTR.result = 1
AND DTR.mode = 'production'
ORDER BY
DTR.dt_run_ID DESC
LIMIT 1 ) as LT
WHERE
EM.data_transfer_ID = 24
AND EM.mode = 'production'
AND EM.active_flag = 1
AND CEQ.modify_DTM > LT.Last_Transfer
AND RECORDS.consumer_id_1 IS NULL
ORDER BY
F.client_license_ID,
CEQ.consumer_ID,
EM.mapping_ID,
CEQ.modify_DTM;
修改为包含您的 RECORDS 别名左连接...然后,查找 RECORDS "consumer_id_1" 列为 NULL(因此 NOT IN)