From 485486c7ff986712ecb09e92217236d276d317c4 Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Mon, 12 Jan 2026 08:46:55 +0000
Subject: feat: deduplicate feed items by guid

Use ROW_NUMBER() window function to filter out duplicate articles
with the same guid across different feeds. This ensures each guid
appears only once in unread/read article lists.

- Add idx_articles_guid index for PARTITION BY performance
- Modify GetUnreadArticles and GetReadArticles queries with CTE
---
 backend/db/queries/articles.sql | 40 ++++++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 14 deletions(-)

(limited to 'backend/db/queries/articles.sql')

diff --git a/backend/db/queries/articles.sql b/backend/db/queries/articles.sql
index 5acdada..bd4cd91 100644
--- a/backend/db/queries/articles.sql
+++ b/backend/db/queries/articles.sql
@@ -7,23 +7,35 @@ INNER JOIN feeds AS f ON a.feed_id = f.id
 WHERE a.id = ?;
 
 -- name: GetUnreadArticles :many
-SELECT
-    a.id, a.feed_id, a.guid, a.title, a.url, a.is_read,
-    f.id as feed_id_2, f.url as feed_url, f.title as feed_title, f.is_subscribed as feed_is_subscribed
-FROM articles AS a
-INNER JOIN feeds AS f ON a.feed_id = f.id
-WHERE a.is_read = 0 AND f.is_subscribed = 1 AND f.user_id = ?
-ORDER BY a.id DESC
+WITH ranked AS (
+    SELECT
+        a.id, a.feed_id, a.guid, a.title, a.url, a.is_read,
+        f.id as feed_id_2, f.url as feed_url, f.title as feed_title, f.is_subscribed as feed_is_subscribed,
+        ROW_NUMBER() OVER (PARTITION BY a.guid ORDER BY a.id) as rn
+    FROM articles AS a
+    INNER JOIN feeds AS f ON a.feed_id = f.id
+    WHERE a.is_read = 0 AND f.is_subscribed = 1 AND f.user_id = ?
+)
+SELECT id, feed_id, guid, title, url, is_read, feed_id_2, feed_url, feed_title, feed_is_subscribed
+FROM ranked
+WHERE rn = 1
+ORDER BY id DESC
 LIMIT 100;
 
 -- name: GetReadArticles :many
-SELECT
-    a.id, a.feed_id, a.guid, a.title, a.url, a.is_read,
-    f.id as feed_id_2, f.url as feed_url, f.title as feed_title, f.is_subscribed as feed_is_subscribed
-FROM articles AS a
-INNER JOIN feeds AS f ON a.feed_id = f.id
-WHERE a.is_read = 1 AND f.is_subscribed = 1 AND f.user_id = ?
-ORDER BY a.id DESC
+WITH ranked AS (
+    SELECT
+        a.id, a.feed_id, a.guid, a.title, a.url, a.is_read,
+        f.id as feed_id_2, f.url as feed_url, f.title as feed_title, f.is_subscribed as feed_is_subscribed,
+        ROW_NUMBER() OVER (PARTITION BY a.guid ORDER BY a.id) as rn
+    FROM articles AS a
+    INNER JOIN feeds AS f ON a.feed_id = f.id
+    WHERE a.is_read = 1 AND f.is_subscribed = 1 AND f.user_id = ?
+)
+SELECT id, feed_id, guid, title, url, is_read, feed_id_2, feed_url, feed_title, feed_is_subscribed
+FROM ranked
+WHERE rn = 1
+ORDER BY id DESC
 LIMIT 100;
 
 -- name: GetArticlesByFeed :many
-- 
cgit v1.2.3-70-g09d2

