Skip to content
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

N+1 problem happen with using Time.current in where sentence(maybe) #161

Closed
gento-ogane opened this issue Jun 29, 2023 · 2 comments
Closed

Comments

@gento-ogane
Copy link

environment

  • Ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]
  • Rails: 7.0.4
  • graphql 1.13.12
  • graphql-batch 0.4.3
  • graphql-pro 1.22.2

I'm creating Rails app with following modules;

Steps to reproduce

module Loaders
  class CountLoader < GraphQL::Batch::Loader
    def initialize(model, column: model.primary_key, conditions: {})
      super()
      @model = model
      @column = column
      @joins = conditions[:joins]
      @wheres = conditions[:wheres]
      @where_nots = conditions[:where_nots]
      @distinct = conditions[:distinct] || false
    end

    def perform(keys)
      query(keys).each { |key, count| fulfill(key, count) }
      keys.each { |key| fulfill(key, 0) unless fulfilled?(key) }
    end

    private

    def query(keys)
      scope = @model
      scope = scope.joins(@joins) if @joins.present?
      @wheres.each { |w| scope = scope.where(w) } if @wheres.present?
      @where_nots.each { |w| scope = scope.where.not(w) } if @where_nots.present?

      if @distinct
        scope.where(@column => keys).group(@column).distinct.count
      else
        scope.where(@column => keys).group(@column).count
      end
    end
  end
end
module Types
  class User < Types::SchemaObject
    def suggested_similar_property_other_client_count(property_id:)
      end_time = Time.current
      start_time = end_time.ago(30.days).beginning_of_day

      wheres = [
          {
            messages:
            {
              created_at: start_time..end_time,
            },
          },
        ]
      joins = [:messages]
      conditions = { joins:, wheres:, distinct: true }

      Loaders::CountLoader.for(
        ::Chat::Room,
        column: :user_id,
        conditions:,
      ).load(object.id)
    end
  end
end
class User < ApplicationRecord
...
has_many :chat_rooms, class_name: 'Chat::Room'
...
end

class Chat::Room < ApplicationRecord
...
belongs_to :user
has_many :messages
...
end

actual result

graphql-batch generates this SQL;

SELECT COUNT(DISTINCT `chat_rooms`.`id`) AS `count_id`, `chat_rooms`.`user_id` AS `chat_rooms_user_id` FROM `chat_rooms` INNER JOIN `chat_
messages` `messages` ON `messages`.`room_id` = `chat_rooms`.`id` WHERE `messages`.`created_at` BETWEEN '2023-05-16 00:00:00' AND '2023-06-15 18:51:49' AND `chat_rooms`.`user_id` = 136 GROUP BY `chat_rooms`.`user_id`

SELECT COUNT(DISTINCT `chat_rooms`.`id`) AS `count_id`, `chat_rooms`.`user_id` AS `chat_rooms_user_id` FROM `chat_rooms` INNER JOIN `chat_m
essages` `messages` ON `messages`.`room_id` = `chat_rooms`.`id` WHERE `messages`.`created_at` BETWEEN '2023-05-16 00:00:00' AND '2023-06-15 18:51:49' AND `chat_rooms`.`user_id` = 137 GROUP BY `chat_rooms`.`user_id`

expected result

graphql-batch should generate;

SELECT COUNT(DISTINCT `chat_rooms`.`id`) AS `count_id`, `chat_rooms`.`user_id` AS `chat_rooms_user_id` FROM `chat_rooms` INNER JOIN `chat_
messages` `messages` ON `messages`.`room_id` = `chat_rooms`.`id` WHERE `messages`.`created_at` BETWEEN '2023-05-16 00:00:00' AND '2023-06-15 18:51:49' AND `chat_rooms`.`user_id` IN (136, 137) GROUP BY `chat_rooms`.`user_id`

additional information

In my case, this problem is resolved with following code.

 def suggested_similar_property_other_client_count(property_id:)
  end_time = Time.zone.today ## !<= changed!
  start_time = end_time.ago(30.days).beginning_of_day

  wheres = [
      {
        messages:
        {
          created_at: start_time..end_time,
        },
      },
    ]
  joins = [:messages]
  conditions = { joins:, wheres:, distinct: true }

  Loaders::CountLoader.for(
    ::Chat::Room,
    column: :user_id,
    conditions:,
  ).load(object.id)
 end
@swalkinshaw
Copy link
Contributor

This is how graphql-batch works and shouldn't be considered a bug. Since you're passing in a timestamp to for, that becomes part of the batch loader "grouping" cache key. Time.current including seconds (and microseconds) so every call will have a different value of course.

Time.zone.today gets around that since the value will be the same within each day at least.

@gento-ogane
Copy link
Author

I now have a deeper understanding of this good gem.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants