[ticket/13713] Cache SQL queries

PHPBB3-13713
This commit is contained in:
lavigor 2018-07-11 02:56:31 +03:00 committed by Marc Alexander
parent b5ce3343ed
commit 0aadd52014
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
9 changed files with 58 additions and 32 deletions

View file

@ -44,10 +44,10 @@ class mention
public function handle() public function handle()
{ {
// if (!$this->request->is_ajax()) if (!$this->request->is_ajax())
// { {
// new RedirectResponse(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext)); new RedirectResponse(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext));
// } }
$keyword = $this->request->variable('keyword', '', true); $keyword = $this->request->variable('keyword', '', true);
$topic_id = $this->request->variable('topic_id', 0); $topic_id = $this->request->variable('topic_id', 0);

View file

@ -68,14 +68,15 @@ abstract class base_group implements source_interface
'FROM' => [ 'FROM' => [
GROUPS_TABLE => 'g', GROUPS_TABLE => 'g',
], ],
'LEFT_JOIN' => array( 'LEFT_JOIN' => [
array( [
'FROM' => array(USER_GROUP_TABLE => 'ug'), 'FROM' => [USER_GROUP_TABLE => 'ug'],
'ON' => 'ug.group_id = g.group_id AND ug.user_pending = 0 AND ug.user_id = ' . (int) $this->user->data['user_id'], 'ON' => 'ug.group_id = g.group_id AND ug.user_pending = 0 AND ug.user_id = ' . (int) $this->user->data['user_id'],
), ],
), ],
]); ]);
$result = $this->db->sql_query($query); // Cache results for 5 minutes
$result = $this->db->sql_query($query, 600);
$this->groups = []; $this->groups = [];
while ($row = $this->db->sql_fetchrow($result)) while ($row = $this->db->sql_fetchrow($result))
@ -111,8 +112,8 @@ abstract class base_group implements source_interface
*/ */
public function get(array &$names, $keyword, $topic_id) public function get(array &$names, $keyword, $topic_id)
{ {
// Grab all group IDs // Grab all group IDs, cache for 5 minutes
$result = $this->db->sql_query($this->query($keyword, $topic_id)); $result = $this->db->sql_query($this->query($keyword, $topic_id), 300);
$group_ids = []; $group_ids = [];
while ($row = $this->db->sql_fetchrow($result)) while ($row = $this->db->sql_fetchrow($result))
@ -142,7 +143,5 @@ abstract class base_group implements source_interface
'rank' => $group_rank['title'], 'rank' => $group_rank['title'],
]); ]);
} }
return $names;
} }
} }

View file

@ -76,10 +76,27 @@ abstract class base_user implements source_interface
public function get(array &$names, $keyword, $topic_id) public function get(array &$names, $keyword, $topic_id)
{ {
$keyword = utf8_clean_string($keyword); $keyword = utf8_clean_string($keyword);
$result = $this->db->sql_query_limit($this->query($keyword, $topic_id), self::NAMES_BATCH_SIZE);
while ($row = $this->db->sql_fetchrow($result)) // Do not query all possible users (just a moderate amount), cache results for 5 minutes
$result = $this->db->sql_query($this->query($keyword, $topic_id), 300);
$i = 0;
while ($i < self::NAMES_BATCH_SIZE)
{ {
$row = $this->db->sql_fetchrow($result);
if (!$row)
{
break;
}
if (!empty($keyword) && strpos($row['username_clean'], $keyword) !== 0)
{
continue;
}
$i++;
$user_rank = $this->user_loader->get_rank($row['user_id'], true); $user_rank = $this->user_loader->get_rank($row['user_id'], true);
array_push($names, [ array_push($names, [
'name' => $row['username'], 'name' => $row['username'],
@ -95,7 +112,5 @@ abstract class base_user implements source_interface
} }
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);
return $names;
} }
} }

View file

@ -33,8 +33,13 @@ class friend extends base_user
*/ */
protected function query($keyword, $topic_id) protected function query($keyword, $topic_id)
{ {
/*
* For optimization purposes all friends are returned regardless of the keyword
* Names filtering is done on the frontend
* Results will be cached on a per-user basis
*/
$query = $this->db->sql_build_query('SELECT', [ $query = $this->db->sql_build_query('SELECT', [
'SELECT' => 'u.username, u.user_id', 'SELECT' => 'u.username_clean, u.username, u.user_id',
'FROM' => [ 'FROM' => [
USERS_TABLE => 'u', USERS_TABLE => 'u',
], ],
@ -45,8 +50,7 @@ class friend extends base_user
] ]
], ],
'WHERE' => 'z.friend = 1 AND z.user_id = ' . (int) $this->user->data['user_id'] . ' 'WHERE' => 'z.friend = 1 AND z.user_id = ' . (int) $this->user->data['user_id'] . '
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . ' AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]),
AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char()),
'ORDER_BY' => 'u.user_lastvisit DESC' 'ORDER_BY' => 'u.user_lastvisit DESC'
]); ]);
return $query; return $query;

