mysql 대량 업데이트 두 가지 방식 효율 시험

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값을 나열합니다. 데이터에 문제가 생기면 포지셔닝이 어렵습니다.
효율성 통계:
  • 10가지 데이터를 대량으로 업데이트하는 두 가지 방법의 사용 시간 통계:
  • 시간은 무시해도 된다.
  • 100개의 데이터를 대량으로 업데이트하는 두 가지 방법의 사용 시간 통계:
  •  
  • 1000개의 데이터를 대량으로 갱신하는 두 가지 방법의 사용 시간 통계:
  •  
  • 5000개의 데이터를 대량으로 갱신하는 두 가지 방법의 사용 시간 통계:
  •  
  • 10000개의 데이터를 대량으로 갱신하는 두 가지 방법의 사용 시간 통계:
  •  
    요약:
    1. 한 번의 수정 데이터량이 비교적 적은 상황에서casewhen 방식과 전체 업데이트 방식은 모두 선택할 수 있지만 전체 업데이트는 시스템 파라미터 설정을 추가해야 한다.
    2. 데이터 문제가 자주 발생하는 경우 전체적인 순환 업데이트 방식을 사용하는 것을 권장합니다. 이 방식은 sql가 간단한 업데이트 문장의 결합으로 포지셔닝 문제에 편리합니다.casewhen 방식은 수정해야 할 매개 변수 뒤에 메인 키를 나열하고 마퍼 파일에 대량의 군더더기가 나타납니다.
    3. 대량 업데이트 데이터량이 많은 경우casewhen의 효율이 낮기 때문에 전체 순환 업데이트를 사용하는 것을 권장합니다.
     
     
    프로젝트 소스 주소:https://git.lug.ustc.edu.cn/zhaiyt/demo7.git
    전재 대상:https://www.cnblogs.com/zhaiyt/p/9550438.html

    좋은 웹페이지 즐겨찾기