
viktorianer
High Performance PostgreSQL for Rails: Explanation regarding the use of includes with a limit (Page 124)
Dear Andrew, @andatki
I found the explanation regarding the use of .includes()
with a limit
in the context of vehicle reservations to be quite insightful. However, I believe the discussion on performance implications could benefit from further clarification, particularly regarding the difference in behavior with and without a limit
.
Here’s a suggested rewrite to enhance clarity:
When using .includes()
to load vehicle reservations, the query is split into two parts. First, a query retrieves a limited number of vehicles (e.g., 2 vehicles). Then, another query fetches the associated reservations for these vehicles using an IN
clause with the vehicle_id
s of the retrieved vehicles. Keep in mind that if the IN
clause contains too many values, it can lead to performance issues.
Performance Implications
-
Without limit: If there are many vehicles, the
IN
clause can become very large, which may degrade performance. -
With limit: The
IN
clause remains small, improving performance for the secondary query.
Additionally, this explanation can move before LEFT OUTER JOIN
part, in order to avoid a context switch.
I hope this suggestion helps make the performance implications clearer for the readers.
Best regards,
Viktor
Popular Prag Prog topics










Other popular topics










Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /zig
- /scala
- /html
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /manjaro
- /django
- /spring
- /diversity
- /nodejs
- /lua
- /julia
- /slackware
- /c
- /neovim