View file

@ -17,12 +17,11 @@ interface source_interface
{ {
/** /**
* Searches database for names to mention * Searches database for names to mention
* and returns and array of found items * and alters the passed array of found items
* *
* @param array $names Array of already fetched data with names * @param array $names Array of already fetched data with names
* @param string $keyword Search string * @param string $keyword Search string
* @param int $topic_id Current topic ID * @param int $topic_id Current topic ID
* @return array Array of names
*/ */
public function get(array &$names, $keyword, $topic_id); public function get(array &$names, $keyword, $topic_id);
} }

View file

@ -20,16 +20,20 @@ class team extends base_user
*/ */
protected function query($keyword, $topic_id) protected function query($keyword, $topic_id)
{ {
/*
* For optimization purposes all team members are returned regardless of the keyword
* Names filtering is done on the frontend
* Results will be cached in a single file
*/
$query = $this->db->sql_build_query('SELECT', [ $query = $this->db->sql_build_query('SELECT', [
'SELECT' => 'u.username, u.user_id', 'SELECT' => 'u.username_clean, u.username, u.user_id',
'FROM' => [ 'FROM' => [
USERS_TABLE => 'u', USERS_TABLE => 'u',
USER_GROUP_TABLE => 'ug', USER_GROUP_TABLE => 'ug',
TEAMPAGE_TABLE => 't', TEAMPAGE_TABLE => 't',
], ],
'WHERE' => 'ug.group_id = t.group_id AND ug.user_id = u.user_id AND ug.user_pending = 0 'WHERE' => 'ug.group_id = t.group_id AND ug.user_id = u.user_id AND ug.user_pending = 0
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . ' AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]),
AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char()),
'ORDER_BY' => 'u.user_lastvisit DESC' 'ORDER_BY' => 'u.user_lastvisit DESC'
]); ]);
return $query; return $query;

View file

@ -20,6 +20,11 @@ class topic extends base_user
*/ */
protected function query($keyword, $topic_id) protected function query($keyword, $topic_id)
{ {
/*
* For optimization purposes all users are returned regardless of the keyword
* Names filtering is done on the frontend
* Results will be cached on a per-topic basis
*/
$query = $this->db->sql_build_query('SELECT', [ $query = $this->db->sql_build_query('SELECT', [
'SELECT' => 'u.username, u.user_id', 'SELECT' => 'u.username, u.user_id',
'FROM' => [ 'FROM' => [
@ -32,8 +37,7 @@ class topic extends base_user
] ]
], ],
'WHERE' => 'p.topic_id = ' . $topic_id . ' 'WHERE' => 'p.topic_id = ' . $topic_id . '
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . ' AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]),
AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char()),
'ORDER_BY' => 'p.post_time DESC' 'ORDER_BY' => 'p.post_time DESC'
]); ]);
return $query; return $query;

View file

@ -20,13 +20,14 @@ class user extends base_user
*/ */
protected function query($keyword, $topic_id) protected function query($keyword, $topic_id)
{ {
// TODO: think about caching ALL users: 1m users results to ~40MB file
$query = $this->db->sql_build_query('SELECT', [ $query = $this->db->sql_build_query('SELECT', [
'SELECT' => 'u.username, u.user_id', 'SELECT' => 'u.username_clean, u.username, u.user_id',
'FROM' => [ 'FROM' => [
USERS_TABLE => 'u', USERS_TABLE => 'u',
], ],
'WHERE' => $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . ' 'WHERE' => $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER])/* . '
AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char()), AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char())*/,
'ORDER_BY' => 'u.user_lastvisit DESC' 'ORDER_BY' => 'u.user_lastvisit DESC'
]); ]);
return $query; return $query;

View file

@ -31,7 +31,7 @@ class usergroup extends base_group
'ON' => 'g.group_id = ug.group_id' 'ON' => 'g.group_id = ug.group_id'
] ]
], ],
'WHERE' => 'ug.user_id = ' . (int) $this->user->data['user_id'], 'WHERE' => 'ug.user_pending = 0 AND ug.user_id = ' . (int) $this->user->data['user_id'],
]); ]);
return $query; return $query;
} }