Compound vs Singular Indexes? #327
Replies: 2 comments 1 reply
-
Without commenting on specific decisions made for this gem, you might find the following two pages from the postgres documentation helpful (if you haven't seen them already): https://www.postgresql.org/docs/current/indexes-multicolumn.html I find the last paragraph of the second link particularly informative:
|
Beta Was this translation helpful? Give feedback.
-
To be honest, I don't have a strong reason (e.g. based on explicit profiling for each index and use case), but just my own inertia and experience. The explanation of that is: the majority of queries are time- and order- based and I'm trying to optimize for index scans. And I read Use the Index Luke maybe 5 years ago (it's an excellent book). More generally, I think indexes are relatively inexpensive, and a slow query can be expensive. Which is also an explanation/apology for defensive indexing. If you did want to dig into query plans, that would be really helpful. I like this Active Record Explain-Analyze gem (there are a couple): 6/activerecord-explain-analyze#3 |
Beta Was this translation helpful? Give feedback.
-
I started playing around with good_job tonight as it seems like a pretty compelling option for our use case: ActiveJob native, uses existing db server, nice development runtime, job uniqueness, and the newly added cron was the final killer feature. Thank you!
I'm also new to PostgreSQL, so while I'm enjoying some of the new toys to play with, I'm also learning some new best practices vs MySQL.
I was noticing in the generated migrations that a few compound indexes were created that had overlapping columns and no uniqueness requirements. Coming from MySQL, where I had to do this a fair amount, it was my understanding that PostgreSQL is smart about mixing and matching its use of indexes. Is there something in particular about your use case that the compound indexes optimize for? I ask mostly to help correct any misconceptions I have.
As a thought, I broke up the indexes to remove the overlap (I think) while maintaining the partial indexes as appropriate. I'd think the biggest win would be eliminating duplication of created_at (on all rows) and scheduled_at (on non-finished rows) in multiple indexes. This does add an additional index, so maybe that's worse.
Edit: After a few moments, I'm wondering it's a sorting thing since job_id and cron_key aren't ints...
Thank you again!
Beta Was this translation helpful? Give feedback.
All reactions