-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathabout_indexing.qmd
601 lines (431 loc) · 14.3 KB
/
about_indexing.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# About Indexing
```{python}
# | echo: false
import django_init
from rich import print
from rich.console import Console
from rich.table import Table
from rich.markdown import Markdown
from django.db import connection
from books.models import *
from utils.perf_display import perf_counter, format_duration
from rest_framework.test import APIClient
from collections import defaultdict
from rest_framework.test import APIClient
from utils.sql import use_indexes, disable_indexes
console = Console()
client = APIClient()
```
```{python}
# | echo: false
from django.db.models import Count
# id not from the 10 libraries with a lot of books
personal = Library.objects.get(id=100)
personal_id = personal.id
public = Library.objects.order_by("-id").first()
public_id = public.id
# Get the library with the most books
alexandria_id = 7
alexandria = Library.objects.get(id=alexandria_id)
previous_alexandria_id = 6
```
```{python}
# | echo: false
from utils.sql import toggle_all_custom_indexes
toggle_all_custom_indexes(False)
```
## 2nd exercice
Let's continue with another usual example.
We want to implement a standard endpoint returning a page of filtered and ordered reviews.
A minimal implementation is available in `books/views/review/ordered.py`
```{.python code-line-numbers="|3,8,15|5,17"}
class ListReviewsView(GenericAPIView):
filter_backends = (OrderingFilter,)
ordering_fields = ["written_at", "id", "rating"]
ordering = ["written_at"]
pagination_class = NoCountHeaderPagination
def get_queryset(self, library_id) -> QuerySet:
return Review.objects.filter(library_id=library_id)
def get(self, request, library_id: int) -> Response:
"""
Similar to rest_framework.mixins.ListModelMixin
with simplified serialization
"""
queryset = self.filter_queryset(self.get_queryset(library_id))
queryset = queryset.values() # simplified serialization
page = self.paginate_queryset(queryset)
return self.get_paginated_response(page)
```
## Ordering by `id` and `-written_at`
```{python}
def get_ordered_page(ordering):
with perf_counter(message=f"ordering by {ordering}", time_sql=True, print_sql=True):
return client.get(f"/reviews/{alexandria_id}/ordered?ordering={ordering}")
```
<br>
```{python}
page = get_ordered_page("id")
```
<br>
```{python}
page = get_ordered_page("-written_at")
```
## Complete Benchmark
```{python}
# | echo: false
from books.tests.benchmarks import benchmark_list_reviews
```
:::: {.columns}
::: {.column width="50%"}
```{python}
#| echo: false
# benchmark the ordering endpoint
benchmark_list_reviews(
["simple-list-reviews", "ordered-list-reviews"],
library_ids=[public_id],
)
```
:::
::: {.column width="50%"}
```{python}
#| echo: false
# benchmark the ordering endpoint
benchmark_list_reviews(
["simple-list-reviews", "ordered-list-reviews"],
library_ids=[alexandria_id],
)
```
:::
::::
::: {.incremental}
A few remarks about these results:
- All these orderings are quite fast for `Public`, and all about the same duration, except for `id`
- `Alexandria` is usually slower than `Public`, except for `id`, which is significantly faster
- All `Alexandria` orderings are slow, except `id`
:::
---
### `EXPLAIN ANALYZE`
We saw previously we cannot explain these differences just by looking at the SQL query.
We'll now use a new tool: the **`EXPLAIN ANALYZE` SQL statement**.
<br>
```{python}
#| code-line-numbers: "|8"
def get_reviews(library_id, ordering, page_size=20):
return Review.objects.filter(library_id=library_id).order_by(ordering)[:page_size]
def explain_qs(library_id, ordering, print_sql=False):
review_qs = get_reviews(library_id, ordering)
with perf_counter(message=f"ordering by {ordering}", time_sql=True, print_sql=print_sql):
result = review_qs.explain(analyze=True)
print(" \n ", result)
```
<br>
This will display the exact internal operations used by the PostgreSQL engine.
In particular, we'll be able to inspect which indexes are used by each query.
---
##
```python
explain_qs(alexandria_id, "-written_at", print_sql=True)
```
<br>
```{.python code-line-numbers="|16|21|12-13|12-15|6,9"}
Limit (cost=1447499.48..1447501.81 rows=20 width=211) (actual time=9512.533..9524.073 rows=20 loops=1)
-> Gather Merge (cost=1447499.48..1630832.48 rows=1571316 width=211) (actual time=9373.984..9385.520 rows=20
loops=1)
Workers Planned: 2
Workers Launched: 2
-> Sort (cost=1446499.45..1448463.60 rows=785658 width=211) (actual time=9354.371..9354.376 rows=16
loops=3)
Sort Key: written_at DESC
Sort Method: top-N heapsort Memory: 36kB
Worker 0: Sort Method: top-N heapsort Memory: 38kB
Worker 1: Sort Method: top-N heapsort Memory: 37kB
-> Parallel Seq Scan on books_review (cost=0.00..1425593.38 rows=785658 width=211) (actual
time=74.842..9070.489 rows=668891 loops=3)
Filter: (library_id = 7)
Rows Removed by Filter: 12672873
Planning Time: 0.119 ms
JIT:
Functions: 13
Options: Inlining true, Optimization true, Expressions true, Deforming true
Timing: Generation 2.553 ms, Inlining 239.797 ms, Optimization 76.160 ms, Emission 46.268 ms, Total 364.778 ms
Execution Time: 9561.506 ms
```
:::: {.fragment fragment-index=1}
Lot of information here. What can we notice?
::: {.fragment fragment-index=2}
- Planning time is less than a millisecond
- Execution time is around 10 seconds
- The main operation is a `Parallel Seq Scan on books_review`, i.e. the whole `books_review` table is read, and only the rows matching `(library_id = 7)` are kept. `12666173` rows are filtered out
- Finally, a heapsort is used to get the first 20 rows
:::
::::
## Adding Indexes
```sql
CREATE INDEX review_written_at_idx ON books_review USING btree (written_at);
CREATE INDEX review_rating_idx ON books_review USING btree (rating);
```
```{python}
with use_indexes("review_written_at_idx"):
explain_qs(alexandria_id, "-written_at")
```
```{python}
with use_indexes("review_written_at_idx"):
with perf_counter(time_sql=True):
reviews = list(get_reviews(alexandria_id, "-written_at"))
```
---
```{python}
with use_indexes("review_rating_idx"):
with perf_counter(time_sql=True):
reviews = list(get_reviews(alexandria_id, "rating"))
```
```{python}
with use_indexes("review_rating_idx"):
with perf_counter(time_sql=True):
reviews = list(get_reviews(alexandria_id, "-rating"))
```
<br>
Ordering by `rating DESC` is still quite slow.
Let's try to understand why
::: {.fragment}
```{.python code-line-numbers="|2|4,5"}
Limit (cost=0.44..991.32 rows=20 width=211) (actual time=2422.868..2423.150 rows=20 loops=1)
-> Index Scan Backward using review_rating_idx on books_review (cost=0.44..93419167.88 rows=1885578 width=211)
(actual time=2422.866..2423.145 rows=20 loops=1)
Filter: (library_id = 7)
Rows Removed by Filter: 1819598
Planning Time: 0.404 ms
Execution Time: 2423.218 ms
```
:::
::: {.fragment}
We still have to filter out a large ammount of rows..
:::
## Multicolumn indexes
```sql
CREATE INDEX review_library_id_rating_idx ON public.books_review USING btree (library_id, rating)
```
```{python}
with use_indexes("review_library_id_rating_idx"):
explain_qs(alexandria_id, "-rating")
```
<br>
Much better!
## Back to `id` sorts
Let's create the same kind of index:
```sql
CREATE INDEX review_library_id_pk_idx ON public.books_review USING btree (library_id, id)
```
```{python}
with use_indexes("review_library_id_pk_idx"):
explain_qs(public_id, "-id")
```
<br>
```{python}
with use_indexes("review_library_id_pk_idx"):
explain_qs(alexandria_id, "-id")
```
## Plan can evolve
The plan for our previous_alexandria id (`6`) is
```{python}
explain_qs(previous_alexandria_id, "-id")
```
<br>
However, the plan, even with the `review_library_id_pk_idx` index enabled, used to be
![](../images/previous_plan_by_id.png)
<br>
Nothing has changed betweeen those two executions, except for the internal PG index stats.
::: {.fragment}
As we can see, the actual plan depends on our data, and can be quit unpredictable
:::
## A bad idea: disabling the PK index
The planner keeps using `books_review_pkey`, even though it does not seem like the right choice.
What would happen without this particular index?
```{python}
with use_indexes("review_library_id_pk_idx"), disable_indexes("books_review_pkey"):
explain_qs(previous_alexandria_id, "-id")
```
<br>
::: {.fragment}
However, that seems like a really bad idea, please don't do that!
:::
::: aside
This does not make sense anymore, as the index is now used
:::
## Another idea: conditional indexes
::: {.fragment}
```sql
CREATE UNIQUE INDEX review_pk_alexandria_idx ON books_review USING btree (id) where library_id = 7;
```
:::
::: {.fragment}
This can be a good idea if the the condition is often used. However, the [PostgreSQL doc explicitely says](https://www.postgresql.org/docs/current/indexes-partial.html#INDEXES-PARTIAL-EX4):
> You might be tempted to create a large set of non-overlapping partial indexes (e.g, in our case, with every `library_id`).
>
> This is a bad idea! Almost certainly, you'll be better off with a single non-partial index (declared like our `review_library_id_pk_idx`)
:::
::: {.fragment}
```{python}
with use_indexes("review_pk_alexandria_idx"):
explain_qs(alexandria_id, "-id")
```
:::
## Back to our benchmark
:::: {.columns}
::: {.column width="50%"}
```{python}
#| echo: false
from books.tests.benchmarks import benchmark_list_reviews
with use_indexes(
"review_written_at_idx",
"review_library_id_rating_idx",
"review_library_id_pk_idx",
"review_pk_alexandria_idx",
):
benchmark_list_reviews(
["ordered-list-reviews"], library_ids=[public_id]
)
```
:::
::: {.column width="50%"}
```{python}
#| echo: false
from books.tests.benchmarks import benchmark_list_reviews
with use_indexes(
"review_written_at_idx",
"review_library_id_rating_idx",
"review_library_id_pk_idx",
"review_pk_alexandria_idx",
):
benchmark_list_reviews(
["ordered-list-reviews"], library_ids=[alexandria_id]
)
```
:::
::::
---
## Adding Filters {.scrollable}
:::: {.columns}
::: {.column width="50%"}
```{python}
#| echo: false
from books.tests.benchmarks import benchmark_list_reviews
with use_indexes(
"review_written_at_idx",
"review_library_id_rating_idx",
"review_library_id_pk_idx",
# "review_library_id_pk_rating_written_at_idx",
):
benchmark_list_reviews(["complete-list-reviews"], library_ids=[public_id])
```
:::
::: {.column width="50%"}
```{python}
#| echo: false
from books.tests.benchmarks import benchmark_list_reviews
with use_indexes(
"review_written_at_idx",
"review_library_id_rating_idx",
"review_library_id_pk_idx",
# "review_library_id_pk_rating_written_at_idx",
):
benchmark_list_reviews(["complete-list-reviews"], library_ids=[alexandria_id])
```
:::
::::
## Yet another index {.scrollable}
Once again this changed at some point.
<br>
<br>
:::: {.columns}
::: {.column width="60%"}
![](../images/previous_filters_perf.png)
<!-- ```{python}
#| echo: false
from books.tests.benchmarks import benchmark_list_reviews
with use_indexes(
"review_written_at_idx",
"review_library_id_rating_idx",
"review_library_id_pk_idx",
"review_library_id_pk_rating_written_at_idx",
):
benchmark_list_reviews(["complete-list-reviews"], library_ids=[alexandria_id])
``` -->
:::
::: {.column width="40%"}
By adding yet another multicolumn index, on `(library_id, id, rating, written_at)`
we managed to improve performance on all combinations,
except for the problematic `id` ordering.
:::
::::
# Back to Readers per book
Let's try to add an index to try and improve perf for Alexandria, on the previous endpoint
```{python}
#| echo: false
from django.contrib.postgres.aggregates import ArrayAgg
from utils.sql import use_indexes
def list_readers_per_book(library_id):
return dict(
Book.objects.filter(library=library_id)
.annotate(reader_names=ArrayAgg("readers__name"))
.values_list("title", "reader_names")
)
alexandria_id = 6
```
```{python}
with perf_counter(time_sql=True):
readers_per_book = list_readers_per_book(alexandria_id)
with use_indexes("review_book_reader_idx"), perf_counter(time_sql=True):
readers_per_book = list_readers_per_book(alexandria_id)
```
## Final thoughts
```{python}
#| echo: false
query = """
SELECT
pg_tables.tablename,
pg_size_pretty(pg_relation_size('public'::text || '.' || quote_ident(pg_tables.tablename)::text)) AS table_size,
pg_class.reltuples AS num_rows,
indexname,
pg_size_pretty(pg_relation_size('public'::text || '.' || quote_ident(indexrelname)::text)) AS index_size
FROM
pg_tables
LEFT OUTER JOIN pg_class ON pg_tables.tablename = pg_class.relname
LEFT OUTER JOIN (
SELECT
pg_class.relname AS ctablename,
ipg.relname AS indexname,
indexrelname
FROM
pg_index
JOIN pg_class ON pg_class.oid = pg_index.indrelid
JOIN pg_class ipg ON ipg.oid = pg_index.indexrelid
JOIN pg_indexes ON ipg.relname = pg_indexes.indexname
JOIN pg_stat_all_indexes psai ON pg_index.indexrelid = psai.indexrelid) AS foo ON pg_tables.tablename = foo.ctablename
WHERE
pg_tables.schemaname = 'public'
AND tablename ilike 'books_%'
ORDER BY
pg_relation_size('public'::text || '.' || quote_ident(pg_tables.tablename)::text) desc,
pg_relation_size('public'::text || '.' || quote_ident(indexrelname)::text) desc;
"""
with connection.cursor() as cursor:
cursor.execute(query)
rows = cursor.fetchall()
table = Table()
table.add_column("Table",style="gold1")
table.add_column("Table size", style="bold green")
table.add_column("Rows count", style="cyan")
table.add_column("Index",style="dark_orange3")
table.add_column("Index size", style="bold green")
for row in map(list, rows):
row[2] = f"{int(row[2]):,}"
table.add_row(*map(str, row[:5]))
console.print(table)
```
::: {.incremental}
- The more filters we allow, the more performance issues can arise
- It's not always easy to predict which index will be used
- Indexes are not _always_ the solution, and can take a huge ammount of space
- You need to test on a database as close as possible to your live, production database
:::