not exists优化 mysql数据存在不插入
Posted 时光荏苒,白驹过隙。且行且珍惜!!!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了not exists优化 mysql数据存在不插入相关的知识,希望对你有一定的参考价值。
记一次sql优化
原代码
INSERT INTO DataAnalysis_HrmDataHistory (estate,lockstate,delestate,creator,created,modifier,modified,name,employeeCode,sex,department_id,place_id,onDutyTime,outDutyTime,isDimission,birthday) SELECT estate,lockstate,delestate,creator,created,modifier,modified,chineseName,employeeCode,sex,department_id,subCompany,onDutyTime,outDutyTime,isDimission,birthday FROM ( SELECT * FROM Hrm_HrmData WHERE (isDimission = 79 AND onDutyTime < date_add(‘2020-0-01‘, interval 1 MONTH)) OR (isDimission = 80 AND outDutyTime >= date_add(‘2020-01-01‘, interval 1 MONTH)) ) h WHERE NOT EXISTS ( SELECT * FROM DataAnalysis_HrmDataHistory WHERE DataAnalysis_HrmDataHistory.employeeCode = h.employeeCode )
INSERT INTO DataAnalysis_HrmDataHistory (estate,lockstate,delestate,creator,created,modifier,modified,name,employeeCode,sex,department_id,place_id,onDutyTime,outDutyTime,isDimission,birthday) SELECT estate,lockstate,delestate,creator,created,modifier,modified,chineseName,employeeCode,sex,department_id,subCompany,onDutyTime,outDutyTime,isDimission,birthday FROM ( SELECT * FROM Hrm_HrmData WHERE (isDimission = 79 AND onDutyTime < date_add(‘2020-01-01‘, interval 1 MONTH)) OR (isDimission = 80 AND outDutyTime >= date_add(‘2020-01-01‘, interval 1 MONTH)) ) h WHERE h.employeeCode not in ( SELECT employeeCode FROM DataAnalysis_HrmDataHistory )
以上是关于not exists优化 mysql数据存在不插入的主要内容,如果未能解决你的问题,请参考以下文章
MySQL优化--NOT EXISTS和LEFT JOIN方式差异
MySQL 当记录不存在时插入(insert if not exists)
Sql语句优化-查询两表不同行NOT INNOT EXISTS连接查询Left Join
MySQL 当记录不存在时插入(insert if not exists)
关于T-SQL中exists或者not exists子查询的“伪优化”的做法
MySQL错误:The user specified as a definer (XXX@XXX) does not exist