diff options
| author | 2025-11-15 20:11:25 +0100 | |
|---|---|---|
| committer | 2025-11-15 20:11:25 +0100 | |
| commit | 45471871ddc4bae6c1c36f2daa9091b85e458e8c (patch) | |
| tree | a67c1c0b9073ed457764cb1b1b831d099acdb42f /app/Models/FeedDAOPGSQL.php | |
| parent | ea8938b0d14ea1373ab3757494d0c0c1ef0e843e (diff) | |
SQL: Optimise speed of updateCachedValues() (#8207)
For PostgreSQL and SQLite
fix https://github.com/FreshRSS/FreshRSS/issues/8206
Diffstat (limited to 'app/Models/FeedDAOPGSQL.php')
| -rw-r--r-- | app/Models/FeedDAOPGSQL.php | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/app/Models/FeedDAOPGSQL.php b/app/Models/FeedDAOPGSQL.php index f436a2ec4..954a6e0ff 100644 --- a/app/Models/FeedDAOPGSQL.php +++ b/app/Models/FeedDAOPGSQL.php @@ -10,4 +10,40 @@ SELECT setval('`_feed_id_seq`', COALESCE(MAX(id), 0) + 1, false) FROM `_feed` SQL; return $this->pdo->exec($sql) !== false; } + + #[\Override] + public function updateCachedValues(int ...$feedIds): int|false { + // Faster than the MySQL version + if (empty($feedIds)) { + $whereFeedIds = 'true'; + $whereEntryIdFeeds = 'true'; + } else { + $whereFeedIds = 'id IN (' . str_repeat('?,', count($feedIds) - 1) . '?)'; + $whereEntryIdFeeds = 'id_feed IN (' . str_repeat('?,', count($feedIds) - 1) . '?)'; + } + $sql = <<<SQL + WITH entry_counts AS ( + SELECT + id_feed, + COUNT(*) AS total_entries, + SUM(CASE WHEN is_read = 0 THEN 1 ELSE 0 END) AS unread_entries + FROM `_entry` + WHERE $whereEntryIdFeeds + GROUP BY id_feed + ) + UPDATE `_feed` + SET `cache_nbEntries` = COALESCE(c.total_entries, 0), + `cache_nbUnreads` = COALESCE(c.unread_entries, 0) + FROM entry_counts c + WHERE id = c.id_feed AND $whereFeedIds + SQL; + $stm = $this->pdo->prepare($sql); + if ($stm !== false && $stm->execute(array_merge($feedIds, $feedIds))) { + return $stm->rowCount(); + } else { + $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo(); + Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info)); + return false; + } + } } |
