-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
trading
1549 lines (1513 loc) · 80.4 KB
/
trading
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
# =============================================================================
# This schema is based on a business buying/selling online trading cards. This
# file simulates queries taking place while schema changes are *not* taking
# place. See the trading-mutation file for the same queries running against
# tables with simulated schema changes in progress.
# =============================================================================
# --------------------------------------------------
# Schema Definitions
# --------------------------------------------------
# Cards is the catalog of all cards that can be traded.
exec-ddl
CREATE TABLE Cards
(
id INT NOT NULL,
name VARCHAR(128) NOT NULL,
rarity VARCHAR(1) NULL,
setname VARCHAR(5) NULL,
number INT NOT NULL,
isfoil BIT NOT NULL,
CONSTRAINT CardsPrimaryKey PRIMARY KEY
(
id ASC
),
CONSTRAINT CardsNameSetNumber UNIQUE
(
name ASC,
setname ASC,
number ASC
)
)
----
exec-ddl
ALTER TABLE Cards INJECT STATISTICS '[
{
"columns": ["id"],
"distinct_count": 57000,
"null_count": 0,
"row_count": 57000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["name"],
"distinct_count": 39000,
"null_count": 0,
"row_count": 57000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["setname"],
"distinct_count": 162,
"null_count": 0,
"row_count": 57000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["number"],
"distinct_count": 829,
"null_count": 0,
"row_count": 57000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["name", "setname"],
"distinct_count": 56700,
"null_count": 0,
"row_count": 57000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["name", "setname", "number"],
"distinct_count": 57000,
"null_count": 0,
"row_count": 57000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
}
]'
----
# CardsInfo tracks current inventory of each card, as well as current buy/sell
# price. It is partitioned on dealerid, which represents multiple licensees
# (dealers) using the trading software.
exec-ddl
CREATE TABLE CardsInfo
(
dealerid OID NOT NULL,
cardid INT NOT NULL,
buyprice DECIMAL(10,4) NOT NULL,
sellprice DECIMAL(10,4) NOT NULL,
discount DECIMAL(10,4) NOT NULL,
desiredinventory INT NOT NULL,
actualinventory INT NOT NULL,
maxinventory INT NOT NULL,
version DECIMAL NOT NULL DEFAULT (cluster_logical_timestamp()),
CONSTRAINT CardsInfoPrimaryKey PRIMARY KEY
(
dealerid ASC,
cardid ASC
),
CONSTRAINT CardsInfoCardIdKey FOREIGN KEY (cardid)
REFERENCES Cards (id),
UNIQUE INDEX CardsInfoVersionIndex (dealerid ASC, version ASC)
)
----
exec-ddl
ALTER TABLE CardsInfo INJECT STATISTICS '[
{
"columns": ["dealerid"],
"distinct_count": 12,
"null_count": 0,
"row_count": 700000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["cardid"],
"distinct_count": 57000,
"null_count": 0,
"row_count": 700000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["version"],
"distinct_count": 700000,
"null_count": 0,
"row_count": 700000,
"created_at": "2020-01-01 0:00:00.00000+00:00",
"histo_col_type": "decimal",
"histo_buckets": [
{"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "1426741777604892000"},
{"num_eq": 0, "num_range": 350000, "distinct_range": 350000, "upper_bound": "1505581778104892000"},
{"num_eq": 0, "num_range": 350000, "distinct_range": 350000, "upper_bound": "1584421778604892000"}
]
},
{
"columns": ["dealerid", "cardid"],
"distinct_count": 700000,
"null_count": 0,
"row_count": 700000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "version"],
"distinct_count": 700000,
"null_count": 0,
"row_count": 700000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
}
]'
----
# InventoryDetails stores the quantity and location of all the cards.
exec-ddl
CREATE TABLE InventoryDetails
(
dealerid OID NOT NULL,
cardid INT NOT NULL,
accountname VARCHAR(128) NOT NULL,
quantity INT NOT NULL,
version DECIMAL NOT NULL DEFAULT (cluster_logical_timestamp()),
CONSTRAINT InventoryDetailsPrimaryKey PRIMARY KEY
(
dealerid ASC,
cardid ASC,
accountname ASC
),
CONSTRAINT InventoryDetailsCardIdKey FOREIGN KEY (cardid)
REFERENCES Cards (id)
)
----
exec-ddl
ALTER TABLE InventoryDetails INJECT STATISTICS '[
{
"columns": ["dealerid"],
"distinct_count": 12,
"null_count": 0,
"row_count": 1700000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["cardid"],
"distinct_count": 50000,
"null_count": 0,
"row_count": 1700000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["accountname"],
"distinct_count": 150,
"null_count": 0,
"row_count": 1700000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "cardid"],
"distinct_count": 250000,
"null_count": 0,
"row_count": 1700000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "cardid", "accountname"],
"distinct_count": 170000,
"null_count": 0,
"row_count": 1700000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
}
]'
----
# Transactions records all buy/sell trading activity.
#
# NOTE: The TransactionsOpIndex is meant to be a partial index, only containing
# non-NULL values. The operationid column is for idempotency, and
# older values get set to NULL after X hours to save space. 99%+ of values
# are NULL.
exec-ddl
CREATE TABLE Transactions
(
dealerid OID NOT NULL,
isbuy BOOL NOT NULL,
date TIMESTAMPTZ NOT NULL,
accountname VARCHAR(128) NOT NULL,
customername VARCHAR(128) NOT NULL,
operationid UUID,
version DECIMAL NOT NULL DEFAULT (cluster_logical_timestamp()),
CONSTRAINT TransactionsPrimaryKey PRIMARY KEY
(
dealerid ASC,
isbuy ASC,
date ASC
),
UNIQUE INDEX TransactionsOpIndex (dealerid ASC, operationid ASC)
--WHERE operationid IS NOT NULL
)
----
exec-ddl
ALTER TABLE Transactions INJECT STATISTICS '[
{
"columns": ["dealerid"],
"distinct_count": 10,
"null_count": 0,
"row_count": 20000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["isbuy"],
"distinct_count": 2,
"null_count": 0,
"row_count": 20000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["date"],
"distinct_count": 20000000,
"null_count": 0,
"row_count": 20000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["operationid"],
"distinct_count": 4000,
"null_count": 19996000,
"row_count": 20000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "isbuy"],
"distinct_count": 15,
"null_count": 0,
"row_count": 20000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "isbuy", "date"],
"distinct_count": 20000000,
"null_count": 0,
"row_count": 20000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
}
]'
----
# TransactionDetails records line items of each Transaction.
exec-ddl
CREATE TABLE TransactionDetails
(
dealerid OID NOT NULL,
isbuy BOOL NOT NULL,
transactiondate TIMESTAMPTZ NOT NULL,
cardid INT NOT NULL,
quantity INT NOT NULL,
sellprice DECIMAL(10,4) NOT NULL,
buyprice DECIMAL(10,4) NOT NULL,
version DECIMAL NOT NULL DEFAULT (cluster_logical_timestamp()),
CONSTRAINT DetailsPrimaryKey PRIMARY KEY
(
dealerid ASC,
isbuy ASC,
transactiondate ASC,
cardid ASC,
quantity ASC
),
CONSTRAINT DetailsDealerDateKey FOREIGN KEY (dealerid, isbuy, transactiondate)
REFERENCES Transactions (dealerid, isbuy, date),
CONSTRAINT DetailsCardIdKey FOREIGN KEY (cardid)
REFERENCES Cards (id),
INDEX DetailsCardIdIndex (dealerid ASC, isbuy ASC, cardid ASC)
)
----
exec-ddl
ALTER TABLE TransactionDetails INJECT STATISTICS '[
{
"columns": ["dealerid"],
"distinct_count": 10,
"null_count": 0,
"row_count": 180000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["isbuy"],
"distinct_count": 2,
"null_count": 0,
"row_count": 180000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["transactiondate"],
"distinct_count": 180000000,
"null_count": 0,
"row_count": 180000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["cardid"],
"distinct_count": 57000,
"null_count": 0,
"row_count": 180000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "isbuy"],
"distinct_count": 15,
"null_count": 0,
"row_count": 180000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "isbuy", "transactiondate"],
"distinct_count": 20000000,
"null_count": 0,
"row_count": 180000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "isbuy", "transactiondate", "cardid"],
"distinct_count": 180000000,
"null_count": 0,
"row_count": 180000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "isbuy", "transactiondate", "cardid", "quantity"],
"distinct_count": 180000000,
"null_count": 0,
"row_count": 180000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "isbuy", "cardid"],
"distinct_count": 350000,
"null_count": 0,
"row_count": 180000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
}
]'
----
# PriceDetails records price history for each card.
exec-ddl
CREATE TABLE PriceDetails
(
dealerid OID NOT NULL,
cardid INT NOT NULL,
pricedate TIMESTAMPTZ NOT NULL,
pricedby VARCHAR(128) NOT NULL,
buyprice DECIMAL(10,4) NOT NULL,
sellprice DECIMAL(10,4) NOT NULL,
discount DECIMAL(10,4) NOT NULL,
version DECIMAL NOT NULL,
CONSTRAINT PriceDetailsPrimaryKey PRIMARY KEY
(
dealerid ASC,
cardid ASC,
pricedate ASC
),
CONSTRAINT PriceDetailsCardIdKey FOREIGN KEY (cardid)
REFERENCES Cards (id)
)
----
exec-ddl
ALTER TABLE PriceDetails INJECT STATISTICS '[
{
"columns": ["dealerid"],
"distinct_count": 2,
"null_count": 0,
"row_count": 40000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["cardid"],
"distinct_count": 57000,
"null_count": 0,
"row_count": 40000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["pricedate"],
"distinct_count": 40000000,
"null_count": 0,
"row_count": 40000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "cardid"],
"distinct_count": 90000,
"null_count": 0,
"row_count": 40000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
},
{
"columns": ["dealerid", "cardid", "pricedate"],
"distinct_count": 40000000,
"null_count": 0,
"row_count": 40000000,
"created_at": "2020-01-01 0:00:00.00000+00:00"
}
]'
----
# NOTE: These views should not be checking for the constant dealerid = 1.
# Instead, the dealerid should be derived from the current user, like this:
#
# dealerid = (SELECT oid FROM pg_roles WHERE rolname = current_user);
#
# However, the optimizer and execution engine are not smart enough to do a good
# job with this. The following needs to be done:
#
# 1. Optimizer needs access to key information about pg_roles so that it can
# infer that there will be at most one row that matches the predicate.
# 2. Optimizer needs to replace "current_user" with constant after the query
# is prepared, as if it were a parameter.
# 3. Execution engine should allow "push down" into virtual tables, or else
# this query will need to enumerate every user and role in order to find
# the requested rolname.
#
exec-ddl
CREATE VIEW CardsView AS
SELECT id AS Id, name AS Name, rarity AS Rarity, setname AS SetName, number AS Number, isfoil AS IsFoil,
buyprice AS BuyPrice, sellprice AS SellPrice, discount AS Discount,
desiredinventory AS DesiredInventory, actualinventory AS ActualInventory,
maxinventory AS MaxInventory, version AS Version
FROM Cards
JOIN CardsInfo
ON id = cardid
WHERE dealerid = 1
----
exec-ddl
CREATE VIEW TransactionsView AS
SELECT
date AS Date, accountname AS AccountName, customername AS CustomerName,
isbuy AS IsBuy, operationid AS OperationId
FROM Transactions
WHERE dealerid = 1
----
exec-ddl
CREATE VIEW TransactionDetailsView AS
SELECT isbuy AS IsBuy, transactiondate AS TransactionDate, cardid AS CardId, quantity AS Quantity,
sellprice AS SellPrice, buyprice AS BuyPrice
FROM TransactionDetails
WHERE dealerid = 1
----
exec-ddl
CREATE VIEW PriceDetailsView AS
SELECT cardid AS CardId, pricedate AS PriceDate, pricedby AS PricedBy, buyprice AS BuyPrice,
sellprice AS SellPrice,
(
CASE
WHEN dealerid <> 1
THEN 0::DECIMAL(10,4)
ELSE discount
END
) AS Discount
FROM PriceDetails
WHERE dealerid = 1
----
exec-ddl
CREATE VIEW GlobalInventoryView AS
SELECT cardid, min(buyprice) AS BuyPrice, max(sellprice) AS SellPrice, max(discount) AS Discount,
sum(desiredinventory) AS DesiredInventory,
sum
(
CASE
WHEN dealerid = 2 AND actualinventory > 24 THEN 24
WHEN dealerid <> 1 AND actualinventory > maxinventory THEN maxinventory
ELSE actualinventory
END
) AS ActualInventory,
sum(maxinventory) AS MaxInventory,
max(version) AS Version
FROM CardsInfo
INNER JOIN Cards
ON cardid = id
WHERE (dealerid = 1 OR dealerid = 2 OR dealerid = 3 OR dealerid = 4)
GROUP BY cardid
----
exec-ddl
CREATE VIEW GlobalCardsView AS
SELECT c.id AS Id, c.name AS Name, c.rarity AS Rarity, c.setname AS SetName, c.number AS Number, c.isfoil AS IsFoil,
inv.BuyPrice, inv.SellPrice, inv.Discount, inv.DesiredInventory, inv.ActualInventory,
inv.MaxInventory, inv.Version
FROM Cards c
INNER JOIN GlobalInventoryView inv
ON c.id = inv.cardid
----
# --------------------------------------------------
# SELECT Queries
# --------------------------------------------------
# Find all cards that have been modified in the last 5 seconds.
opt format=show-stats
SELECT
Id, Name, Rarity, SetName, Number, IsFoil, BuyPrice, SellPrice,
DesiredInventory, ActualInventory, Version, Discount, MaxInventory
FROM CardsView WHERE Version > 1584421773604892000.0000000000
----
project
├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null buyprice:10!null sellprice:11!null desiredinventory:13!null actualinventory:14!null version:16!null discount:12!null maxinventory:15!null
├── immutable
├── stats: [rows=1]
├── key: (16)
├── fd: (1)-->(2-6,10-16), (2,4,5)~~>(1,3,6), (16)-->(1-6,10-15)
└── inner-join (lookup cards)
├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null dealerid:8!null cardid:9!null buyprice:10!null sellprice:11!null discount:12!null desiredinventory:13!null actualinventory:14!null maxinventory:15!null version:16!null
├── key columns: [9] = [1]
├── lookup columns are key
├── immutable
├── stats: [rows=1, distinct(1)=0.0201621393, null(1)=0, distinct(9)=0.0201621393, null(9)=0]
├── key: (9)
├── fd: ()-->(8), (1)-->(2-6), (2,4,5)~~>(1,3,6), (9)-->(10-16), (16)-->(9-15), (1)==(9), (9)==(1)
├── index-join cardsinfo
│ ├── columns: dealerid:8!null cardid:9!null buyprice:10!null sellprice:11!null discount:12!null desiredinventory:13!null actualinventory:14!null maxinventory:15!null version:16!null
│ ├── immutable
│ ├── stats: [rows=0.0201621426, distinct(8)=0.0201621426, null(8)=0, distinct(9)=0.0201621393, null(9)=0, distinct(10)=0.02016214, null(10)=0, distinct(11)=0.02016214, null(11)=0, distinct(12)=0.02016214, null(12)=0, distinct(13)=0.02016214, null(13)=0, distinct(14)=0.02016214, null(14)=0, distinct(15)=0.02016214, null(15)=0, distinct(16)=0.0201621426, null(16)=0, distinct(8,16)=0.0201621426, null(8,16)=0]
│ │ histogram(16)= 0 0 0.020162 0
│ │ <--- 1584421773604892000.0000000000 ---------- 1584421778604892000
│ ├── key: (9)
│ ├── fd: ()-->(8), (9)-->(10-16), (16)-->(9-15)
│ └── scan cardsinfo@cardsinfoversionindex
│ ├── columns: dealerid:8!null cardid:9!null version:16!null
│ ├── constraint: /8/16: (/1/1584421773604892000.0000000000 - /1]
│ ├── stats: [rows=0.0201621426, distinct(8)=0.0201621426, null(8)=0, distinct(9)=0.0201621393, null(9)=0, distinct(16)=0.0201621426, null(16)=0, distinct(8,16)=0.0201621426, null(8,16)=0]
│ │ histogram(16)= 0 0 0.020162 0
│ │ <--- 1584421773604892000.0000000000 ---------- 1584421778604892000
│ ├── key: (9)
│ └── fd: ()-->(8), (9)-->(16), (16)-->(9)
└── filters (true)
# Get version of last card that was changed.
#
opt format=show-stats
SELECT coalesce(max(Version), 0) FROM GlobalCardsView
----
project
├── columns: coalesce:34
├── cardinality: [1 - 1]
├── stats: [rows=1]
├── key: ()
├── fd: ()-->(34)
├── scalar-group-by
│ ├── columns: max:33
│ ├── cardinality: [1 - 1]
│ ├── stats: [rows=1]
│ ├── key: ()
│ ├── fd: ()-->(33)
│ ├── limit
│ │ ├── columns: dealerid:8!null version:16!null
│ │ ├── internal-ordering: -16
│ │ ├── cardinality: [0 - 1]
│ │ ├── stats: [rows=1]
│ │ ├── key: ()
│ │ ├── fd: ()-->(8,16)
│ │ ├── sort
│ │ │ ├── columns: dealerid:8!null version:16!null
│ │ │ ├── cardinality: [0 - 4]
│ │ │ ├── stats: [rows=4, distinct(8,16)=4, null(8,16)=0]
│ │ │ ├── key: (8,16)
│ │ │ ├── ordering: -16
│ │ │ ├── limit hint: 1.00
│ │ │ └── union
│ │ │ ├── columns: dealerid:8!null version:16!null
│ │ │ ├── left columns: dealerid:8!null version:16!null
│ │ │ ├── right columns: dealerid:65 version:73
│ │ │ ├── cardinality: [0 - 4]
│ │ │ ├── stats: [rows=4, distinct(8,16)=4, null(8,16)=0]
│ │ │ ├── key: (8,16)
│ │ │ ├── union
│ │ │ │ ├── columns: dealerid:8!null version:16!null
│ │ │ │ ├── left columns: dealerid:8!null version:16!null
│ │ │ │ ├── right columns: dealerid:55 version:63
│ │ │ │ ├── cardinality: [0 - 3]
│ │ │ │ ├── stats: [rows=3, distinct(8,16)=3, null(8,16)=0]
│ │ │ │ ├── key: (8,16)
│ │ │ │ ├── union
│ │ │ │ │ ├── columns: dealerid:8!null version:16!null
│ │ │ │ │ ├── left columns: dealerid:35 version:43
│ │ │ │ │ ├── right columns: dealerid:45 version:53
│ │ │ │ │ ├── cardinality: [0 - 2]
│ │ │ │ │ ├── stats: [rows=2, distinct(8,16)=2, null(8,16)=0]
│ │ │ │ │ ├── key: (8,16)
│ │ │ │ │ ├── scan cardsinfo@cardsinfoversionindex,rev
│ │ │ │ │ │ ├── columns: dealerid:35!null version:43!null
│ │ │ │ │ │ ├── constraint: /35/43: [/1 - /1]
│ │ │ │ │ │ ├── limit: 1(rev)
│ │ │ │ │ │ ├── stats: [rows=1, distinct(35)=1, null(35)=0, distinct(35,43)=1, null(35,43)=0]
│ │ │ │ │ │ ├── key: ()
│ │ │ │ │ │ └── fd: ()-->(35,43)
│ │ │ │ │ └── scan cardsinfo@cardsinfoversionindex,rev
│ │ │ │ │ ├── columns: dealerid:45!null version:53!null
│ │ │ │ │ ├── constraint: /45/53: [/2 - /2]
│ │ │ │ │ ├── limit: 1(rev)
│ │ │ │ │ ├── stats: [rows=1, distinct(45)=1, null(45)=0, distinct(45,53)=1, null(45,53)=0]
│ │ │ │ │ ├── key: ()
│ │ │ │ │ └── fd: ()-->(45,53)
│ │ │ │ └── scan cardsinfo@cardsinfoversionindex,rev
│ │ │ │ ├── columns: dealerid:55!null version:63!null
│ │ │ │ ├── constraint: /55/63: [/3 - /3]
│ │ │ │ ├── limit: 1(rev)
│ │ │ │ ├── stats: [rows=1, distinct(55)=1, null(55)=0, distinct(55,63)=1, null(55,63)=0]
│ │ │ │ ├── key: ()
│ │ │ │ └── fd: ()-->(55,63)
│ │ │ └── scan cardsinfo@cardsinfoversionindex,rev
│ │ │ ├── columns: dealerid:65!null version:73!null
│ │ │ ├── constraint: /65/73: [/4 - /4]
│ │ │ ├── limit: 1(rev)
│ │ │ ├── stats: [rows=1, distinct(65)=1, null(65)=0, distinct(65,73)=1, null(65,73)=0]
│ │ │ ├── key: ()
│ │ │ └── fd: ()-->(65,73)
│ │ └── 1
│ └── aggregations
│ └── const-agg [as=max:33, outer=(16)]
│ └── version:16
└── projections
└── COALESCE(max:33, 0) [as=coalesce:34, outer=(33)]
# Show last 20 transactions for a particular card.
#
# Problems:
# 1. Wrong join-type is selected (hash instead of lookup). This is because
# "NOT IsBuy" needs to be mapped to "IsBuy = FALSE".
# 2. Index join should be applied after join between TransactionsView and
# TransactionDetailsView.
#
opt format=show-stats
SELECT
d.IsBuy, TransactionDate, CardId, Quantity, SellPrice, BuyPrice,
t.IsBuy AS IsBuy2, Date, AccountName, CustomerName
FROM TransactionDetailsView d
INNER JOIN TransactionsView t
ON t.Date = d.TransactionDate
WHERE (d.CardId = 21953) AND NOT d.IsBuy AND NOT t.IsBuy
ORDER BY TransactionDate DESC
LIMIT 20
----
project
├── columns: isbuy:2!null transactiondate:3!null cardid:4!null quantity:5!null sellprice:6!null buyprice:7!null isbuy2:11!null date:12!null accountname:13!null customername:14!null
├── cardinality: [0 - 20]
├── stats: [rows=20]
├── key: (5,12)
├── fd: ()-->(2,4,11), (3,5)-->(6,7), (12)-->(13,14), (3)==(12), (12)==(3)
├── ordering: -(3|12) opt(2,4,11) [actual: -3]
└── limit
├── columns: transactiondetails.dealerid:1!null transactiondetails.isbuy:2!null transactiondate:3!null cardid:4!null quantity:5!null sellprice:6!null buyprice:7!null transactions.dealerid:10!null transactions.isbuy:11!null date:12!null accountname:13!null customername:14!null
├── internal-ordering: -(3|12) opt(1,2,4,10,11)
├── cardinality: [0 - 20]
├── stats: [rows=20]
├── key: (5,12)
├── fd: ()-->(1,2,4,10,11), (3,5)-->(6,7), (12)-->(13,14), (3)==(12), (12)==(3)
├── ordering: -(3|12) opt(1,2,4,10,11) [actual: -3]
├── inner-join (lookup transactiondetails)
│ ├── columns: transactiondetails.dealerid:1!null transactiondetails.isbuy:2!null transactiondate:3!null cardid:4!null quantity:5!null sellprice:6!null buyprice:7!null transactions.dealerid:10!null transactions.isbuy:11!null date:12!null accountname:13!null customername:14!null
│ ├── key columns: [1 2 3 4 5] = [1 2 3 4 5]
│ ├── lookup columns are key
│ ├── stats: [rows=478.646617, distinct(3)=478.646617, null(3)=0, distinct(12)=478.646617, null(12)=0]
│ ├── key: (5,12)
│ ├── fd: ()-->(1,2,4,10,11), (3,5)-->(6,7), (12)-->(13,14), (3)==(12), (12)==(3)
│ ├── ordering: -(3|12) opt(1,2,4,10,11) [actual: -3]
│ ├── limit hint: 20.00
│ ├── inner-join (lookup transactions)
│ │ ├── columns: transactiondetails.dealerid:1!null transactiondetails.isbuy:2!null transactiondate:3!null cardid:4!null quantity:5!null transactions.dealerid:10!null transactions.isbuy:11!null date:12!null accountname:13!null customername:14!null
│ │ ├── key columns: [21 22 3] = [10 11 12]
│ │ ├── lookup columns are key
│ │ ├── stats: [rows=478.646617, distinct(3)=478.646617, null(3)=0, distinct(12)=478.646617, null(12)=0]
│ │ ├── key: (5,12)
│ │ ├── fd: ()-->(1,2,4,10,11), (12)-->(13,14), (3)==(12), (12)==(3)
│ │ ├── ordering: -(3|12) opt(1,2,4,10,11) [actual: -3]
│ │ ├── limit hint: 100.00
│ │ ├── project
│ │ │ ├── columns: "project_const_col_@10":21!null "project_const_col_@11":22!null transactiondetails.dealerid:1!null transactiondetails.isbuy:2!null transactiondate:3!null cardid:4!null quantity:5!null
│ │ │ ├── stats: [rows=478.646617]
│ │ │ ├── key: (3,5)
│ │ │ ├── fd: ()-->(1,2,4,21,22)
│ │ │ ├── ordering: -3 opt(1,2,4) [actual: -3]
│ │ │ ├── limit hint: 100.00
│ │ │ ├── scan transactiondetails@detailscardidindex,rev
│ │ │ │ ├── columns: transactiondetails.dealerid:1!null transactiondetails.isbuy:2!null transactiondate:3!null cardid:4!null quantity:5!null
│ │ │ │ ├── constraint: /1/2/4/3/5: [/1/false/21953 - /1/false/21953]
│ │ │ │ ├── stats: [rows=478.646617, distinct(1)=1, null(1)=0, distinct(2)=1, null(2)=0, distinct(3)=478.646617, null(3)=0, distinct(4)=1, null(4)=0, distinct(5)=478.640889, null(5)=0, distinct(1,2,4)=1, null(1,2,4)=0]
│ │ │ │ ├── key: (3,5)
│ │ │ │ ├── fd: ()-->(1,2,4)
│ │ │ │ ├── ordering: -3 opt(1,2,4) [actual: -3]
│ │ │ │ └── limit hint: 100.00
│ │ │ └── projections
│ │ │ ├── 1 [as="project_const_col_@10":21]
│ │ │ └── false [as="project_const_col_@11":22]
│ │ └── filters (true)
│ └── filters (true)
└── 20
# Show last 20 prices for a card.
opt format=show-stats
SELECT CardId, PriceDate, PricedBy, BuyPrice, SellPrice
FROM PriceDetailsView
WHERE CardId = 12345
ORDER BY PriceDate DESC
LIMIT 10
----
project
├── columns: cardid:2!null pricedate:3!null pricedby:4!null buyprice:5!null sellprice:6!null
├── cardinality: [0 - 10]
├── stats: [rows=10]
├── key: (3)
├── fd: ()-->(2), (3)-->(4-6)
├── ordering: -3 opt(2) [actual: -3]
└── scan pricedetails,rev
├── columns: dealerid:1!null cardid:2!null pricedate:3!null pricedby:4!null buyprice:5!null sellprice:6!null
├── constraint: /1/2/3: [/1/12345 - /1/12345]
├── limit: 10(rev)
├── stats: [rows=10]
├── key: (3)
├── fd: ()-->(1,2), (3)-->(4-6)
└── ordering: -3 opt(1,2) [actual: -3]
# Show next page of 50 cards.
#
# Problems:
# 1. The TransactionDate comparisons should be the last 2 days from the
# current timestamp. However, the current timestamp is not treated as a
# constant as it should be.
# 2. Missing rule to push "LIMIT 50" into GroupBy->RightJoin complex. This
# would need to be an exploration rule since it involves an ordering.
# Or we could push down the "limit hint" into GroupBy->RightJoin (and
# further into the InnerJoin).
# 3. Wrong join-type (probably due to #2 above). Should be LookupJoin.
#
opt format=show-stats
SELECT
Id, Name, Rarity, SetName, Number, IsFoil, BuyPrice, SellPrice,
DesiredInventory, ActualInventory, Version, Discount, MaxInventory, Value AS TwoDaySales
FROM
(
SELECT *,
coalesce((
SELECT sum(Quantity)
FROM TransactionDetailsView d
WHERE
d.CardId = c.Id AND
d.IsBuy = FALSE AND
d.TransactionDate BETWEEN '2020-03-01'::TIMESTAMPTZ - INTERVAL '2 days' AND '2020-03-01'::TIMESTAMPTZ
), 0) AS Value
FROM CardsView c
) AS c
WHERE (Name, SetName, Number) > ('Shock', '7E', 248)
ORDER BY Name, SetName, Number
LIMIT 50
----
project
├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null buyprice:10!null sellprice:11!null desiredinventory:13!null actualinventory:14!null version:16!null discount:12!null maxinventory:15!null twodaysales:28
├── cardinality: [0 - 50]
├── immutable
├── stats: [rows=50]
├── key: (16,28)
├── fd: (1)-->(2-6,10-16), (2,4,5)~~>(1,3,6), (16)-->(1-6,10-15)
├── ordering: +2,+4,+5
├── limit
│ ├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null cardsinfo.cardid:9!null cardsinfo.buyprice:10!null cardsinfo.sellprice:11!null discount:12!null desiredinventory:13!null actualinventory:14!null maxinventory:15!null cardsinfo.version:16!null sum:27
│ ├── internal-ordering: +2,+4,+5
│ ├── cardinality: [0 - 50]
│ ├── immutable
│ ├── stats: [rows=50]
│ ├── key: (9)
│ ├── fd: (1)-->(2-6), (2,4,5)~~>(1,3,6), (9)-->(1-6,10-16,27), (16)-->(9-15), (1)==(9), (9)==(1)
│ ├── ordering: +2,+4,+5
│ ├── sort
│ │ ├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null cardsinfo.cardid:9!null cardsinfo.buyprice:10!null cardsinfo.sellprice:11!null discount:12!null desiredinventory:13!null actualinventory:14!null maxinventory:15!null cardsinfo.version:16!null sum:27
│ │ ├── immutable
│ │ ├── stats: [rows=19000, distinct(9)=19000, null(9)=0]
│ │ ├── key: (9)
│ │ ├── fd: (1)-->(2-6), (2,4,5)~~>(1,3,6), (9)-->(1-6,10-16,27), (16)-->(9-15), (1)==(9), (9)==(1)
│ │ ├── ordering: +2,+4,+5
│ │ ├── limit hint: 50.00
│ │ └── group-by
│ │ ├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null cardsinfo.cardid:9!null cardsinfo.buyprice:10!null cardsinfo.sellprice:11!null discount:12!null desiredinventory:13!null actualinventory:14!null maxinventory:15!null cardsinfo.version:16!null sum:27
│ │ ├── grouping columns: cardsinfo.cardid:9!null
│ │ ├── immutable
│ │ ├── stats: [rows=19000, distinct(9)=19000, null(9)=0]
│ │ ├── key: (9)
│ │ ├── fd: (1)-->(2-6), (2,4,5)~~>(1,3,6), (9)-->(1-6,10-16,27), (16)-->(9-15), (1)==(9), (9)==(1)
│ │ ├── right-join (hash)
│ │ │ ├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null cardsinfo.dealerid:8!null cardsinfo.cardid:9!null cardsinfo.buyprice:10!null cardsinfo.sellprice:11!null discount:12!null desiredinventory:13!null actualinventory:14!null maxinventory:15!null cardsinfo.version:16!null transactiondetails.dealerid:18 isbuy:19 transactiondate:20 transactiondetails.cardid:21 quantity:22
│ │ │ ├── immutable
│ │ │ ├── stats: [rows=5523583.18, distinct(9)=19000, null(9)=0, distinct(21)=19000, null(21)=0]
│ │ │ ├── key: (9,20-22)
│ │ │ ├── fd: ()-->(8), (1)-->(2-6), (2,4,5)~~>(1,3,6), (9)-->(10-16), (16)-->(9-15), (1)==(9), (9)==(1), (9,20-22)-->(18,19)
│ │ │ ├── scan transactiondetails
│ │ │ │ ├── columns: transactiondetails.dealerid:18!null isbuy:19!null transactiondate:20!null transactiondetails.cardid:21!null quantity:22!null
│ │ │ │ ├── constraint: /18/19/20/21/22: [/1/false/'2020-02-28 00:00:00+00:00' - /1/false/'2020-03-01 00:00:00+00:00']
│ │ │ │ ├── stats: [rows=10630000, distinct(18)=1, null(18)=0, distinct(19)=1, null(19)=0, distinct(20)=10630000, null(20)=0, distinct(21)=57000, null(21)=0, distinct(18,19)=1, null(18,19)=0, distinct(18-20)=10630000, null(18-20)=0]
│ │ │ │ ├── key: (20-22)
│ │ │ │ └── fd: ()-->(18,19)
│ │ │ ├── inner-join (merge)
│ │ │ │ ├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null cardsinfo.dealerid:8!null cardsinfo.cardid:9!null cardsinfo.buyprice:10!null cardsinfo.sellprice:11!null discount:12!null desiredinventory:13!null actualinventory:14!null maxinventory:15!null cardsinfo.version:16!null
│ │ │ │ ├── left ordering: +1
│ │ │ │ ├── right ordering: +9
│ │ │ │ ├── immutable
│ │ │ │ ├── stats: [rows=29618.4611, distinct(1)=19000, null(1)=0, distinct(2)=11668.1409, null(2)=0, distinct(5)=829, null(5)=0, distinct(6)=5572.85686, null(6)=0, distinct(8)=1, null(8)=0, distinct(9)=19000, null(9)=0, distinct(10)=21037.9959, null(10)=0, distinct(11)=21037.9959, null(11)=0, distinct(12)=21037.9959, null(12)=0, distinct(13)=21037.9959, null(13)=0, distinct(14)=21037.9959, null(14)=0, distinct(15)=21037.9959, null(15)=0, distinct(16)=23225.5851, null(16)=0]
│ │ │ │ ├── key: (9)
│ │ │ │ ├── fd: ()-->(8), (1)-->(2-6), (2,4,5)~~>(1,3,6), (9)-->(10-16), (16)-->(9-15), (1)==(9), (9)==(1)
│ │ │ │ ├── select
│ │ │ │ │ ├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null
│ │ │ │ │ ├── immutable
│ │ │ │ │ ├── stats: [rows=19000, distinct(1)=19000, null(1)=0, distinct(2)=13000, null(2)=0, distinct(5)=829, null(5)=0, distinct(6)=5601.15328, null(6)=0]
│ │ │ │ │ ├── key: (1)
│ │ │ │ │ ├── fd: (1)-->(2-6), (2,4,5)~~>(1,3,6)
│ │ │ │ │ ├── ordering: +1
│ │ │ │ │ ├── scan cards
│ │ │ │ │ │ ├── columns: id:1!null name:2!null rarity:3 setname:4 number:5!null isfoil:6!null
│ │ │ │ │ │ ├── stats: [rows=57000, distinct(1)=57000, null(1)=0, distinct(2)=39000, null(2)=0, distinct(5)=829, null(5)=0, distinct(6)=5700, null(6)=0]
│ │ │ │ │ │ ├── key: (1)
│ │ │ │ │ │ ├── fd: (1)-->(2-6), (2,4,5)~~>(1,3,6)
│ │ │ │ │ │ └── ordering: +1
│ │ │ │ │ └── filters
│ │ │ │ │ └── (name:2, setname:4, number:5) > ('Shock', '7E', 248) [outer=(2,4,5), immutable, constraints=(/2/4/5: [/'Shock'/'7E'/249 - ]; tight)]
│ │ │ │ ├── scan cardsinfo
│ │ │ │ │ ├── columns: cardsinfo.dealerid:8!null cardsinfo.cardid:9!null cardsinfo.buyprice:10!null cardsinfo.sellprice:11!null discount:12!null desiredinventory:13!null actualinventory:14!null maxinventory:15!null cardsinfo.version:16!null
│ │ │ │ │ ├── constraint: /8/9: [/1 - /1]
│ │ │ │ │ ├── stats: [rows=58333.3333, distinct(8)=1, null(8)=0, distinct(9)=37420.3552, null(9)=0, distinct(10)=40676.7278, null(10)=0, distinct(11)=40676.7278, null(11)=0, distinct(12)=40676.7278, null(12)=0, distinct(13)=40676.7278, null(13)=0, distinct(14)=40676.7278, null(14)=0, distinct(15)=40676.7278, null(15)=0, distinct(16)=58333.3333, null(16)=0]
│ │ │ │ │ ├── key: (9)
│ │ │ │ │ ├── fd: ()-->(8), (9)-->(10-16), (16)-->(9-15)
│ │ │ │ │ └── ordering: +9 opt(8) [actual: +9]
│ │ │ │ └── filters (true)
│ │ │ └── filters
│ │ │ └── transactiondetails.cardid:21 = id:1 [outer=(1,21), constraints=(/1: (/NULL - ]; /21: (/NULL - ]), fd=(1)==(21), (21)==(1)]
│ │ └── aggregations
│ │ ├── sum [as=sum:27, outer=(22)]
│ │ │ └── quantity:22
│ │ ├── const-agg [as=id:1, outer=(1)]
│ │ │ └── id:1
│ │ ├── const-agg [as=name:2, outer=(2)]
│ │ │ └── name:2
│ │ ├── const-agg [as=rarity:3, outer=(3)]
│ │ │ └── rarity:3
│ │ ├── const-agg [as=setname:4, outer=(4)]
│ │ │ └── setname:4
│ │ ├── const-agg [as=number:5, outer=(5)]
│ │ │ └── number:5
│ │ ├── const-agg [as=isfoil:6, outer=(6)]
│ │ │ └── isfoil:6
│ │ ├── const-agg [as=cardsinfo.buyprice:10, outer=(10)]
│ │ │ └── cardsinfo.buyprice:10
│ │ ├── const-agg [as=cardsinfo.sellprice:11, outer=(11)]
│ │ │ └── cardsinfo.sellprice:11
│ │ ├── const-agg [as=discount:12, outer=(12)]
│ │ │ └── discount:12
│ │ ├── const-agg [as=desiredinventory:13, outer=(13)]
│ │ │ └── desiredinventory:13
│ │ ├── const-agg [as=actualinventory:14, outer=(14)]
│ │ │ └── actualinventory:14
│ │ ├── const-agg [as=maxinventory:15, outer=(15)]
│ │ │ └── maxinventory:15
│ │ └── const-agg [as=cardsinfo.version:16, outer=(16)]
│ │ └── cardsinfo.version:16
│ └── 50
└── projections
└── COALESCE(sum:27, 0) [as=value:28, outer=(27)]
# Daily transaction query.
#
# Problems:
# 1. CardsView is actually a join between Cards and CardsInfo tables. But the
# optimizer is missing join elimination rules. If those were available, we
# could eliminate the join to Cards (because of FK).
# 2. Inequality predicate terms (accountname / customername) are too
# selective.
# 3. The Date comparisons should be the last 7 days from the current
# timestamp. However, the current timestamp is not treated as a constant as
# it should be.
# 4. The row count estimate for the constrained scan of transactions is too
# large, causing us to choose the incorrect join algorithm (we should
# choose a lookup join). Collecting small histograms on all columns would
# fix the issue.
#
opt format=show-stats
SELECT
extract(day from d.TransactionDate),
sum(d.SellPrice * d.Quantity) AS TotalSell,
sum(d.BuyPrice * d.Quantity) AS TotalBuy,
sum((d.SellPrice - d.BuyPrice) * d.Quantity) AS TotalProfit
FROM TransactionDetailsView d, TransactionsView t, CardsView c
WHERE
d.TransactionDate = t.Date AND
c.Id = d.CardId AND
NOT d.IsBuy AND
NOT t.IsBuy AND
t.Date BETWEEN '2020-03-01'::TIMESTAMPTZ - INTERVAL '7 days' AND '2020-03-01'::TIMESTAMPTZ AND
t.AccountName <> 'someaccount' AND
t.customername <> 'somecustomer'
GROUP BY extract(day from d.TransactionDate)
ORDER BY extract(day from d.TransactionDate)
----
sort
├── columns: extract:41 totalsell:36!null totalbuy:38!null totalprofit:40!null
├── stable
├── stats: [rows=750327.164, distinct(41)=750327.164, null(41)=0]
├── key: (41)
├── fd: (41)-->(36,38,40)
├── ordering: +41
└── group-by
├── columns: sum:36!null sum:38!null sum:40!null column41:41
├── grouping columns: column41:41
├── stable
├── stats: [rows=750327.164, distinct(41)=750327.164, null(41)=0]
├── key: (41)
├── fd: (41)-->(36,38,40)
├── project
│ ├── columns: column35:35!null column37:37!null column39:39!null column41:41
│ ├── stable
│ ├── stats: [rows=1198631.87, distinct(41)=750327.164, null(41)=0]
│ ├── inner-join (hash)
│ │ ├── columns: transactiondetails.dealerid:1!null transactiondetails.isbuy:2!null transactiondate:3!null transactiondetails.cardid:4!null quantity:5!null transactiondetails.sellprice:6!null transactiondetails.buyprice:7!null transactions.dealerid:10!null transactions.isbuy:11!null date:12!null accountname:13!null customername:14!null id:18!null cardsinfo.dealerid:25!null cardsinfo.cardid:26!null
│ │ ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
│ │ ├── stats: [rows=1198631.87, distinct(3)=750327.164, null(3)=0, distinct(4)=37420.3552, null(4)=0, distinct(18)=37420.3552, null(18)=0]
│ │ ├── key: (5,12,26)
│ │ ├── fd: ()-->(1,2,10,11,25), (3-5)-->(6,7), (12)-->(13,14), (3)==(12), (12)==(3), (18)==(4,26), (26)==(4,18), (4)==(18,26)
│ │ ├── inner-join (hash)
│ │ │ ├── columns: transactiondetails.dealerid:1!null transactiondetails.isbuy:2!null transactiondate:3!null transactiondetails.cardid:4!null quantity:5!null transactiondetails.sellprice:6!null transactiondetails.buyprice:7!null transactions.dealerid:10!null transactions.isbuy:11!null date:12!null accountname:13!null customername:14!null
│ │ │ ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
│ │ │ ├── stats: [rows=1171234.57, distinct(1)=1, null(1)=0, distinct(2)=1, null(2)=0, distinct(3)=1171234.57, null(3)=0, distinct(4)=56999.9999, null(4)=0, distinct(5)=1091498.71, null(5)=0, distinct(6)=1091498.71, null(6)=0, distinct(7)=1091498.71, null(7)=0, distinct(10)=1, null(10)=0, distinct(11)=1, null(11)=0, distinct(12)=1171234.57, null(12)=0, distinct(13)=551608.449, null(13)=0, distinct(14)=551608.449, null(14)=0]
│ │ │ ├── key: (4,5,12)
│ │ │ ├── fd: ()-->(1,2,10,11), (3-5)-->(6,7), (12)-->(13,14), (3)==(12), (12)==(3)
│ │ │ ├── scan transactiondetails
│ │ │ │ ├── columns: transactiondetails.dealerid:1!null transactiondetails.isbuy:2!null transactiondate:3!null transactiondetails.cardid:4!null quantity:5!null transactiondetails.sellprice:6!null transactiondetails.buyprice:7!null
│ │ │ │ ├── constraint: /1/2/3/4/5: [/1/false/'2020-02-23 00:00:00+00:00' - /1/false/'2020-03-01 00:00:00+00:00']
│ │ │ │ ├── stats: [rows=10630000, distinct(1)=1, null(1)=0, distinct(2)=1, null(2)=0, distinct(3)=10630000, null(3)=0, distinct(4)=57000, null(4)=0, distinct(5)=8207077.23, null(5)=0, distinct(6)=8207077.23, null(6)=0, distinct(7)=8207077.23, null(7)=0, distinct(1,2)=1, null(1,2)=0, distinct(1-3)=10630000, null(1-3)=0]
│ │ │ │ ├── key: (3-5)
│ │ │ │ └── fd: ()-->(1,2), (3-5)-->(6,7)
│ │ │ ├── select
│ │ │ │ ├── columns: transactions.dealerid:10!null transactions.isbuy:11!null date:12!null accountname:13!null customername:14!null
│ │ │ │ ├── stats: [rows=1171234.57, distinct(10)=1, null(10)=0, distinct(11)=1, null(11)=0, distinct(12)=1171234.57, null(12)=0, distinct(13)=666666.667, null(13)=0, distinct(14)=666666.667, null(14)=0, distinct(10,11)=1, null(10,11)=0, distinct(10-14)=1171234.57, null(10-14)=0]
│ │ │ │ ├── key: (12)
│ │ │ │ ├── fd: ()-->(10,11), (12)-->(13,14)
│ │ │ │ ├── scan transactions
│ │ │ │ │ ├── columns: transactions.dealerid:10!null transactions.isbuy:11!null date:12!null accountname:13!null customername:14!null
│ │ │ │ │ ├── constraint: /10/11/12: [/1/false/'2020-02-23 00:00:00+00:00' - /1/false/'2020-03-01 00:00:00+00:00']
│ │ │ │ │ ├── stats: [rows=1181111.11, distinct(10)=1, null(10)=0, distinct(11)=1, null(11)=0, distinct(12)=1181111.11, null(12)=0, distinct(10,11)=1, null(10,11)=0, distinct(10-12)=1181111.11, null(10-12)=0]
│ │ │ │ │ ├── key: (12)
│ │ │ │ │ └── fd: ()-->(10,11), (12)-->(13,14)
│ │ │ │ └── filters
│ │ │ │ ├── accountname:13 != 'someaccount' [outer=(13), constraints=(/13: (/NULL - /'someaccount') [/e'someaccount\x00' - ]; tight)]
│ │ │ │ └── customername:14 != 'somecustomer' [outer=(14), constraints=(/14: (/NULL - /'somecustomer') [/e'somecustomer\x00' - ]; tight)]
│ │ │ └── filters
│ │ │ └── transactiondate:3 = date:12 [outer=(3,12), constraints=(/3: (/NULL - ]; /12: (/NULL - ]), fd=(3)==(12), (12)==(3)]
│ │ ├── inner-join (hash)
│ │ │ ├── columns: id:18!null cardsinfo.dealerid:25!null cardsinfo.cardid:26!null
│ │ │ ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-one)
│ │ │ ├── stats: [rows=58333.3333, distinct(18)=37420.3552, null(18)=0, distinct(25)=1, null(25)=0, distinct(26)=37420.3552, null(26)=0]
│ │ │ ├── key: (26)