Build new indexes efficiently

By default, the database server places one entry in shared memory per call to the am_insert purpose function for a CREATE INDEX statement. The purpose function inserts the single entry and then returns control to the database server, which executes am_insert again until no more entries remain to insert.

The following figure shows how the am_insert purpose function writes multiple new index entries.
Figure 1: Processing multiple index entries
mi_integer my_am_open(MI_AM_TABLE_DESC *td)
{
...
   mi_tab_setniorows(td, 512);
}

mi_integer my_am_insert(MI_AM_TABLE_DESC *td, MI_ROW *newrow,
                  MI_AM_ROWID_DESC *rid)
{
   mi_integernrows;
   mi_integerrowid; 
   mi_integerfragid;

   nrows = mi_tab_niorows(td);
   if (nrows > 0)
   {
      for (row = 0; row < nrows; ++row)
      {
          mi_tab_nextrow(td, &newrow, &rowid, &fragid) 
          /*Write new entry. (Not shown.)*/
      } /* End get new entries from shared memory */
   }
   else
   {/* Shared memory contains only one entry per call to am_insert.*/
      rowid = mi_id_rowid(rid);
      fragid = mi_id_fragid(rid);
       /*Write new entry. (Not shown.)*/
   }/* End write one index entry. */
   /* Return either MI_OK or MI_ERROR, as required. 
   ** (This example does not show error or exception-processing.) */
}
In Processing multiple index entries, the access method performs the following steps:
  1. The am_open purpose function calls mi_tab_setniorows() to specify the number of index entries that the database server can store in shared memory for am_insert.
  2. At the start of am_insert, the purpose function calls mi_tab_niorows() to find out how many rows to retrieve from shared memory.

    The number of rows that shared memory actually contains might not equal the number of rows that mi_tab_setniorows() set.

  3. The server loops through mi_tab_setnextrow() in am_insert to retrieve each new entry from shared memory.