The problem

SearchIndex::updateWordWeights() contains this line:

 $or = $this->replica->condition('OR');
      foreach ($result as $word) {
        $or->condition('word', $word->realword);
      }

This query can exceed SQL limits for the length of SELECT clauses, and in some cases PHP memory limits.

Proposed resolution

Convert to an IN() query.

Potentially chunk the words into multiple smaller queries.

Or use a delete with a subquery instead of building the array in PHP.

Comments

ifish’s picture

Issue summary: View changes

.

brandy.brown’s picture

I don't know if this will solve your problem (or if you already did this), but you might need to up your php limit in settings.php and php.ini -- this solution can be found here: http://drupal.org/node/76156

brandy.brown’s picture

Priority: Major » Normal
brandy.brown’s picture

Status: Active » Postponed (maintainer needs more info)
dddave’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

klokie’s picture

Title: Fatal error: Allowed memory size of 260046848 bytes exhausted (tried to allocate 79 bytes) in /misc/40/000/122/664/4/user/web/te » Fatal error: Allowed memory size exhausted in modules/search/search.module
Version: 7.10 » 7.x-dev
Category: support » bug
Status: Closed (fixed) » Needs review
StatusFileSize
new1.15 KB

Hi, hope you don't mind me reopening this issue but I think that it should be possible to update the search index via cron in under 256 MB of RAM ;)
I've attached a patch that does that, by creating an "IN" rather than an "OR" clause.

Status: Needs review » Needs work

The last submitted patch, 1365786-replace-OR-with-IN-clause-for-search-index-update.patch, failed testing.

klokie’s picture

oops, resubmitting patch

klokie’s picture

Status: Needs work » Needs review
klokie’s picture

Issue summary: View changes

.

jhodgdon’s picture

Version: 7.x-dev » 8.x-dev
Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs backport to D7

The patch here (with a quick reroll) would seem to be relevant to D8 as well -- the code is exactly the same in 7 as it is in 8.

The problem is that the function search_update_totals(), which runs at the end of the cron run, is trying to find all the words that need to be removed from the search index, and it's doing it via a db_or() instead of an IN. Probably what it really needs to do is make sure not to do more than about 100 at a time, because you can easily run out of placeholders in such a query. Either that or do a db delete with a join, which is possible to do.

Here's the code that has the problme:

  // Find words that were deleted from search_index, but are still in
  // search_total. We use a LEFT JOIN between the two tables and keep only the
  // rows which fail to join.
  $result = db_query("SELECT t.word AS realword, i.word FROM {search_total} t LEFT JOIN {search_index} i ON t.word = i.word WHERE i.word IS NULL", array(), array('target' => 'slave'));
  $or = db_or();
  foreach ($result as $word) {
    $or->condition('word', $word->realword);
  }
  if (count($or) > 0) {
    db_delete('search_total')
      ->condition($or)
      ->execute();
  }
nick_vh’s picture

So you are saying to keep a count of the words to delete and execute that per cron run instead of updating everything in 1 execution? I see that you could also keep a count of the words and send multiple queries to not exaust the IN clause but I'm not sure if that would help Drupal from getting out of memory or timing out?

jhodgdon’s picture

I did something similar to this in the API module -- well, similar in character anyway, if not the details.

What I did was basically decide that I could only execute about 100 items at a time in a query, due to placeholder limitations. So I did something like this (this is not meant to be final code, but something like this):

$number_in_query = 0;

foreach ($result as $word) { // this is the loop that is building up the query
  $or->condition('word', $word->realword);
  $number_in_query++;
  if ($number_in_query >= 100) {
     // execute the query and then set $number_in_query back to 0
     
     // also clear out $or
     $or = db_or();
  }
}

if ($number_in_query > 0) {
   // execute the query with whatever is remaining.
}
jhodgdon’s picture

I just filed a meta-issue #2367253: [META] Several problems in search_update_totals() to clarify the similarities and differences between this issue and the others around search_update_totals(). I'm hoping we can fix them all together?

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

catch’s picture

Title: Fatal error: Allowed memory size exhausted in modules/search/search.module » The OR query in SearchIndex::updateWordWeights() can exceed SQL select limits / PHP memory limit, change to an IN()
Version: 8.9.x-dev » 9.2.x-dev
Issue summary: View changes
Issue tags: +Bug Smash Initiative
catch’s picture

Issue summary: View changes

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

quietone’s picture

Status: Needs work » Postponed

The Search Module was approved for removal in #3476883: [Policy, no patch] Move Search module to contrib .

This is Postponed. The status is set according to two policies. The Remove a core extension and move it to a contributed project and the Extensions approved for removal policies.

The deprecation work is in #3565780: [meta] Tasks to deprecate the Search module and the removal work in #3565783: [meta] Tasks to remove the Search module.

Search will be moved to a contributed project before Drupal 12.0.0 is released.