0

Here is the issue I am facing. I have a User model that has_one profile. The query I am running is to find all users that belong to the opposite sex of the current_user and then I am sorting the results by last_logged_in time of all users.

The issue is that last_logged_in is an attribute of the User model, while gender is an attribute of the profile model. Is there a way I can index both last_logged_in and gender? If not, how do I optimize the query for the fastest results?

3 Answers 3

1

An index on gender is unlikely to be effective, unless you're looking for a gender that is very under-represented in the table, so index on last_logged_in and let the opposite gender be filtered out of the result set without an index.

It might be worth it if the columns were on the same table as an index on (gender, last_logged_in) could be used to identify exactly which rows are required, but even then the majority of the performance improvement would come from being able to retrieve the rows in the required sort order by scanning the index.

Stick to indexing the last_logged_in column, and look for an explain plan that demonstrates the index being used to satisfy the sort order.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it a smart idea to index last_logged_in? Because last_logged_in column will be getting updated constantly as users sign in and sign out. Will this increase the overhead in maintaining the index? And would it actually help make the query faster?
Yes I think it would be fine -- every user's value would only update when they login, and that's a "human speed" event so it's not like it'll happen every 10 seconds or anything. The index would be of value if the optimiser can use it to order the result set, as the alternative is to fully scan the table and order the entire set.
1
add_index :users, :last_logged_in
add_index :profiles, :gender

This will speed up finding all opposite-sex users and then sorting them by time. You can't have cross-table indexes.

2 Comments

Is it a smart idea to index last_logged_in? Because last_logged_in column will be getting updated constantly as users sign in and sign out. Will this increase the overhead in maintaining the index? And would it actually help make the query faster?
To my best knowledge you should index each column that you perform search or sort on.
1

I am just writing the query for that

In your User model

def self.get_opposite_sex_users(current_user) 
  self.joins([:profile]).where("profiles.gender = ?", (current_user.profile.gender ==  'M') ? 'F' : 'M').order('users.last_logged_in DESC')
end

In your action you can call it by User.get_opposite_sex_users(current_user)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.