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 Pragprog topics










Other popular topics










Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /preact
- /flutter
- /c-plus-plus
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm