Boris Bresciani

Joined

850 Experience
1 Lesson Completed
1 Question Solved

Activity

Posted in How can I improve query time in SQL ? (PostgreSQL)

Any ?

Posted in How can I improve query time in SQL ? (PostgreSQL)

Hi,

I have 4 models:

class Match < ApplicationRecord
  belongs_to :tournament
  scope :next, -> { where('date > ?', Time.zone.now) }
end
class Tournament < ApplicationRecord
  belongs_to :region
  has_many :matches, dependent: :destroy
end
class Region < ApplicationRecord
  belongs_to :sport
  has_many :tournaments, dependent: :destroy
  has_many :matches, through: :tournaments
end
class Sport < ApplicationRecord
  has_many :regions, dependent: :destroy
  has_many :matches, through: :regions
end

I'm looking for the number of matches in the future from Sport, so actually i do this:
Sport.first.matches.next.size

But my problem is that sometimes it's very long to look for it especially when I have a lot of data (well it's normal) but I'm trying to optimize)

Thank you for your help !

Boris

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.