-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fetching by a cached index produces two queries #374
Comments
The first query only gets the id for the record, which often can be obtained from a covering index, so wouldn't even read from the primary/clustered index. It then checks the cache for the record using its id and only gets the full record on a cache miss, so it doesn't necessary perform a second query. In your example, you should be able to see that the second query isn't performed if you precede One way of avoiding the extra query would be by embedding the whole record in the cache index, but there are trade-offs with that. That would increase how often the cache index gets invalidated, increase the number of keys that need to be invalidated when the record or any of its embedded associations gets updated and increase the amount of memcached memory used to denormalize this data. |
While debugging N+1 queries, I found an unusual behaviour in IDC: when fetching records using a cached attribute, IDC produces two queries instead of one. The first query uses the cached attribute, while the second uses the primary key.
After some investigation and some very helpful tips from coworkers, I managed to boil the issue down to this snippet:
Then, on
rails console
:I also tried with
unique: true
, it produces the same result.I might be wrong, but I would expect only one query to be produced by this snippet. The first query already fetches the desired record, I see no reason to search for the object again using the primary key.
Thoughts?
The text was updated successfully, but these errors were encountered: