{
  "id": 11154651,
  "title": "max(x) uses the index, max(x) FILTER (where ...) scans the table",
  "url": "https://urgent.news/2026/10/01/max-x-uses-the-index-max-x-filter-where-scans-the-table",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-01T09:14:56.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/daniel_pertu/maxx-uses-the-index-maxx-filter-where-scans-the-table-64j"
  },
  "original_language": "en",
  "account": "The source material outlines how a mobile app called Munchable uses an ingredient database to determine if a packaged food item fits a user's gut condition. The key issue discussed is the performance of a SQL query that finds the latest version of the ingredient data based on a status field called \"active.\"\n\nWhen the query is written as MAX(updated_at) FILTER (WHERE status = 'active'), Postgres cannot utilize an index to perform this operation efficiently. This is because the FILTER clause prevents the optimizer from rewriting the query to use the index. The query instead forces a sequential scan of the entire table and applies an aggregate function, which is much slower.\n\nBy rewriting the query as MAX(updated_at) WHERE status = 'active', Postgres can use the index on the updated_at column to quickly find the most recent row with the active status. This results in a significant performance improvement, reducing the query time from 815 milliseconds to a few milliseconds.\n\nThe article also mentions a related issue with caching the ingredient data in Redis, where the Time-To-Live (TTL) was set to five minutes. This TTL was being used as a fallback in case of cache eviction, rather than as a measure of data freshness. The write path already ensures that the snapshot and version are updated whenever the data changes, so the TTL was not actually providing fresh data but rather serving as a backup mechanism. The article concludes by adjusting the TTL to 24 hours, recognizing that it should only be a backup in case of eviction, not a freshness guarantee.",
  "summary": "Munchable reads a packaged food's label and tells you whether it fits your gut condition. Point the phone at a barcode, get a verdict in about a second. The conditions it reasons about are at munchable.app/conditions , and a public slice of the ingredient reasoning is at munchable.app/answers , where every page is produced by running the same engine the app runs. Between the barcode and the…",
  "key_points": [
    "MAX(updatedat) FILTER (WHERE status = 'active') cannot use index efficiently",
    "Rewriting query to MAX(updatedat) WHERE status = 'active' improves performance",
    "Adjusted TTL to 24 hours for ingredient data caching"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}