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 7901913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:21:19+00:00 2026-06-03T09:21:19+00:00

In my app articles have many children or parent articles through a self referential

  • 0

In my app articles have many children or parent articles through a self referential joining model article_relationships

class Article < ActiveRecord::Base

  has_many  :parent_child_relationships,
            :class_name   => "ArticleRelationship",
            :foreign_key  => :child_id,
            :dependent    => :destroy
  has_many  :parents,
            :through    => :parent_child_relationships,
            :source     => :parent

  has_many  :child_parent_relationships,
            :class_name   => "ArticleRelationship",
            :foreign_key  => :parent_id,
            :dependent    => :destroy
  has_many  :children,
            :through    => :child_parent_relationships,
            :source     => :child
end

class ArticleRelationship < ActiveRecord::Base
  belongs_to :parent, :class_name => "Article"
  belongs_to :child, :class_name => "Article"
end

I’ve got a fairly complex query on article_relationships that digs down into the article table

ArticleRelationship.joins(:parent, :child).where("((articles.type IN (:parent_article_type) AND parent_id IN (:ids)) OR (children_article_relationships.type IN (:child_article_type) AND child_id IN (:ids)) AND (article_relationships.created_at > :date OR article_relationships.updated_at > :date))", {:parent_article_type => "Emotion", :child_article_type => "Gateway", :ids => [1,2,3,4], :date => "2010-01-01"})

Is there any way I can index this effectively?

  • 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-03T09:21:20+00:00Added an answer on June 3, 2026 at 9:21 am

    So, just for readability,

    ArticleRelationship.joins(:parent, :child).
     where("((articles.type IN (:parent_article_type) AND parent_id IN (:ids)) OR
             (children_article_relationships.type IN (:child_article_type) AND child_id IN (:ids))
            AND (article_relationships.created_at > :date OR article_relationships.updated_at > :date))",
           { :parent_article_type => "Emotion",
             :child_article_type => "Gateway",
             :ids => [1,2,3,4], :date => "2010-01-01" })
    

    The problem is the OR operators. Because of the OR, in both of your top level AND expressions, the database cannot use an index.

    1. Since, when a record is created, it also sets the updated_at field to the same timestamp, you can remove the OR created_at, allowing you to index on update_at.
    2. If you can filter the parent ids and child ids by article type, you can remove those ORs. However, make sure you can do so efficiently enough to not spend more time filtering the id lists as input, that it doesn’t take longer than the unindexed query. Try 1., first, and then see how long it takes with an updated_at index.

    If you do both 1 and 2, be sure to create one index with all three fields, updated_at, parent_id, and child_id.

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

Sidebar

Related Questions

I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
I have a simple cakephp app with table articles that has a cat_id column
I have an Ember.js app as follows (borrowed from this article on andyMatthews.net): View:
I have a really simple Rails app. Basically an article with comments. I want
I created a push notifications app following this tutorial: http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html It works and when
I have hibernate database with three model: Article - which has is part of
I have two entities named Category and Article which have a many to many
i have read many articles that can be found on soap , i am
I got an article app and trying to make a custom filter, I have
I have a basic CRUD web app where people can create articles/edit them. I

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.