<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="insert-optimization">
      </a>
      10.2.5.1 Optimizing INSERT Statements
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045227662080">
  </a>
  <a class="indexterm" name="idm46045227661040">
  </a>
  <a class="indexterm" name="idm46045227659552">
  </a>
  <a class="indexterm" name="idm46045227658064">
  </a>
  <p>
   To optimize insert speed, combine many small operations into a
          single large operation. Ideally, you make a single connection,
          send the data for many new rows at once, and delay all index
          updates and consistency checking until the very end.
  </p>
  <p>
   The time required for inserting a row is determined by the
          following factors, where the numbers indicate approximate
          proportions:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Connecting: (3)
     </p>
    </li>
    <li class="listitem">
     <p>
      Sending query to server: (2)
     </p>
    </li>
    <li class="listitem">
     <p>
      Parsing query: (2)
     </p>
    </li>
    <li class="listitem">
     <p>
      Inserting row: (1 × size of row)
     </p>
    </li>
    <li class="listitem">
     <p>
      Inserting indexes: (1 × number of indexes)
     </p>
    </li>
    <li class="listitem">
     <p>
      Closing: (1)
     </p>
    </li>
   </ul>
  </div>
  <p>
   This does not take into consideration the initial overhead to
          open tables, which is done once for each concurrently running
          query.
  </p>
  <p>
   The size of the table slows down the insertion of indexes by
          log
   <em class="replaceable">
    <code>
     N
    </code>
   </em>
   , assuming B-tree indexes.
  </p>
  <p>
   You can use the following methods to speed up inserts:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      If you are inserting many rows from the same client at the
              same time, use
      <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
       <code class="literal">
        INSERT
       </code>
      </a>
      statements with multiple
      <code class="literal">
       VALUES
      </code>
      lists
              to insert several rows at a time. This is considerably
              faster (many times faster in some cases) than using
              separate single-row
      <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
       <code class="literal">
        INSERT
       </code>
      </a>
      statements. If you are adding data to a nonempty table,
              you can tune the
      <a class="link" href="server-system-variables.html#sysvar_bulk_insert_buffer_size">
       <code class="literal">
        bulk_insert_buffer_size
       </code>
      </a>
      variable to make data insertion even faster. See
      <a class="xref" href="server-system-variables.html" title="7.1.8 Server System Variables">
       Section 7.1.8, “Server System Variables”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      When loading a table from a text file, use
      <a class="link" href="load-data.html" title="15.2.9 LOAD DATA Statement">
       <code class="literal">
        LOAD DATA
       </code>
      </a>
      . This is usually
              20 times faster than using
      <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
       <code class="literal">
        INSERT
       </code>
      </a>
      statements. See
      <a class="xref" href="load-data.html" title="15.2.9 LOAD DATA Statement">
       Section 15.2.9, “LOAD DATA Statement”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      Take advantage of the fact that columns have default
              values. Insert values explicitly only when the value to be
              inserted differs from the default. This reduces the
              parsing that MySQL must do and improves the insert speed.
     </p>
    </li>
    <li class="listitem">
     <p>
      See
      <a class="xref" href="optimizing-innodb-bulk-data-loading.html" title="10.5.5 Bulk Data Loading for InnoDB Tables">
       Section 10.5.5, “Bulk Data Loading for InnoDB Tables”
      </a>
      for tips specific to
      <code class="literal">
       InnoDB
      </code>
      tables.
     </p>
    </li>
    <li class="listitem">
     <p>
      See
      <a class="xref" href="optimizing-myisam-bulk-data-loading.html" title="10.6.2 Bulk Data Loading for MyISAM Tables">
       Section 10.6.2, “Bulk Data Loading for MyISAM Tables”
      </a>
      for tips specific to
      <code class="literal">
       MyISAM
      </code>
      tables.
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
