[ticket/16940] Optimize phpBB Native Search

- Use `sql_query_limit` instead of `sql_query`
- Update SQL query to reflect the above change
- Assign proper last `post_id` to `$post_counter`

PHPBB3-16940
This commit is contained in:
Dark❶ 2021-12-12 20:15:48 +05:30
parent 2457eea876
commit 0dc0527939
No known key found for this signature in database
GPG key ID: B5C35684F456E634

View file

@ -331,7 +331,7 @@ abstract class base implements search_backend_interface
while (still_on_time() && $post_counter <= $max_post_id) while (still_on_time() && $post_counter <= $max_post_id)
{ {
$rows = $this->get_posts_between($post_counter + 1, $post_counter + self::BATCH_SIZE); $rows = $this->get_posts_batch_after($post_counter);
if ($this->db->sql_buffer_nested_transactions()) if ($this->db->sql_buffer_nested_transactions())
{ {
@ -346,9 +346,8 @@ abstract class base implements search_backend_interface
$this->index('post', (int) $row['post_id'], $row['post_text'], $row['post_subject'], (int) $row['poster_id'], (int) $row['forum_id']); $this->index('post', (int) $row['post_id'], $row['post_text'], $row['post_subject'], (int) $row['poster_id'], (int) $row['forum_id']);
} }
$row_count++; $row_count++;
$post_counter = $row['post_id'];
} }
$post_counter += self::BATCH_SIZE;
} }
// pretend the number of posts was as big as the number of ids we indexed so far // pretend the number of posts was as big as the number of ids we indexed so far
@ -385,7 +384,7 @@ abstract class base implements search_backend_interface
$row_count = 0; $row_count = 0;
while (still_on_time() && $post_counter <= $max_post_id) while (still_on_time() && $post_counter <= $max_post_id)
{ {
$rows = $this->get_posts_between($post_counter + 1, $post_counter + self::BATCH_SIZE); $rows = $this->get_posts_batch_after($post_counter);
$ids = $posters = $forum_ids = array(); $ids = $posters = $forum_ids = array();
foreach ($rows as $row) foreach ($rows as $row)
{ {
@ -400,7 +399,7 @@ abstract class base implements search_backend_interface
$this->index_remove($ids, $posters, $forum_ids); $this->index_remove($ids, $posters, $forum_ids);
} }
$post_counter += self::BATCH_SIZE; $post_counter = end($ids);
} }
if ($post_counter <= $max_post_id) if ($post_counter <= $max_post_id)
@ -445,19 +444,17 @@ abstract class base implements search_backend_interface
} }
/** /**
* Get posts between 2 ids * Get batch of posts after id
* *
* @param int $initial_id * @param int $post_id
* @param int $final_id
* @return \Generator * @return \Generator
*/ */
protected function get_posts_between(int $initial_id, int $final_id): \Generator protected function get_posts_batch_after(int $post_id): \Generator
{ {
$sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id $sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
FROM ' . POSTS_TABLE . ' FROM ' . POSTS_TABLE . '
WHERE post_id >= ' . $initial_id . ' WHERE post_id > ' . $post_id;
AND post_id <= ' . $final_id; $result = $this->db->sql_query_limit($sql, self::BATCH_SIZE);
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result)) while ($row = $this->db->sql_fetchrow($result))
{ {