Skip to content

feature: faster article list endpoints (no DISTINCT joins, list indexes, batched GraphQL authors) - #1256

Open
matthewguerra-cog wants to merge 1 commit into
masterfrom
devin/task-15-article-list-latency
Open

matthewguerra-cog wants to merge 1 commit into
masterfrom
devin/task-15-article-list-latency

Conversation

@matthewguerra-cog

@matthewguerra-cog matthewguerra-cog commented Nov 17, 2026 •

Copy link
Copy Markdown

Summary

Article list endpoints were slow for two reasons:

  1. Row-multiplying joins + DISTINCT. queryArticles / countArticle / findArticlesWithCursor joined article_tags, tags, article_favorites and users twice on every call (even with no filter), then de-duplicated with DISTINCT / count(DISTINCT) and sorted in a temp B-tree. Filters are now IN sub-selects on a plain articles scan, so no join, no DISTINCT:
    select A.id, A.created_at from articles A
    where A.id in (select AT.article_id from article_tags AT join tags T on T.id = AT.tag_id where T.name = ?)   -- only if tag
      and A.user_id in (select id from users where username = ?)                                               -- only if author
      and A.id in (select AF.article_id from article_favorites AF join users U on U.id = AF.user_id where U.username = ?) -- only if favorited
    order by A.created_at desc limit ?, ?
  2. GraphQL author N+1. Article.author called ProfileQueryService.findByUsername per article (2 statements each → 46 statements for 20 articles). It now goes through a DGS MappedBatchLoader (ProfileDataLoader) backed by ProfileQueryService.findByUsernames (one users ... in (...) + one followingAuthors query).

Plus V2__add_article_list_indexes.sql: article_tags(tag_id), article_favorites(user_id, article_id), follows(user_id, follow_id) (all create index if not exists, additive only). I deliberately did not index articles(created_at) or article_tags(article_id): with sqlite-jdbc 3.36 either one changes the hydration plan in findArticles / findArticlesOfAuthors, which have no ORDER BY for tags, so tagList order changed. Without them, the responses are byte-for-byte the same as before.

DefaultJwtServiceTest has a one-line formatting fix only because spotlessJavaCheck fails on master without it.

Before / after

Seeded SQLite: 50 users, 2,000 articles, 30 tags (4,054 article-tag rows), 5,000 favorites, 500 follows (sample values, random.seed(42)). Authenticated as a user who follows 10 authors. 30 warm-up requests, then 200 sequential requests per endpoint; both jars ran on the same box with identical DB copies. SQL count = MyBatis ==> Preparing lines per request.

Endpoint p50 before → after (ms) p95 before → after (ms) SQL statements/request
GET /articles 32.9 → 11.1 70.9 → 22.8 7 → 7
GET /articles?tag=tag7 40.0 → 9.8 64.8 → 13.3 7 → 7
GET /articles/feed 16.3 → 8.5 36.6 → 18.1 7 → 7
GraphQL articles(first: 20) 24.0 → 13.3 41.9 → 26.9 46 → 8

REST statement counts stay at 7 because fillExtraInfo already batches; the REST win comes from cheaper statements. Response parity: 26 REST and GraphQL requests (list, tag, author, favorited, combined filters, offsets, feed, cursor first/last, anonymous and authenticated) return identical bodies before and after.

./gradlew spotlessJavaCheck build passes on JDK 11; a new ProfileQueryServiceTest covers the batch lookup, including following state and missing usernames.

Overlaps with open PRs

Devin-Org: engineering


Latency Optimizer opened this PR for Matthew
Implemented in session Speed up RealWorld API article list endpoints
Review with Devin · Open in Devin Desktop


@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Custom reviewers

  • 🧪 Test 2 reviewer: no findings · View
  • 🚩 TD Enterprise Architecture: no findings · View
  • 👁️ TD Information Security: no findings · View
  • 🧠 TD French Language (Bill 96): no findings · View
  • ⏱️ TD API Contracts: no findings · View

Devin Review

@devin-ai-integration

Copy link
Copy Markdown

Migration Engineer review — SQL/schema safety vs #1252: no blocking issues.

Non-blocking: the Flyway V2 version number collides with #1233 (V2/V3) and the other open PRs listed in the description. Whichever merges second must be renumbered before it lands, or startup fails with a duplicate-version error. Because of if not exists, renumbering this file stays safe.

Written by Devin

@devin-ai-integration

Copy link
Copy Markdown

Review of #1256. I found no blocking bugs. The new IN-subquery filters behave the same as the old DISTINCT joins, and the DataLoader setup looks correct. Risks:

  • suggestion: ProfileDataLoader.load calls SecurityUtil.getCurrentUser() when the DataLoader dispatches, which depends on the security context still being on that thread. That holds today because dispatch is synchronous. If dispatch ever moves to another thread, authentication is null and SecurityUtil throws a NullPointerException. It does not check for null.
  • suggestion: There is no maxBatchSize on the loader, so findByUsernames builds an unbounded IN (...) list. A large first: value could hit SQLite's bound-parameter limit.
  • suggestion: V2 adds no index on articles(created_at) or articles(user_id). Every list query still sorts by created_at desc, and the author filter uses A.user_id in (...), so both stay table scans.
  • question: getAuthor now throws ResourceNotFoundException inside thenApply, so it arrives wrapped in a CompletionException. Check that the GraphQL error type is still NOT_FOUND.
  • nit: Comment authors (getCommentAuthor) and User.profile still load one profile at a time. The DefaultJwtServiceTest reformat is unrelated to this change.

Gaps filled (#1257):

  • DGS slice test: authors load in one findByUsernames call; a missing profile returns an error.
  • ProfileDataLoader unit tests.
  • UserReadService.findByUsernames mapper test.
  • ProfileQueryService.findByUsernames: empty input, anonymous viewer, no matching usernames.
  • Article list queries (no longer using DISTINCT): no duplicates or overcounting with several tags or favorites, combined filters, unknown author or favoriter, and tag-filtered cursor paging in both directions.
  • Check that the V2 indexes exist.

Not yet run: ./gradlew spotlessJavaApply test did not run. Maven Central returns HTTP 429, and the Google mirror hosts Gradle is set up to use are blocked, so some test runtime jars could not be downloaded. The tests compile and were formatted with google-java-format 1.13.0, the version Spotless uses. Once one of the pending network requests is approved, I'll run the suite and push any fixes. Until then, run ./gradlew test on #1257 before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant