mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-31 14:00:31 +02:00
Merge pull request #6321 from rxu/ticket/16902
[ticket/16902] Improve search results count - 3.3.x
This commit is contained in:
@@ -568,8 +568,8 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
);
|
||||
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_select .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : '';
|
||||
$sql_from = ($join_topic) ? TOPICS_TABLE . ' t, ' : '';
|
||||
$field = ($type == 'posts') ? 'post_id' : 'topic_id';
|
||||
if (count($author_ary) && $author_name)
|
||||
@@ -610,11 +610,10 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
$id_ary = array_unique($id_ary);
|
||||
|
||||
// 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(*) 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);
|
||||
@@ -828,12 +827,13 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
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';
|
||||
$sql_select .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : '';
|
||||
|
||||
// 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
|
||||
@@ -847,7 +847,7 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
}
|
||||
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
|
||||
@@ -857,7 +857,7 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
AND t.topic_id = p.topic_id
|
||||
$sql_sort_join
|
||||
$sql_time
|
||||
GROUP BY t.topic_id
|
||||
GROUP BY $sql_select
|
||||
ORDER BY $sql_sort";
|
||||
$field = 'topic_id';
|
||||
}
|
||||
@@ -874,9 +874,10 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
// 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(*) as result_count", $sql);
|
||||
$result = $this->db->sql_query($sql_found_rows);
|
||||
$result_count = (int) $this->db->sql_fetchfield('result_count');
|
||||
$result_count = ($type == 'posts') ? (int) $this->db->sql_fetchfield('result_count') : count($this->db->sql_fetchrowset($result));
|
||||
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
if (!$result_count)
|
||||
|
@@ -908,9 +908,6 @@ class fulltext_native extends \phpbb\search\base
|
||||
switch ($this->db->get_sql_layer())
|
||||
{
|
||||
case 'mysqli':
|
||||
|
||||
// 3.x does not support SQL_CALC_FOUND_ROWS
|
||||
// $sql_array['SELECT'] = 'SQL_CALC_FOUND_ROWS ' . $sql_array['SELECT'];
|
||||
$is_mysql = true;
|
||||
|
||||
break;
|
||||
@@ -968,16 +965,10 @@ class fulltext_native extends \phpbb\search\base
|
||||
);
|
||||
}
|
||||
|
||||
// if using mysql and the total result count is not calculated yet, get it from the db
|
||||
if (!$total_results && $is_mysql)
|
||||
{
|
||||
// Also count rows for the query as if there was not LIMIT. Add SQL_CALC_FOUND_ROWS to SQL
|
||||
$sql_array['SELECT'] = 'SQL_CALC_FOUND_ROWS ' . $sql_array['SELECT'];
|
||||
}
|
||||
|
||||
$sql_array['WHERE'] = implode(' AND ', $sql_where);
|
||||
$sql_array['GROUP_BY'] = ($group_by) ? (($type == 'posts') ? 'p.post_id' : 'p.topic_id') . ', ' . $sort_by_sql[$sort_key] : '';
|
||||
$sql_array['ORDER_BY'] = $sql_sort;
|
||||
$sql_array['SELECT'] .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : '';
|
||||
|
||||
unset($sql_where, $sql_sort, $group_by);
|
||||
|
||||
@@ -990,10 +981,10 @@ class fulltext_native extends \phpbb\search\base
|
||||
}
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
// If using mysql and the total result count is not calculated yet, get it from the db
|
||||
if (!$total_results && $is_mysql)
|
||||
{
|
||||
// Get the number of results as calculated by MySQL
|
||||
$sql_count = 'SELECT FOUND_ROWS() as total_results';
|
||||
$sql_count = str_replace("SELECT {$sql_array['SELECT']}", "SELECT COUNT(DISTINCT {$sql_array['SELECT']}) as total_results", $sql);
|
||||
$result = $this->db->sql_query($sql_count);
|
||||
$total_results = (int) $this->db->sql_fetchfield('total_results');
|
||||
$this->db->sql_freeresult($result);
|
||||
@@ -1015,7 +1006,6 @@ class fulltext_native extends \phpbb\search\base
|
||||
$id_ary[] = (int) $row[(($type == 'posts') ? 'post_id' : 'topic_id')];
|
||||
}
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
}
|
||||
|
||||
// store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page
|
||||
@@ -1147,6 +1137,7 @@ class fulltext_native extends \phpbb\search\base
|
||||
}
|
||||
|
||||
$select = ($type == 'posts') ? 'p.post_id' : 't.topic_id';
|
||||
$select .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : '';
|
||||
$is_mysql = false;
|
||||
|
||||
/**
|
||||
@@ -1202,7 +1193,6 @@ class fulltext_native extends \phpbb\search\base
|
||||
switch ($this->db->get_sql_layer())
|
||||
{
|
||||
case 'mysqli':
|
||||
// $select = 'SQL_CALC_FOUND_ROWS ' . $select;
|
||||
$is_mysql = true;
|
||||
break;
|
||||
|
||||
@@ -1295,15 +1285,9 @@ class fulltext_native extends \phpbb\search\base
|
||||
|
||||
if (!$total_results && $is_mysql)
|
||||
{
|
||||
// Count rows for the executed queries. Replace $select within $sql with SQL_CALC_FOUND_ROWS, and run it.
|
||||
$sql_calc = str_replace('SELECT ' . $select, 'SELECT SQL_CALC_FOUND_ROWS ' . $select, $sql);
|
||||
|
||||
$result = $this->db->sql_query($sql_calc);
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
$sql_count = 'SELECT FOUND_ROWS() as total_results';
|
||||
$sql_count = str_replace("SELECT $select", "SELECT COUNT(*) as total_results", $sql);
|
||||
$result = $this->db->sql_query($sql_count);
|
||||
$total_results = (int) $this->db->sql_fetchfield('total_results');
|
||||
$total_results = ($type == 'posts') ? (int) $this->db->sql_fetchfield('total_results') : count($this->db->sql_fetchrowset($result));
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
if (!$total_results)
|
||||
|
@@ -836,8 +836,9 @@ class fulltext_postgres extends \phpbb\search\base
|
||||
GROUP BY t.topic_id, $sort_by_sql[$sort_key]";
|
||||
}
|
||||
|
||||
$this->db->sql_query($sql_count);
|
||||
$result_count = (int) $this->db->sql_fetchfield('result_count');
|
||||
$result = $this->db->sql_query($sql_count);
|
||||
$result_count = ($type == 'posts') ? (int) $this->db->sql_fetchfield('result_count') : count($this->db->sql_fetchrowset($result));
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
if (!$result_count)
|
||||
{
|
||||
|
Reference in New Issue
Block a user