1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 14:00:31 +02:00

[ticket/16902] Improve search results count for MySQL

PHPBB3-16902
This commit is contained in:
rxu
2021-10-29 21:32:26 +07:00
parent b5b8d26536
commit f78f3135fc
2 changed files with 9 additions and 27 deletions

View File

@@ -508,8 +508,7 @@ class fulltext_mysql extends base implements search_backend_interface
);
extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_keywords_main_query_before', compact($vars)));
$sql_select = (!$result_count) ? 'SQL_CALC_FOUND_ROWS ' : '';
$sql_select = ($type == 'posts') ? $sql_select . 'p.post_id' : 'DISTINCT ' . $sql_select . 't.topic_id';
$sql_select = ($type == 'posts') ? 'p.post_id' : 'DISTINCT t.topic_id';
$sql_from = ($join_topic) ? TOPICS_TABLE . ' t, ' : '';
$field = ($type == 'posts') ? 'post_id' : 'topic_id';
if (count($author_ary) && $author_name)
@@ -554,7 +553,7 @@ class fulltext_mysql extends base implements search_backend_interface
// if the total result count is not cached yet, retrieve it from the db
if (!$result_count && count($id_ary))
{
$sql_found_rows = 'SELECT FOUND_ROWS() as result_count';
$sql_found_rows = str_replace("SELECT $sql_select", "SELECT COUNT($sql_select) as result_count", $sql);
$result = $this->db->sql_query($sql_found_rows);
$result_count = (int) $this->db->sql_fetchfield('result_count');
$this->db->sql_freeresult($result);
@@ -752,12 +751,12 @@ class fulltext_mysql extends base implements search_backend_interface
extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_author_query_before', compact($vars)));
// If the cache was completely empty count the results
$calc_results = ($result_count) ? '' : 'SQL_CALC_FOUND_ROWS ';
$sql_select = ($type == 'posts') ? 'p.post_id' : 't.topic_id';
// Build the query for really selecting the post_ids
if ($type == 'posts')
{
$sql = "SELECT {$calc_results}p.post_id
$sql = "SELECT $sql_select
FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t ' : ' ') . "
WHERE $sql_author
$sql_topic_id
@@ -771,7 +770,7 @@ class fulltext_mysql extends base implements search_backend_interface
}
else
{
$sql = "SELECT {$calc_results}t.topic_id
$sql = "SELECT $sql_select
FROM " . $sql_sort_table . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
WHERE $sql_author
$sql_topic_id
@@ -798,7 +797,7 @@ class fulltext_mysql extends base implements search_backend_interface
// retrieve the total result count if needed
if (!$result_count)
{
$sql_found_rows = 'SELECT FOUND_ROWS() as result_count';
$sql_found_rows = str_replace("SELECT $sql_select", "SELECT COUNT($sql_select) as result_count", $sql);
$result = $this->db->sql_query($sql_found_rows);
$result_count = (int) $this->db->sql_fetchfield('result_count');
$this->db->sql_freeresult($result);