DEV Community

Discussion on: What database do you use?

Collapse
 
zchtodd profile image
zchtodd

PostgreSQL is probably my favorite database too, and I totally agree, it's almost always my crappy queries that are to blame!

The case I talked about in the post was really interesting though, because the query planner was producing radically different plans, all depending on how "fresh" its table stats were.

I learned that table stats are automatically refreshed on a commit, but my slow query was running inside the same transaction, after a huge insert. So the insert skewed the table data in a way that the stats didn't reflect, therefore producing a bad plan.

The ANALYZE command (which is different than EXPLAIN ANALYZE), when executed against a table, tells PG to sample the table contents and refresh its statistics.