mysql 대량 업데이트 두 가지 방식 효율 시험
소개:
mysql에 mybits 프레임워크를 탑재하여 대량으로 업데이트하는 두 가지 방식이 있는데 하나는 xml에서 전체 업데이트 문장을 순환하고 중간에';'칸막이를 하고 또 하나는casewhen을 사용하여 대량 업데이트를 실현하는데 현재 두 가지 방법의 효율을 비교한다.
두 가지 쓰기 방법:
방법1: 전체 순환 업데이트 문장
주의: mysql은 기본적으로 sql를 미리 컴파일하는 것을 지원하지 않습니다.'분할 이런 방식으로 실행하려면 mysql의 jdbcUrl 매개 변수에 allowMultiQueries=true 오픈 지원을 추가해야 합니다
SQL은 다음과 같습니다.
<update id="updateByForech" parameterType="java.util.List">
<foreach collection="list" item="item" >
update t_user
<set>
<if test="item.tName != null and item.tName != ''">
t_name = #{item.tName,jdbcType=VARCHAR},
if>
<if test="item.tAge != null">
t_age = #{item.tAge},
if>
<if test="item.tAddress != null">
t_address = #{item.tAddress,jdbcType=VARCHAR},
if>
<if test="item.tPwd!= null">
t_pwd = #{item.tPwd,jdbcType=VARCHAR},
if>
set>
where t_id = #{item.tId};
foreach>
update>
이 방법은 작성이 간단하고 이해하기 쉽지만 시스템 파라미터를 수정해야 한다.
방법2:case when 방식으로 연결하기
SQL은 다음과 같습니다.
<update id="updateByCaseWhen" parameterType="java.util.List">
update t_user
<trim prefix="set" suffixOverrides=",">
<trim prefix="t_name =case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.tName !=null and item.tName != ''">
when t_id=#{item.tId} then #{item.tName,jdbcType=VARCHAR}
if>
foreach>
trim>
<trim prefix="t_address =case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.tAddress != null and item.tAddress != ''">
when t_id=#{item.tId} then #{item.tAddress,jdbcType=VARCHAR}
if>
foreach>
trim>
<trim prefix="t_age =case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.tAge != null">
when t_id=#{item.tId} then #{item.tAge}
if>
foreach>
trim>
<trim prefix="t_pwd =case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.tPwd != null">
when t_id=#{item.tId} then #{item.tPwd}
if>
foreach>
trim>
trim>
where t_id in
<foreach collection="list" item="item" separator="," open="(" close=")">
#{item.tId}
foreach>
update>
이 방법은 sql 연결이 복잡하여 업데이트할 필드마다 id값을 나열합니다. 데이터에 문제가 생기면 포지셔닝이 어렵습니다.
효율성 통계:
요약:
1. 한 번의 수정 데이터량이 비교적 적은 상황에서casewhen 방식과 전체 업데이트 방식은 모두 선택할 수 있지만 전체 업데이트는 시스템 파라미터 설정을 추가해야 한다.
2. 데이터 문제가 자주 발생하는 경우 전체적인 순환 업데이트 방식을 사용하는 것을 권장합니다. 이 방식은 sql가 간단한 업데이트 문장의 결합으로 포지셔닝 문제에 편리합니다.casewhen 방식은 수정해야 할 매개 변수 뒤에 메인 키를 나열하고 마퍼 파일에 대량의 군더더기가 나타납니다.
3. 대량 업데이트 데이터량이 많은 경우casewhen의 효율이 낮기 때문에 전체 순환 업데이트를 사용하는 것을 권장합니다.
프로젝트 소스 주소:https://git.lug.ustc.edu.cn/zhaiyt/demo7.git
전재 대상:https://www.cnblogs.com/zhaiyt/p/9550438.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SQLite의 query로 망설임이것은 내가 처음 안드로이드 응용 프로그램 개발에서 망설이고, 그 후 해결 된 방법을 비망록으로 철자하고 있습니다. java에서 SQLite를 이용한 애플리케이션을 작성하는 동안 EditText에 입력된 item이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.