处理很多大数据量插入重复数据有唯一键时使用,这种用法只能用于mysql中,其他数据库不适用
1、在xml文件中常用写法:
<update id="UPDATE_HOTEL_COUNTY_DISTRICT" parameterType="java.lang.String">
INSERT INTO VST_SEARCH_PROD_HOTEL(PRODUCT_ID,COUNTY_DISTRICT_ID,COUNTY_DISTRICT_NAME)
(
SELECT
t1.PRODUCT_ID,
t2.DISTRICT_ID COUNTY_DISTRICT_ID,
t2.DISTRICT_NAME COUNTY_DISTRICT_NAME
FROM
VST_SEARCH_PROD_HOTEL t1
LEFT JOIN VST_SEARCH_DISTRICT_LEVEL t2 ON t1.DISTRICT_ID = t2.DISTRICT_ID
WHERE t2.DISTRICT_TYPE ='COUNTY'
<if test="_parameter != null">
AND t1.PRODUCT_ID IN (${_parameter})
</if>
)
ON DUPLICATE KEY UPDATE
COUNTY_DISTRICT_ID = VALUES(COUNTY_DISTRICT_ID),
COUNTY_DISTRICT_NAME = VALUES(COUNTY_DISTRICT_NAME)
</update>
2、在java代码中常用拼sql的方式实现:
public synchronized static void insertBatch(List<RealTimePriceBean> list) {
StringBuffer sql = new StringBuffer();
StringBuffer subsql = new StringBuffer();
sql.append("INSERT INTO VST_HOTEL_REAL_TIME_PRICE (")//
.append("PRODUCT_ID,")//
.append("REAL_TIME_PRICE1,")//
.append("REAL_TIME_PRICE2,")//
.append("REAL_TIME_REMAIN1,")//
.append("REAL_TIME_REMAIN2,")//
.append("SELL_OUT_FLAG1,")//
.append("SELL_OUT_FLAG2,")//
.append("UPDATE_TIME) VALUES");//
for (RealTimePriceBean realTimePriceBean : list) {
sql.append("(")//
.append(realTimePriceBean.getProductId()).append(",")//
.append(realTimePriceBean.getRealTimePrice1()).append(",")//
.append(realTimePriceBean.getRealTimePrice2()).append(",")//
.append(realTimePriceBean.getRealTimeRemain1()).append(",")//
.append(realTimePriceBean.getRealTimeRemain2()).append(",")//
.append(StringUtils.isBlank(realTimePriceBean.getSellOutFlag1()) ? null : "'" + realTimePriceBean.getSellOutFlag1() + "'").append(",")//
.append(StringUtils.isBlank(realTimePriceBean.getSellOutFlag2()) ? null : "'" + realTimePriceBean.getSellOutFlag2() + "'").append(",")//
.append("NOW()")//
.append("),");//
}
String newsql = sql.substring(0, sql.length() - 1);
subsql.append(" ON DUPLICATE KEY UPDATE ")//
.append("REAL_TIME_PRICE1 = VALUES(REAL_TIME_PRICE1),")//
.append("REAL_TIME_PRICE2 = VALUES(REAL_TIME_PRICE2),")//
.append("REAL_TIME_REMAIN1 = VALUES(REAL_TIME_REMAIN1),")//
.append("REAL_TIME_REMAIN2 = VALUES(REAL_TIME_REMAIN2),")//
.append("SELL_OUT_FLAG1 = VALUES(SELL_OUT_FLAG1),")//
.append("SELL_OUT_FLAG2 = VALUES(SELL_OUT_FLAG2),")//
.append("UPDATE_TIME = VALUES(UPDATE_TIME)");//
executeBatch(newsql + subsql);
}
3、直接在客户端上使用时可以通过过:
INSERT INTO appointment (book_id,student_id,appoint_time)
VALUES (1, 8, NOW()) ON DUPLICATE KEY
UPDATE student_id=8;
4、有些情况可以忽略主键,如果存在则不插入:
INSERT IGNORE INTO appointment (book_id,student_id,appoint_time) VALUES (1000, 8, NOW())
注:生产中发现使用 updae 更新数据2000/s 使用 insert into ... duplicate key 更新速度为40000/s ,差别非常明显,推荐使用 insert into ... duplicate key更新数据