From 0dc0527939cd39a5c3b67a05677e42e014225fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dark=E2=9D=B6?= <25451052+Dark1z@users.noreply.github.com> Date: Sun, 12 Dec 2021 20:15:48 +0530 Subject: [PATCH 1/5] [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 --- phpBB/phpbb/search/backend/base.php | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index a6bcb08850..a68588e97a 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -331,7 +331,7 @@ abstract class base implements search_backend_interface 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()) { @@ -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']); } $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 @@ -385,7 +384,7 @@ abstract class base implements search_backend_interface $row_count = 0; 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(); foreach ($rows as $row) { @@ -400,7 +399,7 @@ abstract class base implements search_backend_interface $this->index_remove($ids, $posters, $forum_ids); } - $post_counter += self::BATCH_SIZE; + $post_counter = end($ids); } 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 $final_id + * @param int $post_id * @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 FROM ' . POSTS_TABLE . ' - WHERE post_id >= ' . $initial_id . ' - AND post_id <= ' . $final_id; - $result = $this->db->sql_query($sql); + WHERE post_id > ' . $post_id; + $result = $this->db->sql_query_limit($sql, self::BATCH_SIZE); while ($row = $this->db->sql_fetchrow($result)) { From 90433d0d89e8652c8c290fae1659cb4f0280a9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dark=E2=9D=B6?= <25451052+Dark1z@users.noreply.github.com> Date: Sun, 12 Dec 2021 20:46:07 +0530 Subject: [PATCH 2/5] [ticket/16940] Optimize phpBB Native Search - Removed `end()` replaced with `$ids[count($ids) - 1]` PHPBB3-16940 --- phpBB/phpbb/search/backend/base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index a68588e97a..58e7be83f4 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -399,7 +399,7 @@ abstract class base implements search_backend_interface $this->index_remove($ids, $posters, $forum_ids); } - $post_counter = end($ids); + $post_counter = $ids[count($ids) - 1]; } if ($post_counter <= $max_post_id) From ff6f7c4186269f0c07294260a59222b7b2f18f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dark=E2=9D=B6?= <25451052+Dark1z@users.noreply.github.com> Date: Mon, 13 Dec 2021 00:38:35 +0530 Subject: [PATCH 3/5] [ticket/16940] Optimize phpBB Native Search - Fixed infinite loop PHPBB3-16940 --- phpBB/phpbb/search/backend/base.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index 58e7be83f4..ff57189a85 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -357,7 +357,7 @@ abstract class base implements search_backend_interface $this->tidy(); $this->config['num_posts'] = $num_posts; - if ($post_counter <= $max_post_id) + if ($post_counter < $max_post_id) { $totaltime = microtime(true) - $starttime; $rows_per_second = $row_count / $totaltime; @@ -402,7 +402,7 @@ abstract class base implements search_backend_interface $post_counter = $ids[count($ids) - 1]; } - if ($post_counter <= $max_post_id) + if ($post_counter < $max_post_id) { $totaltime = microtime(true) - $starttime; $rows_per_second = $row_count / $totaltime; From 6389a576331ad907a3e2c1354963186d59d3d007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dark=E2=9D=B6?= <25451052+Dark1z@users.noreply.github.com> Date: Mon, 13 Dec 2021 19:39:01 +0530 Subject: [PATCH 4/5] [ticket/16940] Optimize phpBB Native Search - Use `ORDER BY post_id ASC` for batch posts. PHPBB3-16940 --- phpBB/phpbb/search/backend/base.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index ff57189a85..cb2a6e7f39 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -452,8 +452,9 @@ abstract class base implements search_backend_interface protected function get_posts_batch_after(int $post_id): \Generator { $sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id - FROM ' . POSTS_TABLE . ' - WHERE post_id > ' . $post_id; + FROM ' . POSTS_TABLE . ' + WHERE post_id > ' . (int) $post_id . ' + ORDER BY post_id ASC'; $result = $this->db->sql_query_limit($sql, self::BATCH_SIZE); while ($row = $this->db->sql_fetchrow($result)) From a4302d9fa526110982dfd545c01a9e207fbf1d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dark=E2=9D=B6?= <25451052+Dark1z@users.noreply.github.com> Date: Tue, 14 Dec 2021 16:28:50 +0530 Subject: [PATCH 5/5] [ticket/16940] Optimize phpBB Native Search PHPBB3-16940 --- phpBB/phpbb/search/backend/base.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index cb2a6e7f39..4003e102ef 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -397,9 +397,8 @@ abstract class base implements search_backend_interface if (count($ids)) { $this->index_remove($ids, $posters, $forum_ids); + $post_counter = $ids[count($ids) - 1]; } - - $post_counter = $ids[count($ids) - 1]; } if ($post_counter < $max_post_id)