r/PostgreSQL • • May 18 '18

How to speed up PostgreSQL performance 100x

https://statsbot.co/blog/postgresql-query-optimization/
1 Upvotes

15 comments sorted by

11

u/[deleted] May 18 '18 edited Jun 01 '24

bike support cautious ad hoc gaping correct voiceless dazzling cheerful divide

This post was mass deleted and anonymized with Redact

7

u/dark-panda May 18 '18

Truly a groundbreaking revelation

2

u/[deleted] May 18 '18

[removed] — view removed comment

5

u/punpunpun May 18 '18

Wait till they hear about a WHERE clause...

3

u/antlife May 18 '18

Is that like SANTA clause!?!?

6

u/therealgaxbo May 18 '18

There's some fairly dubious advice in that article:

Tip: Put in the first place columns, which you use in filters with the biggest number of unique values.

While having the most selective columns at the start of an index will lead to a slight improvement, I think this is a distant third-place consideration behind choosing the leading columns based on how many queries it will be useful for, and most importantly, choosing leading columns that are tested for equality not ranges. Which brings us on to...

Tip: Date columns are usually one of the best candidates for the first column in a multicolumn index [...]

Wut? Date columns are almost always used in range conditions - indeed that's how they are used in the example given. That means that the second column in the index may as well not even be there. You can even see in the explain output that it's not used!

2

u/jakdak May 18 '18

"Tip: Create one index per unique query for better performance."

This is a horrible guideline and far better stated at "Make sure that every key query in your system is supported by your indexing strategy"

But if you aren't doing that anyway you shouldn't be writing database code.

0

u/ptiunov May 19 '18

Yeah. It's a good note. And indexing strategy is inevitable for OLTP systems but not a requirement for analytic systems. And these tips are mostly for analytic queries.

2

u/mage2k May 18 '18

Date columns are almost always used in range conditions

And sorts, which are another reason to have them at the end of the index.

1

u/ptiunov May 19 '18

Could you please provide example for it?

1

u/platohut Guru May 18 '18

While having the most selective columns at the start of an index will lead to a slight improvement,

The improvement can be slight or massive based on the frequency of the query using it, and the difference between the uniqueness of values between the columns in the index. I've had postgres refusing to use index altogether, or opting to use filter after index just because of the ordering.

0

u/ptiunov May 19 '18

Could you please elaborate on example you're referring to?

1

u/ptiunov May 19 '18

Hi therealgaxbo, You actually brought up really good questions! First of all these tips are mostly for analytic queries which are used mostly in ROLAP solutions. They obviously can be used in OLTP solutions as well but not always relevant. Added this note in the beginning of article to make it clear. For analytics your goal is to optimize read query times at cost of space and insert time unlike in OLTP where there's always tradeoff of speed of read and write operations. Even if index helps to speed only one analytic query it usually enough to consider adding it. Adding columns with high cardinality as a first column of index is a rule of thumb and should be considered for each analytic query in separate. It means equality indexes will be useful where you have this condition in query and won't if you don't have it. Regarding date column example: it's not clear but second order_id column is actually used. I changed example to make it more clear by adding price column. You can note index only scan which says underlying table won't be used at all in this particular query.

4

u/[deleted] May 18 '18

I didn't know they had clickbait articles for databases

1

u/muddiedwaters45 May 19 '18

Articles like this make me want to kill myself. Repeatedly.