commit 0a578e13fa97902d9f1410fc56de95812084277e
Author: Daniel Black <daniel.black@openquery.com.au>
Date:   Mon Mar 31 08:27:54 2014 +1100

    Issue #2229013 by danblack: Add index on created and truncate table using delete from cache created < REQUEST_TIME

diff --git a/includes/cache.inc b/includes/cache.inc
index 09f4d75..412d764 100644
--- a/includes/cache.inc
+++ b/includes/cache.inc
@@ -510,7 +510,11 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
           // so we don't need to verify them first. This ensures that non-cache
           // tables cannot be truncated accidentally.
           if ($this->isValidBin()) {
-            db_truncate($this->bin)->execute();
+            // while a db_truncate here will work it does create timing conflicts
+            // as per issue #2229013.
+            db_delete($this->bin)
+              ->condition('created', REQUEST_TIME, '<')
+              ->execute();
           }
           else {
             throw new Exception(t('Invalid or missing cache bin specified: %bin', array('%bin' => $this->bin)));
diff --git a/includes/update.inc b/includes/update.inc
index a17161c..0f21dd9 100644
--- a/includes/update.inc
+++ b/includes/update.inc
@@ -295,6 +295,7 @@ function update_prepare_d7_bootstrap() {
       ),
       'indexes' => array(
         'expire' => array('expire'),
+        'created' => array('created'),
       ),
       'primary key' => array('cid'),
     );
@@ -515,6 +516,7 @@ function update_fix_d7_requirements() {
     // Add 7.x indexes.
     db_add_index('system', 'system_list', array('weight', 'name'));
 
+    db_add_index('cache_bootstrap', 'created', array('created'));
     // Add the cache_path table.
     if (db_table_exists('cache_path')) {
       db_drop_table('cache_path');
diff --git a/modules/system/system.install b/modules/system/system.install
index 43c7383..7a2f7a3 100644
--- a/modules/system/system.install
+++ b/modules/system/system.install
@@ -713,6 +713,7 @@ function system_schema() {
     ),
     'indexes' => array(
       'expire' => array('expire'),
+      'created' => array('created'),
     ),
     'primary key' => array('cid'),
   );
@@ -3151,6 +3152,17 @@ function system_update_7079() {
 }
 
 /**
+ * Add index on {cache}.created.
+ */
+function system_update_7080() {
+  db_add_index('cache', 'created', array('created'));
+  $caches = array('block', 'bootstrap', 'field', 'filter', 'form', 'menu', 'page', 'path');
+  foreach($caches as $tbl) {
+    db_add_index('cache_' . $tbl, 'created', array('created'));
+  }
+}
+
+/**
  * @} End of "defgroup updates-7.x-extra".
  * The next series of updates should start at 8000.
  */
