ChaelCodes / MeetAnotherDay

An app to help you find and meet up with your friends at conferences.
MIT License
35 stars 20 forks source link

Fix my Indexes #178

Open ChaelCodes opened 7 months ago

ChaelCodes commented 7 months ago

I think there's some missing indexes that could really improve performance on our queries. There's one missing from friendships, but there are probably others.

How to test indexes, even without enough data.

In the database.yml, add the following to the appropriate environment (development/test).

  variables: {
    enable_seqscan: 'off'
  }

Then restart the rails server, and all future requests will attempt to use any index available before doing a seq scan.

Indexes Needed

Query attempted in profile.friends

Makes a nested loop - check if it's cleaner after indexes?

    test_query = Profile.where(Profile.arel_table[:id].in(Arel.sql(<<-SQL.squish, profile_id: id)))
      SELECT CASE
        WHEN friendships.friend_id = :profile_id THEN friendships.buddy_id
        WHEN friendships.buddy_id = :profile_id THEN friendships.friend_id
      END AS profile_id
      FROM friendships
      WHERE (friend_id = :profile_id
      OR buddy_id = :profile_id)
      AND status = 0
    SQL