Skip to content

Fix my Indexes #178

Description

@ChaelCodes

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

  • Add index for [buddy_id, friend_id] on friendship

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs workthis ticket is not fully groomed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions