MDL-74586 rss: Make rss_get_userid_from_token() use table index

Due to missing `AND k.script = 'rss'` condition, the query did not make
use of the existing script-value compound index defined for the table.
So it had to perform the full sequential scan for all rows when
searching for the token. This had serious performance issues on sites
with many users, especially in case on non-existing token / key.
This commit is contained in:
David Mudrák
2023-02-28 13:26:12 +01:00
parent 6458658400
commit 06d446aec1
2 changed files with 25 additions and 5 deletions
+9 -4
View File
@@ -413,10 +413,15 @@ function rss_geterrorxmlfile($errortype = 'rsserror') {
function rss_get_userid_from_token($token) {
global $DB;
$sql = 'SELECT u.id FROM {user} u
JOIN {user_private_key} k ON u.id = k.userid
WHERE u.deleted = 0 AND u.confirmed = 1
AND u.suspended = 0 AND k.value = ?';
$sql = "SELECT u.id
FROM {user} u
JOIN {user_private_key} k ON u.id = k.userid
WHERE u.deleted = 0
AND u.confirmed = 1
AND u.suspended = 0
AND k.script = 'rss'
AND k.value = ?";
return $DB->get_field_sql($sql, array($token), IGNORE_MISSING);
}
+16 -1
View File
@@ -20,7 +20,7 @@ defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
require_once($CFG->libdir . '/rsslib.php');
/**
* These tests rely on the rsstest.xml file on download.moodle.org,
@@ -140,4 +140,19 @@ EOD;
$this->assertSame('Moodle News', $feed->get_title());
$this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link());
}
/**
* Test that we can get the right user ID based on the provided private key (token).
*
* @covers ::rss_get_userid_from_token
*/
public function test_rss_get_userid_from_token() {
global $USER;
$this->resetAfterTest();
$this->setGuestUser();
$key = rss_get_token($USER->id);
$this->assertSame(rss_get_userid_from_token($key), $USER->id);
}
}