Rails Postgres Partial Indexing

04-Feb-2021 1308
Postgres has a nice feature where you can index only the values you want to query on. Today I learned that Rails has a nice shortcut for taking advantage of said feature.While working on a new Box Out feature, I needed to query for graphics that have been shared on social media.I had a graphics table with a tweet_id column. I wanted to query for graphics that have been shared on twitter (aka tweet_id IS NOT NULL). The table is in the hundreds of thousands of rows, so I wanted to index tweet_id to help the query along. I was going to a plain old add_index. But then I remembered that you can do partial indexes.
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle