Skip to content

Preload multiple attributes at once - N+1 queries #139

Open
@Laykou

Description

@Laykou

Is there a way to preload multiple attributes at once?

Having:

class Transaction < ApplicationRecord
  kredis_datetime :locked_at
end


Transaction.limit(100).each do |transaction|
  puts transaction.locked_at
end

triggers 100 Kredis Proxy .... calls

Can I somehow use e.g. redis.multi method to do this only once?

I tried wrapping it somehow into:

transactions = Transaction.limit(100).to_a

$redis.multi do
  transactions.each { |e| e.locked_at } # do the preload
end

# later I expected to use the `.locked_at` without additional call to Redis

but this resulted only in a warning:

Pipelining commands on a Redis instance is deprecated and will be removed in Redis 5.0.0.

redis.multi do
  redis.get("key")
end

should be replaced by

redis.multi do |pipeline|
  pipeline.get("key")
end

Thank you for your help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions