content_update_1009

Versions
5
content_update_1009()

Index the 'nid' column on data tables to optimize node deletion. Large tables might deserve a multipass update.

Code

contributions/cck/content.install, line 566

<?php
function content_update_1009() {
  $ret = array();

  // Gather list of tables.
  if (!isset($_SESSION['content_update_1009_tables'])) {
    $_SESSION['content_update_1009']['tables'] = array();
    // Get per-field tables.
    $result = db_query('SELECT * FROM {node_field_instance} nfi '.
      ' LEFT JOIN {node_field} nf ON nf.field_name = nfi.field_name WHERE db_storage = %d', CONTENT_DB_STORAGE_PER_FIELD);
    while ($field = db_fetch_array($result)) {
      $table = _content_tablename($field['field_name'], CONTENT_DB_STORAGE_PER_FIELD);
      $_SESSION['content_update_1009_tables'][$table] = $table;
    }
    // Additionally, get all per-type tables.
    foreach (node_get_types() as $type) {
      $table = _content_tablename($type->type, CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
      $_SESSION['content_update_1009_tables'][$table] = $table;
    }
    $_SESSION['content_update_1009_count'] = count($_SESSION['content_update_1009_tables']);
  }

  // One pass : add index on one table.
  $table = array_shift($_SESSION['content_update_1009_tables']);
  if (db_table_exists($table)) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $ret[] = update_sql("ALTER TABLE {". $table ."} ADD INDEX (nid)");
        break;
      case 'pgsql':
        $ret[] = update_sql("CREATE INDEX {". $table ."}_nid_idx ON {". $table ."}(nid)");
        break;
    }
  }
  $ret['#finished'] = 1 - count($_SESSION['content_update_1009_tables']) / $_SESSION['content_update_1009_count'];
  return $ret;
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.