Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7972179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:47:52+00:00 2026-06-04T07:47:52+00:00

The question may be long but the problem is actually simple. I have 3

  • 0

The question may be long but the problem is actually simple. I have 3 models: List, Appearance and Movie. A List has many Movies through an Appearance, and they should be sorted by the attribute rank of the joins model. So what indexes should I be using? This is how my models currently look:

# Models
class Movie < ActiveRecord::Base  
  has_many :appearances, :dependent => :destroy
  has_many :lists, :through => :appearances
end

class Appearance < ActiveRecord::Base
  belongs_to :list
  belongs_to :movie
end

class List < ActiveRecord::Base
  has_many :appearances, :dependent => :destroy
  has_many :movies, :through => :appearances

  def self.find_complete(id)
    List.includes({:appearances => :movie}).where("appearances.rank IS NOT NULL").order("appearances.rank ASC").find(id)
  end
end

And here are the indexes I already have. Do I need a composite index? Or just an index by rank?

# Tables
class CreateAppearances < ActiveRecord::Migration
  def change
    create_table :appearances do |t|
      t.integer :list_id, :null => false
      t.integer :movie_id, :null => false
      t.integer :rank
    end
    add_index :appearances, :list_id
    add_index :appearances, :movie_id
  end
end

Finally, is there any way to refactor that find_complete List method? Like using a default_scope? Don’t forget that a rank may be null.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:47:54+00:00Added an answer on June 4, 2026 at 7:47 am

    ActiveRecord is doing two queries.

    SELECT DISTINCT `lists`.id
    FROM `lists` LEFT OUTER JOIN `appearances` ON `appearances`.`list_id` = `lists`.`id`
                 LEFT OUTER JOIN `movies` ON `movies`.`id` = `appearances`.`movie_id`
    WHERE `lists`.`id` = 1 AND (appearances.rank IS NOT NULL)
    ORDER BY appearances.rank ASC LIMIT 1
    

    and

    SELECT `lists`.`id` AS t0_r0, `lists`.`name` AS t0_r1, `lists`.`created_at` AS t0_r2, `lists`.`updated_at` AS t0_r3, `appearances`.`id` AS t1_r0, `appearances`.`list_id` AS t1_r1, `appearances`.`movie_id` AS t1_r2, `appearances`.`rank` AS t1_r3, `appearances`.`created_at` AS t1_r4, `appearances`.`updated_at` AS t1_r5, `movies`.`id` AS t2_r0, `movies`.`name` AS t2_r1, `movies`.`created_at` AS t2_r2, `movies`.`updated_at` AS t2_r3
    FROM `lists` LEFT OUTER JOIN `appearances` ON `appearances`.`list_id` = `lists`.`id`
                 LEFT OUTER JOIN `movies` ON `movies`.`id` = `appearances`.`movie_id`
    WHERE `lists`.`id` = 1 AND `lists`.`id` IN (1) AND (appearances.rank IS NOT NULL)
    ORDER BY appearances.rank ASC
    

    In both cases what you need is an index with both lists.id and appearances.rank. Some databases will create indexes on multiple tables. I also don’t know if your database will be smart enough to use an index on appearances.list_id and appearances.rank, but that is worth a try.

    add_index :appearances, [:list_id, :rank]
    

    If that doesn’t help, at least index :rank, so the database might be able to avoid the sort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem/question that I may have been staring at too long today.
Problem This question may seem a bit long, but I try to give as
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This question may have been asked before, but I had trouble finding an answer,
This one may seem like a simple question, but it's really got me scratching
Apologies in advance for the long-winded question. I'm really a database programmer, but have
This question may seem trivial, but I hope you won't ignore it. Before destroying
The question may be a little vague but here's an example of what I
This question may not directly relate to programming. I have noticed that the technology
The question may sound a bit stupid but I think it's interesting. Is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.