Shopify / Shopify/identity_cache
Fetching by a cached index produces two queries
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 2k
- Forks
- 174
- Avg merge
- 13m
- Merged PRs (30d)
- 3
Description
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:
# migration file
class CreateSampleRecords < ActiveRecord::Migration[5.2]
mark_as_podded_migration!
def up
create_table :sample_records do |t|
t.string :key
t.timestamps
end
end
def down
drop_table :sample_records
end
end
# ActiveRecord model
class SampleRecord < ApplicationRecord
include IdentityCache
cache_index :key
end
Then, on rails console:
[1] pry(main)> SampleRecord.create(key: '123')
(1.6ms) BEGIN
SampleRecord Create (1.0ms) INSERT INTO `sample_records` (`key`) VALUES ('123')
(2.3ms) COMMIT
=> #<SampleRecord:0x00007fa11061a158 id: 1, key: "123">
[2] pry(main)> SampleRecord.fetch_by_key('123')
(1.4ms) SELECT `sample_records`.`id` FROM `sample_records` WHERE `sample_records`.`key` = '123'
SampleRecord Load (0.9ms) SELECT `sample_records`.* FROM `sample_records` WHERE `sample_records`.`id` = 2
=> [#<SampleRecord:0x00007fa0f5169a70 id: 1, key: "123">]
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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the behavior in a Rails console with the SampleRecord model, cache_index :key, and SampleRecord.fetch_by_key('123'). Then read the fetch_by_key and cache_index entry points to determine why the primary-key lookup follows the cached-index query. Done means the reproduction performs one query and has regression coverage for the cached-index fetch.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100