Save 36% for Black Friday! Learn more

All threads / How can I improve query time in SQL ? (PostgreSQL)

Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Boris Bresciani asked in Databases

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

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 39,609+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.