aboutsummaryrefslogtreecommitdiff
path: root/14-indexes-and-optimization/indexes-and-optimization.ipynb
blob: 30efe4a665d13ba55d6899337f5746201845f1b8 (about) (plain)
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2b1b1fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import agh_db_lectures\n",
    "agh_db_lectures.prepare_notebook_for_sql()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8ba2bdc",
   "metadata": {},
   "source": [
    "# Indexes and optimization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f301048",
   "metadata": {},
   "outputs": [],
   "source": [
    "%sql postgresql://demo_user:demo_pwd@localhost:25432/agh_it_northwind\n",
    "%config SqlMagic.feedback = False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d285644a",
   "metadata": {},
   "source": [
    "## Drive Access"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b69e064f",
   "metadata": {},
   "source": [
    "Access to **persistent storage** is usually block-based."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5734a9ed",
   "metadata": {},
   "source": [
    "![](magnetic-disk.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e69024e",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "In HDDs, bits of data are stored by polarizing the surface of a disk covered with magnetic material.  Data bits are stored in circular tracks on the surface and tracks are divided into **sectors**.\n",
    "\n",
    "The disk rotates.  Reads and writes of data bits are performed by a head that is mounted on a movable arm.  When a read or write is to be peformed, the arm must first move (to position the head over the right track).  The time the arm needs is called the **seek time**.  The system must then wait (while the disk rotates) until the head is above the right sector of that track.\n",
    "\n",
    "512B sectors are typical for HDDs."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69ebc46f",
   "metadata": {},
   "source": [
    "![](flash-nor-nand.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e0a7bc2",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Flash memories are semiconductor-based and are used in USB thumb drives, SD cards, eMMCs, and SSDs.\n",
    "\n",
    "There are 2 main types of flash memories, NOR and NAND, named after logic gates.  Data in NOR memories can be accessed in a word-based fashion.  \"Word\" in computer science is a unit of memory that might have different size depending on the context but is generally in the order or magnitude of 2 bytes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed9b2960",
   "metadata": {},
   "source": [
    "![](nand-writes.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fd492fe",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Data in NAND memories is accessed in a block-based fashion.  NAND memories are more cost-efficient and dominate the industry.\n",
    "\n",
    "512B - 4096B blocks are typical for SSDs.\n",
    "\n",
    "Note that we often say \"sectors\", \"blocks\", or \"pages\" and mean the same.  Be aware, though, that in some specific context they might have slightly different meanings."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "969ee35e",
   "metadata": {},
   "source": [
    "![](blocks-managed-by-drive.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb1cbcfd",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Both HDDs and SSDs have controllers inside that manage memory blocks.  They can put aside blocks detected as damaged / worn out.\n",
    "\n",
    "Physically close data sectors can be retrieved faster in HDDs (saving on seek time).  Multiple data items can be retrieved/modified faster if stored in the same sector (in case of both HDDs and SSDs)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebc7ffc0",
   "metadata": {},
   "source": [
    "## Records Layout"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5049df1",
   "metadata": {},
   "source": [
    "Records can be stored in a file (or in a page) in different ways."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7431e92e",
   "metadata": {},
   "source": [
    "### Fixed or Variable Size Records"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ec43bc5",
   "metadata": {},
   "source": [
    "![](fixed-size-records.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28a82272",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "The storage overhead can be unnecessarily large due to the padding bytes.\n",
    "\n",
    "What if the padding byte (be it the ASCII space character or the null byte) is allowed to occur in the actual data?\n",
    "\n",
    "Fixed size records are less frequently used in practice."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "babb75c5",
   "metadata": {},
   "source": [
    "![](variable-size-records.svg)\n",
    "\n",
    "_(another, real-life approach is presented in the \"Postgres Page\" subsection)_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1683b321",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "The picture above represents just one possible way of table rows in variable length records."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9ccfdec",
   "metadata": {},
   "source": [
    "### Gaps"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea500cbc",
   "metadata": {},
   "source": [
    "![](record-gaps.svg)\n",
    "\n",
    "_(another, real-life approach is presented in the \"Postgres Page\" section)_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e2cfa0b",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Note that **regardless** of whether a DBMS uses fixed or variable size records, it has to handle row deletions.  A likely approach is one that leaves free space (\"gaps\") after deleted records.\n",
    "\n",
    "Presence of gaps would make it impossible to process records in a file sequentially!  As a solution, each record could include a pointer to the next non-deleted record in the file.  This would enable sequential (**forward only**) processing of records."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52a27e4d",
   "metadata": {},
   "source": [
    "## Data Layout in Postgres"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9c778c1",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Postgres — 1GB (by default) segment files.  Internally organized as 8 KB (by default) pages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c7c6e01",
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls /var/lib/postgresql/data/base"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc632690",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%sql\n",
    "SELECT oid, datname FROM pg_database"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db52527c",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Each Postgres database has its own filesystem directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a1c2231",
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls /var/lib/postgresql/data/base/16385"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a2f133d",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Put the oid of the Northwind database in the command above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3271f479",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%sql\n",
    "SELECT oid, relname, relfilenode FROM pg_class WHERE relname NOT LIKE 'pg_%'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f295978f",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Each table and index is in a separate file.\n",
    "\n",
    "`_fsm` files store \"free space maps\" of tables (binary tree tracking free space in each page, one byte per page in leaf nodes).\n",
    "\n",
    "`_vm` files store \"visibility maps\" of tables (tacking of pages with no dead tuples, i.e., pages that are visible to all active transactions)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20fb17e3",
   "metadata": {},
   "source": [
    "![](postgres-page.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19274ce0",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Rows organized in a block as depicted.\n",
    "\n",
    "What if a row would be crossing block boundary?  In particular, what about big objects (CLOB, BLOB)?\n",
    "\n",
    "Postgres uses special \"TOAST\" tables for out-of-line storing of big values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "559e7c27",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%sql\n",
    "-- COMMIT;\n",
    "VACUUM FULL;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a16cbed6",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Postgres does not automatically reorder records within a page to eliminate gaps.  Neither does it exploit every possibility of inserting a new record within a gap.  The database administrator can request immediate repacking of records with the `VACUUM` command."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d335963e",
   "metadata": {},
   "source": [
    "More: https://www.postgresql.org/docs/current/storage.html"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cff2ae1",
   "metadata": {},
   "source": [
    "## Index types"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a12990a",
   "metadata": {},
   "source": [
    "Data structures.\n",
    "\n",
    "- B-Tree\n",
    "- Hash\n",
    "- GiST Indexes\n",
    "- others…\n",
    "\n",
    "More: https://www.postgresql.org/docs/current/indexes-types.html"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c93e9887",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Indexes **might** speed up some queries and typically slow down data modifications (need to update the index as well as the data).\n",
    "\n",
    "Postgres is famous for its \"Generalized Search Tree\" indexes.  They are useful for querying geospatial data, e.g., to find points that lie within a rectangle."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b004e03",
   "metadata": {},
   "source": [
    "## Sequential File Scan"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a003d19",
   "metadata": {},
   "source": [
    "![](no-index.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc8748ae",
   "metadata": {},
   "source": [
    "$\\sigma_{name > Monocled}(users)$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72b5779f",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Consider a relation with users data.  The basic operation that can be performed, regardless of the presence of idexes, is a **file scan**.  It boils down to reading all record pages of the relation one by one.\n",
    "\n",
    "The cost of a file scan is easy to estimate based on the size of the relation's data."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eeb79e63",
   "metadata": {},
   "source": [
    "## Clustered Index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24a3a575",
   "metadata": {},
   "source": [
    "Also called a **primary index**."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0a9e08a",
   "metadata": {},
   "source": [
    "![](dense-clustering-index.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc2f5ca6",
   "metadata": {},
   "source": [
    "$\\sigma_{name > Monocled}(users)$\n",
    "\n",
    "$\\sigma_{name < Monocled}(users)$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2986635b",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Clustered index is one whose ordering matches the physical ordering of the data.\n",
    "\n",
    "A clustered index could be looked up once to speed up a \"greater than\" selection using its indexed attribute(s).  The file would then be scanned sequentially from that point to the end to retrieve the records.  For a B-tree index, we can estimate the maximum cost of index lookup as $n$ times the page read cost, where $n$ is the height of the B-tree.  In practice, the cost is likely to be slightly lower, due to the tree root being already cached.\n",
    "\n",
    "A clustered index would not be used with a \"less than\" selection on its attribute(s).  Such selection would be performed with a file scan starting from the beginning of the file.  The operation would terminate once the first record outside the desired range is read.\n",
    "\n",
    "The difference in index usefulness in \"less than\" and \"greater than\" selections results from the assumption that file can only be scanned forward (which might be the case if records only contain pointers to subsequent records)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9785898f",
   "metadata": {},
   "source": [
    "## Non-Clustered Index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7129e6c",
   "metadata": {},
   "source": [
    "Also called a **secondary index**."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2370e9a2",
   "metadata": {},
   "source": [
    "![](2-dense-indexes.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74bba5d4",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "A non-clustered index uses an ordering different from the physical ordering of the records.\n",
    "\n",
    "At any point in time there can exist at most one clustered index on a table.  Multiple non-clustered indexes can exist simultaneously.\n",
    "\n",
    "A clustered index does **not have to** be made on the primary key attributes.  It could be (and it might be the most frequent practice), but the physical ordering of the records **can also match the ordering of some other attribute(s) or none at all**."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f41b11e",
   "metadata": {},
   "source": [
    "## Sparse Index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97c261d7",
   "metadata": {},
   "source": [
    "![](sparse-and-dense-index.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bad783e",
   "metadata": {},
   "source": [
    "$\\sigma_{name=Rantoo}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62762684",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "A clustered index can be sparse, e.g., only point to a subset of the records.\n",
    "\n",
    "The remaining records can be located by finding the closest record present in the sparse index and then scanning the file forward until the desired record is found.\n",
    "\n",
    "A non-clustered index has to be dense.  Imagine that we wanted to make our non-clustered index on `registration_date` sparse.  If we had removed any entry from it, it would not have been possible to easily locate it in the file using the index.  A sequential file scan would have been required, defeating the purpose of an index."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38dfc196",
   "metadata": {},
   "source": [
    "## Selection Speedup With a Non-Clustering Index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88c735a8",
   "metadata": {},
   "source": [
    "![](secondary-index-pages.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "272b6b4f",
   "metadata": {},
   "source": [
    "$\\sigma_{registration\\_date = 2024-04-15}$\n",
    "\n",
    "$\\sigma_{registration\\_date \\geq 2024-10-01}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53991721",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Consider a selection that should retrieve a single or a handful of records.  It can leverage a secondary index to locate these records and only read the necessary data pages.\n",
    "\n",
    "Now, consider a selection that should retrieve half of the rows of some table.  It might seem that a secondary index on the selection's attribute can be used to speed up this operation.  But it might be the case that almost every page of the relation file does contain at least one of the desired rows.  If almost all pages need to be read, then a sequential file scan is going to be just as fast as indexed lookup.\n",
    "\n",
    "Indexed lookup could be even worse if we were to retrieve the records in the order of the index.  We would need to read page 4 for the first record, page 5 for the second, page 6 for the third and page 1 for the fourth…  But later on we would need another record from page 4.  In case it already got evicted from the cache, we would need to read it again, thus unoptimally performing more page reads than necessary.\n",
    "\n",
    "If we consider HDD drives (where additional time is required to seek to a particular page), such use of the index is even less optimal."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cfc0e6a",
   "metadata": {},
   "source": [
    "### Page Bitmap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5dae613",
   "metadata": {},
   "source": [
    "|page  | bitmap |\n",
    "|------|--------|\n",
    "|page 1| 1      |\n",
    "|page 2| 1      |\n",
    "|page 3| 0      |\n",
    "|page 4| 1      |\n",
    "|page 5| 1      |\n",
    "|page 6| 1      |\n",
    "|page 7| 1      |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b0ec7b0",
   "metadata": {},
   "source": [
    "To counter that, a database system might first use the index to compute a bitmap of the pages that need to be accessed.  Then, it can access the pages in the optimal order, without repeated reads of the same page.\n",
    "\n",
    "Since only 1 bit has to be stored for each page, the bitmap is likely to fit in the operational memory of the computer."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87c771a0",
   "metadata": {},
   "source": [
    "## A Note on Record Pointers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3e07004",
   "metadata": {},
   "source": [
    "<strike>Postgres index --> record</strike>\n",
    "\n",
    "Postgres index --> **page** of the record"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "943585f0",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Indexes in Postgres (and likely many other DBMSes) only have pointers to pages.  Records inside a page can be rearranged without affecting the indexes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d546f84",
   "metadata": {},
   "source": [
    "## Use of Data Statistics"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1debb1fb",
   "metadata": {},
   "source": [
    "![](statistics-to-planning.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc2b3ebb",
   "metadata": {},
   "source": [
    "```sql\n",
    "-- Show plan.\n",
    "EXPLAIN SELECT * FROM users WHERE registration_date >= '2025-10-01';\n",
    "```\n",
    "\n",
    "```sql\n",
    "-- Show plan and perform.\n",
    "EXPLAIN ANALYZE SELECT * FROM users WHERE registration_date >= '2025-10-01';\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d808e2de",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "The DBMS gathers statistics about data during normal operation (queries execution).  It uses them to estimate the cost of different applicable query execution plans (say, it tries to answer \"How likely is it that this query will require reading over 95% pages in the relation's file?\").\n",
    "\n",
    "`EXPLAIN` is a common, non-standard SQL commad that presents the query execution plan that the DBMS would use for a query (we'll soon see some sample plans that `EXPLAIN` can produce).\n",
    "\n",
    "Under Postgres, the `ANALYZE` keyword can be used to have the DBMS additionally execute the query and show both the estimated and real execution costs.\n",
    "\n",
    "Postgres' `ANALYZE` can cause statitics to be updated.  This means running `EXPLAIN ANALYZE` twice in a row can yield different results."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b553ca23",
   "metadata": {},
   "source": [
    "## Nested-Loop Join Algorithm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e33145b1",
   "metadata": {},
   "source": [
    "$x \\bowtie_\\Theta y$\n",
    "\n",
    "```\n",
    "for each record block b_x in x:\n",
    "    for each record block b_y in y:\n",
    "        for each record r_x in b_x:\n",
    "            for each record r_y in b_y:\n",
    "                if theta(r_x, r_y) is true:\n",
    "                    yield {r_x, r_y};\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b71a2152",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "We shall use pseudocode to explain some of the algorithms.\n",
    "\n",
    "A generic join algorithm that can be used regardless of the presence of indexes.\n",
    "\n",
    "Has an **outer** and **inner** loop processing relations' data.  The 2 additional loops inside our \"inner\" loop are of little interest to us now.\n",
    "\n",
    "If each relations' data is too large to fit in the available RAM, the inner relation's data shall be read multiple times.\n",
    "\n",
    "It is better to use smaller relation in the inner loop.\n",
    "\n",
    "The loop that reads blocks of the inner relation can process the each time in a different order, to better utilize the LRU caching of blocks.\n",
    "\n",
    "If our theta join is in fact an equi-join, an inner relation's index on join's attribute(s) can be used.\n",
    "\n",
    "If the inner relation can be kept entirely in memory, no block needs to be read twice."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e26239c",
   "metadata": {},
   "source": [
    "## External Sort-Merge Algorithm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d93ef94e",
   "metadata": {},
   "source": [
    "Also called \"N-way merge\"."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fdd354d",
   "metadata": {},
   "source": [
    "```\n",
    "N := 1;\n",
    "\n",
    "while there are relation blocks left to read:\n",
    "    read as many record blocks as can be sorted in memory;\n",
    "    sort records in memory;\n",
    "    write records to run file nr N;\n",
    "    N := N + 1;\n",
    "\n",
    "current_blocks[N];\n",
    "\n",
    "for I from 1 to N:\n",
    "    current_blocks[I] := read the first block of run file I;\n",
    "\n",
    "while there are any records in current_blocks:\n",
    "    I := index of the block with smallest record;\n",
    "    yield the record from current_blocks[I];\n",
    "    remove the record from current_blocks[I];\n",
    "    if current_blocks[I] is empty:\n",
    "        if there are more blocks in run file I:\n",
    "            current_blocks[I] := read a block from run file I;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b103d2f4",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Sorting of data that fits in RAM is easy.\n",
    "\n",
    "Data that does not fit in RAM can be sorted by storing intermediate results in temporary files (\"run files\").\n",
    "\n",
    "If we have to little memory to hold N record blocks at once, we can perform the N-way merge sort in parts — first soft the data from some M run files to a new run file, then sort the data from the next M run files, etc.  The M would be chosen so that M blocks can be loaded to memory at once.\n",
    "\n",
    "Query optimizer would account for the costs of reads and writes on run files.\n",
    "\n",
    "In general, sorting is expensive."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dd80350",
   "metadata": {},
   "source": [
    "## Equi-Join Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d76ea70",
   "metadata": {},
   "source": [
    "$x \\bowtie_{x.a = y.a} y$\n",
    "\n",
    "$x \\bowtie y \\equiv \\Pi_{attributes\\ with\\ a\\ deduplicated}(x \\bowtie_{x.a = y.a} y)$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cb605c2",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "A faster algorithm than the nested loop join may exist if the join condition is the equality of relation's respective attribute values.\n",
    "\n",
    "Note that a natural join is just an equi-join with a projection."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc4e1c12",
   "metadata": {},
   "source": [
    "### Merge-Join Algorithm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36a9b803",
   "metadata": {},
   "source": [
    "```\n",
    "sort both relations on the `a' attribute;\n",
    "\n",
    "S_x := read the initial records from x with the same value of `a' attribute;\n",
    "S_y := read the initial records from y with the same value of `a' attribute;\n",
    "\n",
    "loop:\n",
    "    if S_x.a = S_y.a:\n",
    "        yield all pairs of combined S_x and S_y;\n",
    "        if there are no more records in x or y:\n",
    "            break;\n",
    "        S_x := read subsequent records from x with the same value of `a' attribute;\n",
    "        S_y:= read subsequent records from y with the same value of `a' attribute;\n",
    "    else if S_x.a < S_y.a:\n",
    "        if there are no more records in x:\n",
    "            break;\n",
    "        S_x := read subsequent records from x with the same value of `a' attribute;\n",
    "    else if S_y.a < S_x.a:\n",
    "        if there are no more records in y:\n",
    "            break;\n",
    "        S_y:= read subsequent records from y with the same value of `a' attribute;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ee4bd5a",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "For larger relations avoids the need to read one of them multiple times.\n",
    "\n",
    "Incurs the overhead of sorting its inputs, unless they are already appropriately sorted.\n",
    "\n",
    "Instead of operating on sorted records, the DBMS can operate on sorted leaf nodes of B-tree indexes.  Additional sorting of the intermediate result on record addresses shall then be needed to access the actual records in their physical order."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efc82a9b",
   "metadata": {},
   "source": [
    "## Hash Join Algorithm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cb44fb0",
   "metadata": {},
   "source": [
    "```\n",
    "N := number of blocks that fit in memory;\n",
    "N := N - 1;\n",
    "\n",
    "h := hash function that returns a number from 1 to N;\n",
    "\n",
    "buckets[N];\n",
    "\n",
    "for each record r_x in x:\n",
    "    I := h(r_x.a);\n",
    "    put r_x in buckets[I];\n",
    "    if buckets[I] is a full block:\n",
    "        write buckets[I] to run file x_I;\n",
    "        empty buckets[I];\n",
    "write the remaining partially-filled buckets to run files x_*;\n",
    "\n",
    "empty buckets[*];\n",
    "\n",
    "for each record r_y in y:\n",
    "    I := h(r_y.a);\n",
    "    put r_y in buckets[I];\n",
    "    if buckets[I] is a full block:\n",
    "        write buckets[I] to run file y_I;\n",
    "        empty buckets[I];\n",
    "write the remaining partially-filled buckets to run files y_*;\n",
    "\n",
    "delete buckets;\n",
    "\n",
    "procedure hash_join_in_memory(partition_file_x, partition_file_y):\n",
    "    f := a suitable hash function (different from h above);\n",
    "    Idx := in-memory hash index using f;\n",
    "    for each record r_x in partition_file_x:\n",
    "        add r_x to Idx;\n",
    "    for each record r_y in partition_file_y:\n",
    "        for each record r_x in Idx[r_y.a]:\n",
    "            if r_x.a = r_y.a:\n",
    "                yield {r_x, r_y};\n",
    "        \n",
    "\n",
    "for I from 1 to N:\n",
    "    yield the output of hash_join_in_memory(x_I, y_I);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16228532",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "The first hashing has its buckets written to disk.  The second hashing is performed on each bucket's records and is done in memory.\n",
    "\n",
    "If buckets produced by the first hashing are too large to be processed in memory, the algorithm can be extended to recursively partition them recursively, with results written to disk, until the partitions are small enough.\n",
    "\n",
    "In many cases it shall of course be possible to perform the entire hash-join in-memory, without the need for the algorithm part that writes records to run files.\n",
    "\n",
    "We built the index `Idx` on partitions of records from `x`.  `x` is therefore called the **build input** and `y` is the **probe input**."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e0cdf96",
   "metadata": {},
   "source": [
    "## Operations With Complex Conditions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e84491d",
   "metadata": {},
   "source": [
    "### Conjunction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19146378",
   "metadata": {},
   "source": [
    "$\\sigma_{\\Theta_1 \\land \\Theta_2 \\land \\Theta_3}(x)$\n",
    "\n",
    "If, e.g., an index $l$ is available for the $\\Theta_2$ condition:\n",
    "\n",
    "![](conjunction.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d6dca36",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Otherwise, a sequential scan can be used.\n",
    "\n",
    "If many subconditions have compatible indexes, the DBMS can use statistics about data to determine which one to use (which one should return the smallest number of rows).\n",
    "\n",
    "Alternatively, the DBMS can compute a bit map of pages to access based on multiple indexes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b2c1586",
   "metadata": {},
   "source": [
    "### Disjunction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a6a03dd",
   "metadata": {},
   "source": [
    "$\\sigma_{\\Theta_1 \\lor \\Theta_2 \\lor \\Theta_3}(x)$\n",
    "\n",
    "If indexes $l_1$, $l_2$, and $l_3$ are available for the $\\Theta_1$, $\\Theta_2$, and $\\Theta_3$ conditions:\n",
    "\n",
    "![](disjunction.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1f7cb20",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "If the index for at least one subconditions is missing, sequential file scan becomes necessary.\n",
    "\n",
    "Set operations are also implemented with mergesort-like algoritms (hence can be expensive)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5e06774",
   "metadata": {},
   "source": [
    "## Optimization With \"EXPLAIN\" Command"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33957cf3",
   "metadata": {},
   "source": [
    "**Nonstandard**, likely to differ even between versions of the same DBMS."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94239550",
   "metadata": {},
   "source": [
    "### MySQL Format\n",
    "\n",
    "From: https://dev.mysql.com/doc/refman/8.4/en/explain-output.html\n",
    "\n",
    "```sql\n",
    "EXPLAIN SELECT tt.TicketNumber, tt.TimeIn,\n",
    "               tt.ProjectReference, tt.EstimatedShipDate,\n",
    "               tt.ActualShipDate, tt.ClientID,\n",
    "               tt.ServiceCodes, tt.RepetitiveID,\n",
    "               tt.CurrentProcess, tt.CurrentDPPerson,\n",
    "               tt.RecordVolume, tt.DPPrinted, et.COUNTRY,\n",
    "               et_1.COUNTRY, do.CUSTNAME\n",
    "        FROM tt, et, et AS et_1, do\n",
    "        WHERE tt.SubmitTime IS NULL\n",
    "          AND tt.ActualPC = et.EMPLOYID\n",
    "          AND tt.AssignedPC = et_1.EMPLOYID\n",
    "          AND tt.ClientID = do.CUSTNMBR;\n",
    "```\n",
    "\n",
    "May produce (in presence of appropriate indexes):\n",
    "\n",
    "```\n",
    "table type   possible_keys key      key_len ref           rows Extra\n",
    "et    ALL    PRIMARY       NULL     NULL    NULL          74\n",
    "tt    ref    AssignedPC,   ActualPC 15      et.EMPLOYID   52   Using\n",
    "             ClientID,                                         where\n",
    "             ActualPC\n",
    "et_1  eq_ref PRIMARY       PRIMARY  15      tt.AssignedPC 1\n",
    "do    eq_ref PRIMARY       PRIMARY  15      tt.ClientID   1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "797cdb25",
   "metadata": {},
   "source": [
    "## Postgres Format"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb71286b",
   "metadata": {},
   "source": [
    "From: https://thoughtbot.com/blog/reading-an-explain-analyze-query-plan\n",
    "\n",
    "```sql\n",
    "EXPLAIN SELECT *\n",
    "FROM tenk1 t1, tenk2 t2\n",
    "WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2\n",
    "ORDER BY t1.fivethous;\n",
    "```\n",
    "\n",
    "May produce (in presence of appropriate indexes):\n",
    "\n",
    "\n",
    "```\n",
    "                                           QUERY PLAN\n",
    "-------------------------------------------------------------------------------------------------\n",
    " Sort  (cost=717.34..717.59 rows=101 width=488)\n",
    "   Sort Key: t1.fivethous\n",
    "   Sort Method: quicksort  Memory: 77kB\n",
    "   ->  Hash Join  (cost=230.47..713.98 rows=101 width=488)\n",
    "         Hash Cond: (t2.unique2 = t1.unique2)\n",
    "         ->  Seq Scan on tenk2 t2  (cost=0.00..445.00 rows=10000 width=244)\n",
    "         ->  Hash  (cost=229.20..229.20 rows=101 width=244)\n",
    "               Buckets: 1024  Batches: 1  Memory Usage: 28kB\n",
    "               ->  Bitmap Heap Scan on tenk1 t1  (cost=5.07..229.20 rows=101 width=244)\n",
    "                     Recheck Cond: (unique1 < 100)\n",
    "                     ->  Bitmap Index Scan on tenk1_unique1  (cost=0.00..5.04 rows=101 width=0)\n",
    "                           Index Cond: (unique1 < 100)\n",
    " Planning time: 0.194 ms\n",
    " Execution time: 8.008 ms\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9279e419",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "By adding `ANALYZE` after `EXPLAIN` we can additionally get information about the time really spend on query execution."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80c891ec",
   "metadata": {},
   "source": [
    "## Index Creation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d059899b",
   "metadata": {},
   "source": [
    "```sql\n",
    "-- Postgres\n",
    "CREATE INDEX my_index ON some_table (some_colum);\n",
    "\n",
    "CREATE UNIQUE INDEX my_index\n",
    "ON some_table USING BTREE\n",
    "(some_colum * other_column);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "745c4b8c",
   "metadata": {},
   "source": [
    "```sql\n",
    "-- MySQL / MariaDB\n",
    "CREATE INDEX my_index ON some_table (some_colum);\n",
    "\n",
    "CREATE UNIQUE INDEX my_index\n",
    "ON some_table (some_colum * other_column, third_column)\n",
    "USING BTREE;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3f0dd85",
   "metadata": {},
   "source": [
    "### Automatic Index Creation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b05d60c",
   "metadata": {},
   "source": [
    "`UNIQUE` and `PRIMARY KEY` constraints."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa232426",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Databases typically automatically create indexes needed to verify the `UNIQUE` and `PRIMARY KEY` constraints.\n",
    "\n",
    "Under default configuration, MySQL and MariaDB make the primary key index (if present) a clustering index.\n",
    "\n",
    "In Postgres there are **no clustered indexes**.\n",
    "\n",
    "Postgres does have a `CLUSTER` command that orders table's data according to some index, but **it does not do what you'd expect**.  It's a one-time operation and the order is not maintained afterwards."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "/gnu/store/q35bxk1i5blx0rcgxphhrq9xqmgha9bz-python-3.11.11/bin/python3",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}