Friday, May 28, 2021

When to index a foreign key column?

I don't post so much here these days. I'm more active on Reddit where I'm actually interacting with people. :) But here's a comment I wrote on Reddit yesterday about my thoughts on when to index a foreign key column. https://old.reddit.com/r/PostgreSQL/comments/nmdkgn/beware_the_missing_foreign_key_index_a_postgres/gzpw8qx/

The original poster blogged about a pitfall he ran into, in which his deletion of a parent table was very slow due to a lack of an index on the child's foreign key column. So I just thought I'd throw in my two cents on the subject.

Here is the original blog post I was responding to: https://dev.to/jbranchaud/beware-the-missing-foreign-key-index-a-postgres-performance-gotcha-3d5i - I like that he explains well and constructs good test cases. In fact, he provided CREATE TABLE statements, along with the INSERT statements, that I could just copy/paste into my local Postgres instance, which made it a lot easier to run my own tests for my reply.