Sample Header Ad - 728x90

Why the second query is so much faster than the first one?

1 vote
1 answer
2694 views
I have a query that look like this:
SELECT
  DISTINCT "articles"."id",
  "articles"."company_uuid",
  "articles"."status",
  "articles"."discount_reference",
  "articles"."created_at"
  --- more columns
FROM
  "articles"
  INNER JOIN "customers" ON "customers"."rec" = 'f'
  AND "customers"."article_id" = "articles"."id"
WHERE
  "articles"."type" = 'sending_request'
  AND "articles"."hidden" = 'f'
  AND "articles"."discount_reference" = 'Discount/737251623'
  AND "articles"."api_domain" = 'company'
  AND "articles"."status" IN ('completed', 'active')
  AND (customers.search_text ILIKE unaccent('%verb%'))
ORDER BY
  authored_on DESC NULLS LAST
LIMIT
  20 OFFSET 0;
The first query is slow, but when I reran it is always faster. I do not see a clear answer when I compare plans. I have a gin trigram index for the customers search text. https://explain.dalibo.com/plan/b11657f576699fa8 And second run https://explain.dalibo.com/plan/g81h74b9g521g5e7 Is the difference in the IO & Buffers the source of the difference? I am running on PostgreSQL 14 on RDS.
Asked by Mio (651 rep)
May 16, 2023, 04:26 PM
Last activity: May 17, 2023, 08:10 AM