-
Notifications
You must be signed in to change notification settings - Fork 889
Expand file tree
/
Copy pathjoin_optimizer.cc
More file actions
7004 lines (6371 loc) · 296 KB
/
join_optimizer.cc
File metadata and controls
7004 lines (6371 loc) · 296 KB
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
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/join_optimizer/join_optimizer.h"
#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <bitset>
#include <initializer_list>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ft_global.h"
#include "map_helpers.h"
#include "mem_root_deque.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_bitmap.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "my_table_map.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/udf_registration_types.h"
#include "mysqld_error.h"
#include "prealloced_array.h"
#include "scope_guard.h"
#include "sql/field.h"
#include "sql/filesort.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/item_sum.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/join_optimizer/bit_utils.h"
#include "sql/join_optimizer/build_interesting_orders.h"
#include "sql/join_optimizer/compare_access_paths.h"
#include "sql/join_optimizer/cost_model.h"
#include "sql/join_optimizer/estimate_selectivity.h"
#include "sql/join_optimizer/explain_access_path.h"
#include "sql/join_optimizer/find_contained_subqueries.h"
#include "sql/join_optimizer/graph_simplification.h"
#include "sql/join_optimizer/hypergraph.h"
#include "sql/join_optimizer/interesting_orders.h"
#include "sql/join_optimizer/interesting_orders_defs.h"
#include "sql/join_optimizer/make_join_hypergraph.h"
#include "sql/join_optimizer/node_map.h"
#include "sql/join_optimizer/overflow_bitset.h"
#include "sql/join_optimizer/print_utils.h"
#include "sql/join_optimizer/relational_expression.h"
#include "sql/join_optimizer/secondary_engine_costing_flags.h"
#include "sql/join_optimizer/subgraph_enumeration.h"
#include "sql/join_optimizer/walk_access_paths.h"
#include "sql/join_type.h"
#include "sql/key.h"
#include "sql/key_spec.h"
#include "sql/mem_root_array.h"
#include "sql/opt_costmodel.h"
#include "sql/parse_tree_node_base.h"
#include "sql/partition_info.h"
#include "sql/query_options.h"
#include "sql/range_optimizer/index_range_scan_plan.h"
#include "sql/range_optimizer/internal.h"
#include "sql/range_optimizer/path_helpers.h"
#include "sql/range_optimizer/range_analysis.h"
#include "sql/range_optimizer/range_opt_param.h"
#include "sql/range_optimizer/range_optimizer.h"
#include "sql/range_optimizer/tree.h"
#include "sql/sql_array.h"
#include "sql/sql_base.h"
#include "sql/sql_bitmap.h"
#include "sql/sql_class.h"
#include "sql/sql_cmd.h"
#include "sql/sql_const.h"
#include "sql/sql_executor.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_opt_exec_shared.h"
#include "sql/sql_optimizer.h"
#include "sql/sql_partition.h"
#include "sql/sql_select.h"
#include "sql/sql_tmp_table.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/table_function.h"
#include "sql/temp_table_param.h"
#include "sql/uniques.h"
#include "sql/window.h"
#include "template_utils.h"
using hypergraph::Hyperedge;
using hypergraph::Node;
using hypergraph::NodeMap;
using std::find_if;
using std::min;
using std::pair;
using std::string;
using std::swap;
using std::vector;
namespace {
string PrintAccessPath(const AccessPath &path, const JoinHypergraph &graph,
const char *description_for_trace);
void PrintJoinOrder(const AccessPath *path, string *join_order);
AccessPath *CreateMaterializationPath(THD *thd, JOIN *join, AccessPath *path,
TABLE *temp_table,
Temp_table_param *temp_table_param,
bool copy_items);
AccessPath *GetSafePathToSort(THD *thd, JOIN *join, AccessPath *path,
bool need_rowid);
/**
CostingReceiver contains the main join planning logic, selecting access paths
based on cost. It receives subplans from DPhyp (see enumerate_subgraph.h),
assigns them costs based on a cost model, and keeps the ones that are
cheapest. In the end, this means it will be left with a root access path that
gives the lowest total cost for joining the tables in the query block, ie.,
without ORDER BY etc.
Currently, besides the expected number of produced rows (which is the same no
matter how we access the table) we keep only a single value per subplan
(total cost), and thus also only a single best access path. In the future,
we will have more dimensions to worry about, such as initial cost versus total
cost (relevant for LIMIT), ordering properties, and so on. At that point,
there is not necessarily a single “best” access path anymore, and we will need
to keep multiple ones around, and test all of them as candidates when building
larger subplans.
*/
class CostingReceiver {
public:
CostingReceiver(
THD *thd, Query_block *query_block, JoinHypergraph &graph,
const LogicalOrderings *orderings,
const Mem_root_array<SortAheadOrdering> *sort_ahead_orderings,
const Mem_root_array<ActiveIndexInfo> *active_indexes,
const Mem_root_array<FullTextIndexInfo> *fulltext_searches,
NodeMap fulltext_tables, uint64_t sargable_fulltext_predicates,
table_map update_delete_target_tables,
table_map immediate_update_delete_candidates, bool need_rowid,
SecondaryEngineFlags engine_flags, int subgraph_pair_limit,
secondary_engine_modify_access_path_cost_t secondary_engine_cost_hook,
string *trace)
: m_thd(thd),
m_query_block(query_block),
m_access_paths(thd->mem_root),
m_graph(&graph),
m_orderings(orderings),
m_sort_ahead_orderings(sort_ahead_orderings),
m_active_indexes(active_indexes),
m_fulltext_searches(fulltext_searches),
m_fulltext_tables(fulltext_tables),
m_sargable_fulltext_predicates(sargable_fulltext_predicates),
m_update_delete_target_nodes(GetNodeMapFromTableMap(
update_delete_target_tables, graph.table_num_to_node_num)),
m_immediate_update_delete_candidates(GetNodeMapFromTableMap(
immediate_update_delete_candidates, graph.table_num_to_node_num)),
m_need_rowid(need_rowid),
m_engine_flags(engine_flags),
m_subgraph_pair_limit(subgraph_pair_limit),
m_secondary_engine_cost_hook(secondary_engine_cost_hook),
m_trace(trace) {
// At least one join type must be supported.
assert(Overlaps(engine_flags,
MakeSecondaryEngineFlags(
SecondaryEngineFlag::SUPPORTS_HASH_JOIN,
SecondaryEngineFlag::SUPPORTS_NESTED_LOOP_JOIN)));
}
// Not copyable, but movable so that we can reset it after graph
// simplification if needed.
CostingReceiver &operator=(const CostingReceiver &) = delete;
CostingReceiver &operator=(CostingReceiver &&) = default;
CostingReceiver(const CostingReceiver &) = delete;
CostingReceiver(CostingReceiver &&) = default;
bool HasSeen(NodeMap subgraph) const {
return m_access_paths.count(subgraph) != 0;
}
bool FoundSingleNode(int node_idx);
// Called EmitCsgCmp() in the DPhyp paper.
bool FoundSubgraphPair(NodeMap left, NodeMap right, int edge_idx);
const Prealloced_array<AccessPath *, 4> &root_candidates() {
const auto it =
m_access_paths.find(TablesBetween(0, m_graph->nodes.size()));
assert(it != m_access_paths.end());
return it->second.paths;
}
FunctionalDependencySet active_fds_at_root() const {
const auto it =
m_access_paths.find(TablesBetween(0, m_graph->nodes.size()));
assert(it != m_access_paths.end());
return it->second.active_functional_dependencies;
}
size_t num_subplans() const { return m_access_paths.size(); }
size_t num_access_paths() const {
size_t access_paths = 0;
for (const auto &[nodes, pathset] : m_access_paths) {
access_paths += pathset.paths.size();
}
return access_paths;
}
/// True if the result of the join is found to be always empty, typically
/// because of an impossible WHERE clause.
bool always_empty() const {
const auto it =
m_access_paths.find(TablesBetween(0, m_graph->nodes.size()));
return it != m_access_paths.end() && it->second.always_empty;
}
AccessPath *ProposeAccessPath(
AccessPath *path, Prealloced_array<AccessPath *, 4> *existing_paths,
OrderingSet obsolete_orderings, const char *description_for_trace) const;
bool HasSecondaryEngineCostHook() const {
return m_secondary_engine_cost_hook != nullptr;
}
private:
THD *m_thd;
/// The query block we are planning.
Query_block *m_query_block;
/**
Besides the access paths for a set of nodes (see m_access_paths),
AccessPathSet contains information that is common between all access
paths for that set. One would believe num_output_rows would be such
a member (a set of tables should produce the same number of output
rows no matter the join order), but due to parameterized paths,
different access paths could have different outputs. delayed_predicates
is another, but currently, it's already efficiently hidden space-wise
due to the use of a union.
*/
struct AccessPathSet {
Prealloced_array<AccessPath *, 4> paths;
FunctionalDependencySet active_functional_dependencies{0};
// Once-interesting orderings that we don't care about anymore,
// e.g. because they were interesting for a semijoin but that semijoin
// is now done (with or without using the ordering). This reduces
// the number of access paths we have to keep in play, since they are
// de-facto equivalent.
//
// Note that if orderings were merged, this could falsely prune out
// orderings that we would actually need, but as long as all of the
// relevant ones are semijoin orderings (which are never identical,
// and never merged with the relevant-at-end orderings), this
// should not happen.
OrderingSet obsolete_orderings{0};
// True if the join of the tables in this set has been found to be always
// empty (typically because of an impossible WHERE clause).
bool always_empty{false};
};
/**
For each subset of tables that are connected in the join hypergraph,
keeps the current best access paths for producing said subset.
There can be several that are best in different ways; see comments
on ProposeAccessPath().
Also used for communicating connectivity information back to DPhyp
(in HasSeen()); if there's an entry here, that subset will induce
a connected subgraph of the join hypergraph.
*/
mem_root_unordered_map<NodeMap, AccessPathSet> m_access_paths;
/**
How many subgraph pairs we've seen so far. Used to give up
if we end up allocating too many resources (prompting us to
create a simpler join graph and try again).
*/
int m_num_seen_subgraph_pairs = 0;
/// The graph we are running over.
JoinHypergraph *m_graph;
/// Whether we have applied clamping due to a multi-column EQ_REF at any
/// point. There is a known issue (see bug #33550360) where this can cause
/// row count estimates to be inconsistent between different access paths.
/// Obviously, we should fix this bug by adjusting the selectivities
/// (and we do for single-column indexes), but for multipart indexes,
/// this is nontrivial. See the bug for details on some ideas, but the
/// gist of it is that we probably will want a linear program to adjust
/// multi-selectivities so that they are consistent, and not above 1/N
/// (for N-row tables) if there are unique indexes on them.
///
/// The only reason why we collect this information, like
/// JoinHypergraph::has_reordered_left_joins, is to be able to assert
/// on inconsistent row counts between APs, excluding this (known) issue.
bool has_clamped_multipart_eq_ref = false;
/// Whether we have a semijoin where the inner child is parameterized on the
/// outer child, and the row estimate of the inner child is possibly clamped,
/// for example because of some other semijoin. In this case, we may see
/// inconsistent row count estimates between the ordinary semijoin plan and
/// the rewrite_semi_to_inner plan, because it's hard to tell how much the
/// already-applied-as-sargable selectivity affected the row count estimate of
/// the child.
///
/// The only reason why we collect this information, is to be able to assert
/// on inconsistent row counts between access paths, excluding this known
/// issue.
bool has_semijoin_with_possibly_clamped_child = false;
/// Keeps track of interesting orderings in this query block.
/// See LogicalOrderings for more information.
const LogicalOrderings *m_orderings;
/// List of all orderings that are candidates for sort-ahead
/// (because they are, or may eventually become, an interesting ordering).
const Mem_root_array<SortAheadOrdering> *m_sort_ahead_orderings;
/// List of all indexes that are active and that we can apply in this query.
/// Indexes can be useful in several ways: We can use them for ref access,
/// for index-only scans, or to get interesting orderings.
const Mem_root_array<ActiveIndexInfo> *m_active_indexes;
/// List of all active full-text indexes that we can apply in this query.
const Mem_root_array<FullTextIndexInfo> *m_fulltext_searches;
/// A map of tables that are referenced by a MATCH function (those tables that
/// have Table_ref::is_fulltext_searched() == true). It is used for
/// preventing hash joins involving tables that are full-text searched.
NodeMap m_fulltext_tables = 0;
/// The set of WHERE predicates which are on a form that can be satisfied by a
/// full-text index scan. This includes calls to MATCH with no comparison
/// operator, and predicates on the form MATCH > const or MATCH >= const
/// (where const must be high enough to make the comparison return false for
/// documents with zero score).
uint64_t m_sargable_fulltext_predicates = 0;
/// The target tables of an UPDATE or DELETE statement.
NodeMap m_update_delete_target_nodes = 0;
/// The set of tables that are candidates for immediate update or delete.
/// Immediate update/delete means that the rows from the table are deleted
/// while reading the rows from the topmost iterator. (As opposed to buffered
/// update/delete, where the row IDs are stored in temporary tables, and only
/// updated or deleted after the topmost iterator has been read to the end.)
/// The candidates are those target tables that are only referenced once in
/// the query. The requirement for immediate deletion is that the deleted row
/// will not have to be read again later. Currently, at most one of the
/// candidate tables is chosen, and it is always the outermost table in the
/// join tree.
NodeMap m_immediate_update_delete_candidates = 0;
/// Whether we will be needing row IDs from our tables, typically for
/// a later sort. If this happens, derived tables cannot use streaming,
/// but need an actual materialization, since filesort expects to be
/// able to go back and ask for a given row. (This is different from
/// when we need row IDs for weedout, which doesn't preclude streaming.
/// The hypergraph optimizer does not use weedout.)
bool m_need_rowid;
/// The flags declared by the secondary engine. In particular, it describes
/// what kind of access path types should not be created.
SecondaryEngineFlags m_engine_flags;
/// The maximum number of pairs of subgraphs we are willing to accept,
/// or -1 if no limit. If this limit gets hit, we stop traversing the graph
/// and return an error; the caller will then have to modify the hypergraph
/// (see GraphSimplifier) to make for a graph with fewer options, so that
/// planning time will come under an acceptable limit.
int m_subgraph_pair_limit;
/// Pointer to a function that modifies the cost estimates of an access path
/// for execution in a secondary storage engine, or nullptr otherwise.
secondary_engine_modify_access_path_cost_t m_secondary_engine_cost_hook;
/// If not nullptr, we store human-readable optimizer trace information here.
string *m_trace;
/// A map of tables that can never be on the right side of any join,
/// ie., they have to be leftmost in the tree. This only affects recursive
/// table references (ie., when WITH RECURSIVE is in use); they work by
/// continuously tailing new records, which wouldn't work if we were to
/// scan them multiple times or put them in a hash table. Naturally,
/// there must be zero or one bit here; the common case is zero.
NodeMap forced_leftmost_table = 0;
/// A special MEM_ROOT for allocating OverflowBitsets that we might end up
/// discarding, ie. for AccessPaths that do not yet live in m_access_paths.
/// For any AccessPath that is to have a permanent life (ie., not be
/// immediately discarded as inferior), the OverflowBitset _must_ be taken
/// out of this MEM_ROOT and onto the regular one, as it is cleared often.
/// (This significantly reduces the amount of memory used in situations
/// where lots of AccessPaths are produced and discarded. Of course,
/// it only matters for queries with >= 64 predicates.)
///
/// The copying is using CommitBitsetsToHeap(). ProposeAccessPath() will
/// automatically call CommitBitsetsToHeap() for accepted access paths,
/// but it will not call it on any of their children. Thus, if you've
/// added more than one AccessPath in the chain (e.g. if you add a join,
/// then a sort of that join, and then propose the sort), you will need
/// to make sure there are no stray bitsets left on this MEM_ROOT.
///
/// Because this can be a source of subtle bugs, you should be conservative
/// about what bitsets you put here; really, only the ones you could be
/// allocating many of (like joins) should be candidates.
MEM_ROOT m_overflow_bitset_mem_root;
/// A special MEM_ROOT for temporary data for the range optimizer.
/// It can be discarded immediately after we've decided on the range scans
/// for a given table (but we reuse its memory as long as there are more
/// tables left to scan).
MEM_ROOT m_range_optimizer_mem_root;
/// For trace use only.
string PrintSet(NodeMap x) const {
std::string ret = "{";
bool first = true;
for (size_t node_idx : BitsSetIn(x)) {
if (!first) {
ret += ",";
}
first = false;
ret += m_graph->nodes[node_idx].table->alias;
}
return ret + "}";
}
/// For trace use only.
string PrintSubgraphHeader(const JoinPredicate *edge,
const AccessPath &join_path, NodeMap left,
NodeMap right) const;
/// Checks whether the given engine flag is active or not.
bool SupportedEngineFlag(SecondaryEngineFlag flag) const {
return Overlaps(m_engine_flags, MakeSecondaryEngineFlags(flag));
}
bool FindIndexRangeScans(int node_idx, bool *impossible,
double *num_output_rows_after_filter);
void ProposeIndexMerge(TABLE *table, int node_idx, const SEL_IMERGE &imerge,
int pred_idx, bool inexact,
bool allow_clustered_primary_key_scan,
int num_where_predicates,
double num_output_rows_after_filter,
RANGE_OPT_PARAM *param,
bool *has_clustered_primary_key_scan);
void TraceAccessPaths(NodeMap nodes);
void ProposeAccessPathForBaseTable(int node_idx,
double force_num_output_rows_after_filter,
const char *description_for_trace,
AccessPath *path);
void ProposeAccessPathForIndex(int node_idx,
OverflowBitset applied_predicates,
OverflowBitset subsumed_predicates,
double force_num_output_rows_after_filter,
const char *description_for_trace,
AccessPath *path);
void ProposeAccessPathWithOrderings(NodeMap nodes,
FunctionalDependencySet fd_set,
OrderingSet obsolete_orderings,
AccessPath *path,
const char *description_for_trace);
bool ProposeTableScan(TABLE *table, int node_idx,
double force_num_output_rows_after_filter);
bool ProposeIndexScan(TABLE *table, int node_idx,
double force_num_output_rows_after_filter,
unsigned key_idx, bool reverse, int ordering_idx);
bool ProposeRefAccess(TABLE *table, int node_idx, unsigned key_idx,
double force_num_output_rows_after_filter, bool reverse,
table_map allowed_parameter_tables, int ordering_idx);
bool ProposeAllUniqueIndexLookupsWithConstantKey(int node_idx, bool *found);
bool RedundantThroughSargable(
OverflowBitset redundant_against_sargable_predicates,
const AccessPath *left_path, const AccessPath *right_path, NodeMap left,
NodeMap right);
inline pair<bool, bool> AlreadyAppliedAsSargable(
Item *condition, const AccessPath *left_path,
const AccessPath *right_path);
bool ProposeAllFullTextIndexScans(TABLE *table, int node_idx,
double force_num_output_rows_after_filter);
bool ProposeFullTextIndexScan(TABLE *table, int node_idx,
Item_func_match *match, int predicate_idx,
int ordering_idx,
double force_num_output_rows_after_filter);
void ProposeNestedLoopJoin(NodeMap left, NodeMap right, AccessPath *left_path,
AccessPath *right_path, const JoinPredicate *edge,
bool rewrite_semi_to_inner,
FunctionalDependencySet new_fd_set,
OrderingSet new_obsolete_orderings,
bool *wrote_trace);
void ProposeHashJoin(NodeMap left, NodeMap right, AccessPath *left_path,
AccessPath *right_path, const JoinPredicate *edge,
FunctionalDependencySet new_fd_set,
OrderingSet new_obsolete_orderings,
bool rewrite_semi_to_inner, bool *wrote_trace);
void ApplyPredicatesForBaseTable(int node_idx,
OverflowBitset applied_predicates,
OverflowBitset subsumed_predicates,
bool materialize_subqueries,
AccessPath *path,
FunctionalDependencySet *new_fd_set);
void ApplyDelayedPredicatesAfterJoin(
NodeMap left, NodeMap right, const AccessPath *left_path,
const AccessPath *right_path, int join_predicate_first,
int join_predicate_last, bool materialize_subqueries,
AccessPath *join_path, FunctionalDependencySet *new_fd_set);
double FindAlreadyAppliedSelectivity(const JoinPredicate *edge,
const AccessPath *left_path,
const AccessPath *right_path,
NodeMap left, NodeMap right);
void CommitBitsetsToHeap(AccessPath *path) const;
bool BitsetsAreCommitted(AccessPath *path) const;
};
/// Lists the current secondary engine flags in use. If there is no secondary
/// engine, will use a default set of permissive flags suitable for
/// non-secondary engine use.
SecondaryEngineFlags EngineFlags(const THD *thd) {
if (const handlerton *secondary_engine = SecondaryEngineHandlerton(thd);
secondary_engine != nullptr) {
return secondary_engine->secondary_engine_flags;
}
return MakeSecondaryEngineFlags(
SecondaryEngineFlag::SUPPORTS_HASH_JOIN,
SecondaryEngineFlag::SUPPORTS_NESTED_LOOP_JOIN);
}
/// Gets the secondary storage engine cost modification function, if any.
secondary_engine_modify_access_path_cost_t SecondaryEngineCostHook(
const THD *thd) {
const handlerton *secondary_engine = SecondaryEngineHandlerton(thd);
if (secondary_engine == nullptr) {
return nullptr;
} else {
return secondary_engine->secondary_engine_modify_access_path_cost;
}
}
/// Returns the MATCH function of a predicate that can be pushed down to a
/// full-text index. This can be done if the predicate is a MATCH function,
/// or in some cases (see IsSargableFullTextIndexPredicate() for details)
/// where the predicate is a comparison function which compares the result
/// of MATCH with a constant. For example, predicates on this form could be
/// pushed down to a full-text index:
///
/// WHERE MATCH (x) AGAINST ('search string') AND @<more predicates@>
///
/// WHERE MATCH (x) AGAINST ('search string') > 0.5 AND @<more predicates@>
///
/// Since full-text index scans return documents with positive scores only, an
/// index scan can only be used if the predicate excludes negative or zero
/// scores.
Item_func_match *GetSargableFullTextPredicate(const Predicate &predicate) {
Item_func *func = down_cast<Item_func *>(predicate.condition);
switch (func->functype()) {
case Item_func::MATCH_FUNC:
// The predicate is MATCH (x) AGAINST ('search string'), which can be
// pushed to the index.
return down_cast<Item_func_match *>(func->get_arg(0))->get_master();
case Item_func::LT_FUNC:
case Item_func::LE_FUNC:
// The predicate is const < MATCH or const <= MATCH, with a constant value
// which makes it pushable.
assert(func->get_arg(0)->const_item());
return down_cast<Item_func_match *>(func->get_arg(1))->get_master();
case Item_func::GT_FUNC:
case Item_func::GE_FUNC:
// The predicate is MATCH > const or MATCH >= const, with a constant value
// which makes it pushable.
assert(func->get_arg(1)->const_item());
return down_cast<Item_func_match *>(func->get_arg(0))->get_master();
default:
// The predicate is not on a form that can be pushed to a full-text index
// scan. We should not get here.
assert(false);
return nullptr;
}
}
/// Is the current statement a DELETE statement?
bool IsDeleteStatement(const THD *thd) {
return thd->lex->sql_command == SQLCOM_DELETE ||
thd->lex->sql_command == SQLCOM_DELETE_MULTI;
}
/// Is the current statement a DELETE statement?
bool IsUpdateStatement(const THD *thd) {
return thd->lex->sql_command == SQLCOM_UPDATE ||
thd->lex->sql_command == SQLCOM_UPDATE_MULTI;
}
void CostingReceiver::TraceAccessPaths(NodeMap nodes) {
auto it = m_access_paths.find(nodes);
if (it == m_access_paths.end()) {
*m_trace += " - ";
*m_trace += PrintSet(nodes);
*m_trace += " has no access paths (this should not normally happen)\n";
return;
}
*m_trace += " - current access paths for ";
*m_trace += PrintSet(nodes);
*m_trace += ": ";
bool first = true;
for (const AccessPath *path : it->second.paths) {
if (!first) {
*m_trace += ", ";
}
*m_trace += PrintAccessPath(*path, *m_graph, "");
first = false;
}
*m_trace += ")\n";
}
/**
Called for each table in the query block, at some arbitrary point before we
start seeing subsets where it's joined to other tables.
We support table scans and ref access, so we create access paths for both
(where possible) and cost them. In this context, “tables” in a query block
also includes virtual tables such as derived tables, so we need to figure out
if there is a cost for materializing them.
*/
bool CostingReceiver::FoundSingleNode(int node_idx) {
if (m_thd->is_error()) return true;
m_graph->secondary_engine_costing_flags &=
~SecondaryEngineCostingFlag::HAS_MULTIPLE_BASE_TABLES;
TABLE *table = m_graph->nodes[node_idx].table;
Table_ref *tl = table->pos_in_table_list;
if (m_trace != nullptr) {
*m_trace += StringPrintf("\nFound node %s [rows=%llu]\n",
m_graph->nodes[node_idx].table->alias,
table->file->stats.records);
}
// First look for unique index lookups that use only constants.
{
bool found_eq_ref = false;
if (ProposeAllUniqueIndexLookupsWithConstantKey(node_idx, &found_eq_ref)) {
return true;
}
// If we found an unparameterized EQ_REF path, we can skip looking for
// alternative access methods, like parameterized or non-unique index
// lookups, index range scans or table scans, as they are unlikely to be any
// better. Returning early to reduce time spent planning the query, which is
// especially beneficial for point selects.
if (found_eq_ref) {
if (m_trace != nullptr) {
TraceAccessPaths(TableBitmap(node_idx));
}
return false;
}
}
// We run the range optimizer before anything else, because we can use
// its estimates to adjust predicate selectivity, giving us consistent
// row count estimation between the access paths. (It is also usually
// more precise for complex range conditions than our default estimates.
// This is also the reason why we run it even if HA_NO_INDEX_ACCESS is set.)
double range_optimizer_row_estimate = -1.0;
{
auto cleanup_mem_root = create_scope_guard([this, node_idx] {
if (node_idx == 0) {
// We won't be calling the range optimizer anymore, so we don't need
// to keep its temporary allocations around. Note that FoundSingleNode()
// counts down from N-1 to 0, not up.
m_range_optimizer_mem_root.Clear();
} else {
m_range_optimizer_mem_root.ClearForReuse();
}
});
if (!tl->is_recursive_reference() && m_graph->num_where_predicates > 0) {
// Note that true error returns in itself is not enough to fail the query;
// the range optimizer could be out of RAM easily enough, which is
// nonfatal. That just means we won't be using it for this table.
bool impossible = false;
if (FindIndexRangeScans(node_idx, &impossible,
&range_optimizer_row_estimate) &&
m_thd->is_error()) {
return true;
}
if (impossible) {
const char *const cause = "WHERE condition is always false";
if (!IsBitSet(tl->tableno(), m_graph->tables_inner_to_outer_or_anti)) {
// The entire top-level join is going to be empty, so we can abort the
// planning and return a zero rows plan.
m_query_block->join->zero_result_cause = cause;
return true;
}
AccessPath *table_path =
NewTableScanAccessPath(m_thd, table, /*count_examined_rows=*/false);
AccessPath *zero_path = NewZeroRowsAccessPath(m_thd, table_path, cause);
// We need to get the set of functional dependencies right,
// even though we don't need to actually apply any filters.
FunctionalDependencySet new_fd_set;
ApplyPredicatesForBaseTable(
node_idx,
/*applied_predicates=*/
MutableOverflowBitset{m_thd->mem_root, m_graph->predicates.size()},
/*subsumed_predicates=*/
MutableOverflowBitset{m_thd->mem_root, m_graph->predicates.size()},
/*materialize_subqueries=*/false, zero_path, &new_fd_set);
zero_path->filter_predicates =
MutableOverflowBitset{m_thd->mem_root, m_graph->predicates.size()};
zero_path->ordering_state =
m_orderings->ApplyFDs(zero_path->ordering_state, new_fd_set);
ProposeAccessPathWithOrderings(TableBitmap(node_idx), new_fd_set,
/*obsolete_orderings=*/0, zero_path, "");
if (m_trace != nullptr) {
TraceAccessPaths(TableBitmap(node_idx));
}
return false;
}
}
}
if (ProposeTableScan(table, node_idx, range_optimizer_row_estimate)) {
return true;
}
if (Overlaps(table->file->ha_table_flags(), HA_NO_INDEX_ACCESS) ||
tl->is_recursive_reference()) {
// We can't use any indexes, so end here.
if (m_trace != nullptr) {
TraceAccessPaths(TableBitmap(node_idx));
}
return false;
}
// Propose index scan (for getting interesting orderings).
// We only consider those that are more interesting than a table scan;
// for the others, we don't even need to create the access path and go
// through the tournament.
for (const ActiveIndexInfo &order_info : *m_active_indexes) {
if (order_info.table != table) {
continue;
}
const int forward_order =
m_orderings->RemapOrderingIndex(order_info.forward_order);
const int reverse_order =
m_orderings->RemapOrderingIndex(order_info.reverse_order);
for (bool reverse : {false, true}) {
if (reverse && reverse_order == 0) {
continue;
}
const int order = reverse ? reverse_order : forward_order;
if (order != 0) {
if (ProposeIndexScan(table, node_idx, range_optimizer_row_estimate,
order_info.key_idx, reverse, order)) {
return true;
}
}
// Propose ref access using only sargable predicates that reference no
// other table.
if (ProposeRefAccess(table, node_idx, order_info.key_idx,
range_optimizer_row_estimate, reverse,
/*allowed_parameter_tables=*/0, order)) {
return true;
}
// Propose ref access using all sargable predicates that also refer to
// other tables (e.g. t1.x = t2.x). Such access paths can only be used
// on the inner side of a nested loop join, where all the other
// referenced tables are among the outer tables of the join. Such path
// is called a parameterized path.
//
// Since indexes can have multiple parts, the access path can also end
// up being parameterized on multiple outer tables. However, since
// parameterized paths are less flexible in joining than
// non-parameterized ones, it can be advantageous to not use all parts
// of the index; it's impossible to say locally. Thus, we enumerate all
// possible subsets of table parameters that may be useful, to make sure
// we don't miss any such paths.
table_map want_parameter_tables = 0;
for (const SargablePredicate &sp :
m_graph->nodes[node_idx].sargable_predicates) {
if (sp.field->table == table &&
sp.field->part_of_key.is_set(order_info.key_idx) &&
!Overlaps(sp.other_side->used_tables(),
PSEUDO_TABLE_BITS | table->pos_in_table_list->map())) {
want_parameter_tables |= sp.other_side->used_tables();
}
}
for (table_map allowed_parameter_tables :
NonzeroSubsetsOf(want_parameter_tables)) {
if (ProposeRefAccess(table, node_idx, order_info.key_idx,
range_optimizer_row_estimate, reverse,
allowed_parameter_tables, order)) {
return true;
}
}
}
}
if (tl->is_fulltext_searched()) {
if (ProposeAllFullTextIndexScans(table, node_idx,
range_optimizer_row_estimate)) {
return true;
}
}
if (m_trace != nullptr) {
TraceAccessPaths(TableBitmap(node_idx));
}
return false;
}
// Figure out which predicates we have that are not applied/subsumed
// by scanning this specific index; we already did a check earlier,
// but that was on predicates applied by scanning _any_ index.
// The difference is those that
//
// a) Use a field that's not part of this index.
// b) Use a field that our index is only partial for; these are still
// counted as applied for selectivity purposes (which is possibly
// overcounting), but need to be rechecked (ie., not subsumed).
// c) Use a field where we've been told by get_ranges_from_tree()
// that it had to set up a range that was nonexact (because it was
// part of a predicate that had a non-equality condition on an
// earlier keypart). These are handled as for b).
//
// We use this information to build up sets of which fields an
// applied or subsumed predicate is allowed to reference,
// then check each predicate against those lists.
void FindAppliedAndSubsumedPredicatesForRangeScan(
THD *thd, KEY *key, unsigned used_key_parts, unsigned num_exact_key_parts,
TABLE *table, OverflowBitset tree_applied_predicates,
OverflowBitset tree_subsumed_predicates, const JoinHypergraph &graph,
OverflowBitset *applied_predicates_out,
OverflowBitset *subsumed_predicates_out) {
MutableOverflowBitset applied_fields{thd->mem_root, table->s->fields};
MutableOverflowBitset subsumed_fields{thd->mem_root, table->s->fields};
MutableOverflowBitset applied_predicates(thd->mem_root,
graph.predicates.size());
MutableOverflowBitset subsumed_predicates(thd->mem_root,
graph.predicates.size());
for (unsigned keypart_idx = 0; keypart_idx < used_key_parts; ++keypart_idx) {
const KEY_PART_INFO &keyinfo = key->key_part[keypart_idx];
applied_fields.SetBit(keyinfo.field->field_index());
if (keypart_idx < num_exact_key_parts &&
!Overlaps(keyinfo.key_part_flag, HA_PART_KEY_SEG)) {
subsumed_fields.SetBit(keyinfo.field->field_index());
}
}
for (int predicate_idx : BitsSetIn(tree_applied_predicates)) {
Item *condition = graph.predicates[predicate_idx].condition;
bool any_not_applied =
WalkItem(condition, enum_walk::POSTFIX, [&applied_fields](Item *item) {
return item->type() == Item::FIELD_ITEM &&
!IsBitSet(down_cast<Item_field *>(item)->field->field_index(),
applied_fields);
});
if (any_not_applied) {
continue;
}
applied_predicates.SetBit(predicate_idx);
if (IsBitSet(predicate_idx, tree_subsumed_predicates)) {
bool any_not_subsumed = WalkItem(
condition, enum_walk::POSTFIX, [&subsumed_fields](Item *item) {
return item->type() == Item::FIELD_ITEM &&
!IsBitSet(
down_cast<Item_field *>(item)->field->field_index(),
subsumed_fields);
});
if (!any_not_subsumed) {
subsumed_predicates.SetBit(predicate_idx);
}
}
}
*applied_predicates_out = std::move(applied_predicates);
*subsumed_predicates_out = std::move(subsumed_predicates);
}
struct PossibleRangeScan {
unsigned idx;
unsigned mrr_flags;
unsigned mrr_buf_size;
unsigned used_key_parts;
double cost;
ha_rows num_rows;
bool is_ror_scan;
bool is_imerge_scan;
OverflowBitset applied_predicates;
OverflowBitset subsumed_predicates;
Quick_ranges ranges;
};
bool CollectPossibleRangeScans(
THD *thd, SEL_TREE *tree, RANGE_OPT_PARAM *param,
OverflowBitset tree_applied_predicates,
OverflowBitset tree_subsumed_predicates, const JoinHypergraph &graph,
Mem_root_array<PossibleRangeScan> *possible_scans) {
for (unsigned idx = 0; idx < param->keys; idx++) {
SEL_ROOT *root = tree->keys[idx];
if (root == nullptr || root->type == SEL_ROOT::Type::MAYBE_KEY ||
root->root->maybe_flag) {
continue;
}
const uint keynr = param->real_keynr[idx];
const bool covering_index = param->table->covering_keys.is_set(keynr);
unsigned mrr_flags, buf_size;
Cost_estimate cost;
bool is_ror_scan, is_imerge_scan;
// NOTE: We give in ORDER_NOT_RELEVANT now, but will re-run with
// ORDER_ASC/ORDER_DESC when actually proposing the index, if that
// yields an interesting order.
ha_rows num_rows = check_quick_select(
thd, param, idx, covering_index, root, /*update_tbl_stats=*/true,
ORDER_NOT_RELEVANT, /*skip_records_in_range=*/false, &mrr_flags,
&buf_size, &cost, &is_ror_scan, &is_imerge_scan);
if (num_rows == HA_POS_ERROR) {
continue;
}
// TODO(sgunders): See if we could have had a pre-filtering mechanism
// that allowed us to skip extracting these ranges if the path would
// obviously too high cost. As it is, it's a bit hard to just propose
// the path and see if it came in, since we need e.g. num_exact_key_parts
// as an output from this call, and that in turn affects filter cost.
Quick_ranges ranges(param->return_mem_root);
unsigned used_key_parts, num_exact_key_parts;
if (get_ranges_from_tree(param->return_mem_root, param->table,
param->key[idx], keynr, root, MAX_REF_PARTS,
&used_key_parts, &num_exact_key_parts, &ranges)) {
return true;
}
KEY *key = ¶m->table->key_info[keynr];
PossibleRangeScan scan;
scan.idx = idx;
scan.mrr_flags = mrr_flags;
scan.mrr_buf_size = buf_size;
scan.used_key_parts = used_key_parts;
scan.cost = cost.total_cost();
scan.num_rows = num_rows;
scan.is_ror_scan = is_ror_scan;
scan.is_imerge_scan = is_imerge_scan;
scan.ranges = std::move(ranges);
FindAppliedAndSubsumedPredicatesForRangeScan(
thd, key, used_key_parts, num_exact_key_parts, param->table,
tree_applied_predicates, tree_subsumed_predicates, graph,
&scan.applied_predicates, &scan.subsumed_predicates);
possible_scans->push_back(std::move(scan));
}
return false;
}