79442915

Date: 2025-02-16 10:00:16
Score: 0.5
Natty:
Report link

You'll not like the consequences if somehow you'll force rails to run this in single query.

Because of the DISTINCT "posts"."id", rails manages to avoid

DISTINCT "posts"."*" . Having the last one condition will force DB to compare records in full, instead of just the primary keys!

And I'm 100% sure that in case:

SELECT DISTINCT "posts"."id" FROM "posts"
    LEFT OUTER JOIN "post_translations" 
    ON "post_translations"."post_id" = "posts"."id" LIMIT $1  [["LIMIT", 3]]

under the hood, this request happens to be Index Only Scan, unless of course you skipped corresponding indexes.

Here the difference between distincts, on a small size dataset:

Distinct.id: enter image description here enter image description here

Distinct.*: enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Алексей Лещук