在MYSQL中有效地更新10k记录表

时间:2018-01-11 13:50:07

标签: mysql sql query-optimization

我有两个表,表A和表B.表A有10k记录。表B有那些10K的旧记录和附加记录。我需要更新表B中的旧10k记录。

我用过,

UPDATE CONTENT_WORKFLOW_MASTER CWM, CWSCOPY TMP SET
CWM.PROGRAM_ID = TMP.PROGRAM_ID,
CWM.PROGRAM_TYPE_ID = TMP.PROGRAM_TYPE_ID, 
CWM.PROGRAM_TYPE_NAME = TMP.PROGRAM_TYPE_NAME, 
CWM.ORIGINAL_TITLE = TMP.ORIGINAL_TITLE, 
CWM.SOURCE_GROUP_ID = TMP.SOURCE_GROUP_ID, 
CWM.MASTER_TITLE = TMP.MASTER_TITLE, 
CWM.PROGRAM_LANGUAGE = TMP.PROGRAM_LANGUAGE, 
CWM.REGION = TMP.REGION, 
CWM.CW_STATUS = 'NEW', 
CWM.COPY_CULTURE = TMP.COPY_CULTURE, 
CWM.GENRES = TMP.GENRES, 
CWM.AIR_DATE_TIME = TMP.AIR_DATE_TIME, 
CWM.PROCESS_ID = TMP.GUID, 
CWM.PRIORITY = 0, 
CWM.UPDATED_BY_SCHEDULE = 1, 
CWM.MODIFIED_USER = 'StoredProcUser', 
CWM.MODIFIED_DATE = NOW() WHERE CWM.PROGRAM_ID = TMP.PROGRAM_ID ;

我用过,

UPDATE CONTENT_WORKFLOW_MASTER CWM INNER JOIN t.temp TMP 
USING (PROGRAM_ID) 
SET 
CWM.PROGRAM_ID = TMP.PROGRAM_ID,
CWM.PROGRAM_TYPE_ID = TMP.PROGRAM_TYPE_ID, 
CWM.PROGRAM_TYPE_NAME = TMP.PROGRAM_TYPE_NAME, 
CWM.ORIGINAL_TITLE = TMP.ORIGINAL_TITLE, 
CWM.SOURCE_GROUP_ID = TMP.SOURCE_GROUP_ID, 
CWM.MASTER_TITLE = TMP.MASTER_TITLE, 
CWM.PROGRAM_LANGUAGE = TMP.PROGRAM_LANGUAGE, 
CWM.REGION = TMP.REGION, 
CWM.CW_STATUS = 'NEW', 
CWM.COPY_CULTURE = TMP.COPY_CULTURE, 
CWM.GENRES = TMP.GENRES, 
CWM.AIR_DATE_TIME = TMP.AIR_DATE_TIME, 
CWM.PROCESS_ID = TMP.GUID, 
CWM.PRIORITY = 0, 
CWM.UPDATED_BY_SCHEDULE = 1, 
CWM.MODIFIED_USER = 'StoredProcUser', 
CWM.MODIFIED_DATE = NOW() WHERE CWM.USER_LOCKED=0;

create TEMPORARY table t.temp AS
SELECT DISTINCT CWS.CONTENT_WORKFLOW_STAGING_ID,CWS.PROGRAM_ID,CWS.SOURCE_GROUP_NAME,CWS.COPY_CULTURE,
CWS.PROGRAM_TYPE_ID,  CWS.PROGRAM_TYPE_NAME, CWS.ORIGINAL_TITLE, CWS.SOURCE_GROUP_ID, CWS.MASTER_TITLE, 
CWS.PROGRAM_LANGUAGE, CWS.REGION, CWS.GENRES, CWS.AIR_DATE_TIME, CWS.GUID,CWS.IS_PROCESSED, CWS.INTERNAL_TRANSACTION_ID 
FROM CONTENT_WORKFLOW_STAGING CWS INNER JOIN CONTENT_WORKFLOW_MASTER CWM ON
CWM.PROGRAM_ID = CWS.PROGRAM_ID AND 
CWM.SOURCE_GROUP_NAME = CWS.SOURCE_GROUP_NAME AND CWM.COPY_CULTURE = CWS.COPY_CULTURE AND CWM.USER_LOCKED = 0 
AND 
CWS.IS_PROCESSED = 0 AND CWS.INTERNAL_TRANSACTION_ID = INTERNAL_TRANSACTION_ID;

这两个查询都不是完全有效的。任何人都建议有效的方法。至少10秒内

0 个答案:

没有答案