Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/support/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ public function getResourceUpdates($resource_id, $page, $order = null, $directio
$page = $page == 1 ? 0 : 10 * ($page - 1);

if (!is_null($this->conn)) {
$updatesStmt = $this->_resource_update('AND r.resource_id = :resource_id', 10, $page, $order, $direction);
// Filter by rv instead of r: PDO sends the id as a string, and then MySQL does not apply a filter on r to rv.
// It would read all versions of the resource again for each of its updates.
$updatesStmt = $this->_resource_update('AND rv.resource_id = :resource_id', 10, $page, $order, $direction);
$updatesStmt->bindParam(':resource_id', $resource_id);

if ($updatesStmt->execute()) {
Expand Down Expand Up @@ -287,10 +289,13 @@ private function _resource_update($additional_where_clauses, $limit = 1, $offset
$orderClause = $this->prepare_order_options($orderOptions, $order, $direction);
$offsetClause = is_null($offset) ? '' : 'OFFSET :offset';

// There is no index on rv.resource_update_id, but one that starts with rv.resource_id.
// Without the condition on resource_id, every request reads the whole xf_resource_version table.
$query = sprintf(
"SELECT r.resource_update_id, r.resource_id, rv.resource_version_id, rv.version_string, rv.download_count, r.post_date, r.title, r.message
FROM xf_resource_update r
INNER JOIN xf_resource_version rv ON r.resource_update_id = rv.resource_update_id
INNER JOIN xf_resource_version rv
ON r.resource_id = rv.resource_id AND r.resource_update_id = rv.resource_update_id
WHERE r.message_state = 'visible' AND rv.version_state = 'visible'
%s
%s
Expand Down