<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="innodb-index-types">
      </a>
      17.6.2.1 Clustered and Secondary Indexes
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045165869152">
  </a>
  <a class="indexterm" name="idm46045165867696">
  </a>
  <a class="indexterm" name="idm46045165866208">
  </a>
  <a class="indexterm" name="idm46045165864720">
  </a>
  <p>
   Each
   <code class="literal">
    InnoDB
   </code>
   table has a special index called
      the clustered index that stores row data. Typically, the clustered
      index is synonymous with the primary key. To get the best
      performance from queries, inserts, and other database operations,
      it is important to understand how
   <code class="literal">
    InnoDB
   </code>
   uses
      the clustered index to optimize the common lookup and DML
      operations.
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      When you define a
      <code class="literal">
       PRIMARY KEY
      </code>
      on a table,
      <code class="literal">
       InnoDB
      </code>
      uses it as the clustered index. A
          primary key should be defined for each table. If there is no
          logical unique and non-null column or set of columns to use a
          the primary key, add an auto-increment column. Auto-increment
          column values are unique and are added automatically as new
          rows are inserted.
     </p>
    </li>
    <li class="listitem">
     <p>
      If you do not define a
      <code class="literal">
       PRIMARY KEY
      </code>
      for a
          table,
      <code class="literal">
       InnoDB
      </code>
      uses the first
      <code class="literal">
       UNIQUE
      </code>
      index with all key columns defined
          as
      <code class="literal">
       NOT NULL
      </code>
      as the clustered index.
     </p>
    </li>
    <li class="listitem">
     <p>
      If a table has no
      <code class="literal">
       PRIMARY KEY
      </code>
      or suitable
      <code class="literal">
       UNIQUE
      </code>
      index,
      <code class="literal">
       InnoDB
      </code>
      generates a hidden clustered index named
      <code class="literal">
       GEN_CLUST_INDEX
      </code>
      on a synthetic column that
          contains row ID values. The rows are ordered by the row ID
          that
      <code class="literal">
       InnoDB
      </code>
      assigns. The row ID is a 6-byte
          field that increases monotonically as new rows are inserted.
          Thus, the rows ordered by the row ID are physically in order
          of insertion.
     </p>
    </li>
   </ul>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="innodb-index-types-query-performance">
       </a>
       How the Clustered Index Speeds Up Queries
      </h5>
     </div>
    </div>
   </div>
   <p>
    Accessing a row through the clustered index is fast because the
        index search leads directly to the page that contains the row
        data. If a table is large, the clustered index architecture
        often saves a disk I/O operation when compared to storage
        organizations that store row data using a different page from
        the index record.
   </p>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="innodb-index-types-secondary-clustered">
       </a>
       How Secondary Indexes Relate to the Clustered Index
      </h5>
     </div>
    </div>
   </div>
   <p>
    Indexes other than the clustered index are known as secondary
        indexes. In
    <code class="literal">
     InnoDB
    </code>
    , each record in a
        secondary index contains the primary key columns for the row, as
        well as the columns specified for the secondary index.
    <code class="literal">
     InnoDB
    </code>
    uses this primary key value to search
        for the row in the clustered index.
   </p>
   <p>
    If the primary key is long, the secondary indexes use more
        space, so it is advantageous to have a short primary key.
   </p>
   <p>
    For guidelines to take advantage of
    <code class="literal">
     InnoDB
    </code>
    clustered and secondary indexes, see
    <a class="xref" href="optimization-indexes.html" title="10.3 Optimization and Indexes">
     Section 10.3, “Optimization and Indexes”
    </a>
    .
   </p>
  </div>
 </div>
 <br/>
</div>
