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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:52:25+00:00 2026-05-27T14:52:25+00:00

I have a model called A with columns col and col2 . The col1

  • 0

I have a model called A with columns col and col2. The col1 has an unique index. (I am using Postgres database.)

class A < ActiveRecord::Base
  # id, col1, col2
end

Data:

id  col1   col2
==  ====   ====
1   2       3
2   3       4

Let us try to find the first row matching the given value in col2 OR col1.

a = A.first(:conditions=> ["col2 = :id OR col1 = :id", {:id => 3}])
# select * from A where (col2 = 3 OR col1 = 3) LIMIT 1

The SQL above returns the row#2 rather than row#1. I suspect the query optimizer chooses to execute col1 = 3 first as col1 has an unique index.

How do I override this behavior? How do I instruct Postgres optimizer to use the existing order for the OR condition?

  • 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-05-27T14:52:26+00:00Added an answer on May 27, 2026 at 2:52 pm

    If you don’t specify an explicit order then first row doesn’t make any sense; the result set from a specific query at a specific moment in time will, of course, have a first row but there is no guarantee that running the same query again with the same data will produce the same first row. Tables in a relational database are not ordered so there is no specified existing order at all. You’re making a bad assumption; the implementation may implicitly order the records by the PK on the disk but there’s no guarantee of that and no guarantee that the next version will behave the same; furthermore, there’s no guarantee that the database will return rows in disk-order, the database is free to order the rows in any way it wants to unless you have specified a specific order with an ORDER BY clause.

    If you want an order then you should include an ORDER BY clause; you probably want something more like this:

    a = A.where("col2 = :id OR col1 = :id", {:id => 3}).order(:id).first
    

    And if you want to look at col2 before col1 then you should say so:

    a =  A.where('col2 = :id', :id => 3).order(:id).first \
      || A.where('col1 = :id', :id => 3).order(:id).first
    

    col2 = :id OR col1 = :id and col1 = :id OR col2 = :id are equivalent boolean expressions so the database is free to check col2 = :id before, after, or at the same time as col1 = :id and it is free to access the rows in any order it pleases.

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

Sidebar

Related Questions

I have a Rails model called Person which has database table columns for first_name
I have model called Product and it has the following columns: create_table :products do
I am using Rails v2.3.2 . I have a model called UsersCar : class
I have a model called Contact which has_one guest like so. class Contact <
I have a model called Answer which has a ForeignKey relationship to another model
I have a model called company that has_many users then users belongs_to company. class
Say I have a model called User that has the following parameters: favorite_color, favorite_animal,
Let's assume I have a model called product. Let's assume that product has three
I have a model called shipments. I added some columns to the shipments table
I have a model called event_recurrences it contains 2 important columns event_id and cached_schedule

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.