forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_physical_plan.cpp
More file actions
1591 lines (1519 loc) · 61.9 KB
/
Copy pathob_physical_plan.cpp
File metadata and controls
1591 lines (1519 loc) · 61.9 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) 2025 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define USING_LOG_PREFIX SQL_ENG
#include "ob_physical_plan.h"
#include "sql/engine/ob_operator_factory.h"
#include "share/ob_truncated_string.h"
#include "sql/code_generator/ob_static_engine_cg.h"
#include "sql/monitor/ob_sql_plan.h"
namespace oceanbase
{
using namespace common;
using namespace common::serialization;
using namespace share::schema;
using namespace lib;
namespace sql
{
ObPhysicalPlan::ObPhysicalPlan(MemoryContext &mem_context /* = CURRENT_CONTEXT */)
: ObPlanCacheObject(ObLibCacheNameSpace::NS_CRSR, mem_context),
phy_hint_(),
root_op_spec_(NULL),
param_count_(0),
signature_(0),
field_columns_(mem_context->get_arena_allocator()),
param_columns_(mem_context->get_arena_allocator()),
returning_param_columns_(mem_context->get_arena_allocator()),
autoinc_params_(allocator_),
stmt_need_privs_(allocator_),
vars_(allocator_),
sql_expression_factory_(allocator_),
expr_op_factory_(allocator_),
literal_stmt_type_(stmt::T_NONE),
plan_type_(OB_PHY_PLAN_UNINITIALIZED),
location_type_(OB_PHY_PLAN_UNINITIALIZED),
require_local_execution_(false),
use_px_(false),
px_dop_(0),
px_parallel_rule_(PXParallelRule::USE_PX_DEFAULT),
next_phy_operator_id_(0),
next_expr_operator_id_(0),
regexp_op_count_(0),
like_op_count_(0),
px_exchange_out_op_count_(0),
is_sfu_(false),
is_contains_assignment_(false),
affected_last_insert_id_(false),
is_affect_found_row_(false),
has_top_limit_(false),
is_wise_join_(false),
contain_table_scan_(false),
has_nested_sql_(false),
session_id_(0),
immediate_refresh_external_table_ids_(allocator_),
concurrent_num_(0),
max_concurrent_num_(ObMaxConcurrentParam::UNLIMITED),
table_locations_(allocator_),
das_table_locations_(allocator_),
row_param_map_(allocator_),
is_update_uniq_index_(false),
contain_index_location_(false),
base_constraints_(allocator_),
strict_constrinats_(allocator_),
non_strict_constrinats_(allocator_),
expr_frame_info_(allocator_),
stat_(),
op_stats_(),
need_drive_dml_query_(false),
var_init_exprs_(allocator_),
is_returning_(false),
is_late_materialized_(false),
is_dep_base_table_(false),
is_insert_select_(false),
is_plain_insert_(false),
flashback_query_items_(allocator_),
contain_paramed_column_field_(false),
first_array_index_(OB_INVALID_INDEX),
need_consistent_snapshot_(true),
is_batched_multi_stmt_(false),
is_new_engine_(false),
use_pdml_(false),
use_temp_table_(false),
has_link_table_(false),
has_link_sfd_(false),
has_link_udf_(false),
need_serial_exec_(false),
temp_sql_can_prepare_(false),
is_need_trans_(false),
batch_size_(0),
contain_pl_udf_or_trigger_(false),
ddl_schema_version_(0),
ddl_table_id_(0),
ddl_execution_id_(-1),
ddl_task_id_(0),
is_packed_(false),
has_instead_of_trigger_(false),
min_cluster_version_(GET_MIN_CLUSTER_VERSION()),
need_record_plan_info_(false),
enable_append_(false),
append_table_id_(0),
logical_plan_(),
use_rich_format_(false),
subschema_ctx_(allocator_),
das_dop_(0),
disable_auto_memory_mgr_(false),
is_inner_sql_(false),
is_batch_params_execute_(false),
all_local_session_vars_(&allocator_),
udf_has_dml_stmt_(false),
mview_ids_(&allocator_),
enable_inc_direct_load_(false),
enable_replace_(false),
insert_overwrite_(false),
online_sample_percent_(1.),
can_set_feedback_info_(true),
need_switch_to_table_lock_worker_(false),
data_complement_gen_doc_id_(false),
dml_table_ids_(&allocator_),
direct_load_need_sort_(false),
insertup_can_do_gts_opt_(false),
px_node_policy_(ObPxNodePolicy::INVALID),
px_node_addrs_(&allocator_),
px_node_count_(ObPxNodeHint::UNSET_PX_NODE_COUNT),
px_worker_share_plan_enabled_(false)
{
}
ObPhysicalPlan::~ObPhysicalPlan()
{
destroy();
}
void ObPhysicalPlan::reset()
{
ObPlanCacheObject::reset();
phy_hint_.reset();
root_op_spec_ = NULL;
param_count_ = 0;
signature_ = 0;
field_columns_.reset();
param_columns_.reset();
returning_param_columns_.reset();
autoinc_params_.reset();
stmt_need_privs_.reset();
vars_.reset();
sql_expression_factory_.destroy();
expr_op_factory_.destroy();
literal_stmt_type_ = stmt::T_NONE;
plan_type_ = OB_PHY_PLAN_UNINITIALIZED;
location_type_ = OB_PHY_PLAN_UNINITIALIZED;
require_local_execution_ = false;
use_px_ = false;
px_dop_ = 0;
next_phy_operator_id_ = 0;
next_expr_operator_id_ = 0;
regexp_op_count_ = 0;
like_op_count_ = 0;
px_exchange_out_op_count_ = 0;
is_sfu_ = false;
is_contain_virtual_table_ = false;
is_contain_inner_table_ = false;
is_contains_assignment_ = false;
affected_last_insert_id_ = false;
is_affect_found_row_ = false;
has_top_limit_ = false;
is_wise_join_ = false;
contain_table_scan_ = false;
has_nested_sql_ = false;
session_id_ = 0;
immediate_refresh_external_table_ids_.reset();
concurrent_num_ = 0;
max_concurrent_num_ = ObMaxConcurrentParam::UNLIMITED;
is_update_uniq_index_ = false;
contain_index_location_ = false;
ObPlanCacheObject::reset();
is_returning_ = false;
is_late_materialized_ = false;
is_dep_base_table_ = false;
is_insert_select_ = false;
is_plain_insert_ = false;
base_constraints_.reset();
strict_constrinats_.reset();
non_strict_constrinats_.reset();
flashback_query_items_.reset();
contain_paramed_column_field_ = false;
first_array_index_ = OB_INVALID_INDEX;
need_consistent_snapshot_ = true;
is_batched_multi_stmt_ = false;
temp_sql_can_prepare_ = false;
is_new_engine_ = false;
#ifndef NDEBUG
bit_set_.reset();
#endif
use_pdml_ = false;
use_temp_table_ = false;
has_link_table_ = false;
has_link_sfd_ = false;
need_serial_exec_ = false;
batch_size_ = 0;
contain_pl_udf_or_trigger_ = false;
is_packed_ = false;
has_instead_of_trigger_ = false;
enable_append_ = false;
use_rich_format_ = false;
append_table_id_ = 0;
stat_.expected_worker_map_.destroy();
stat_.minimal_worker_map_.destroy();
need_record_plan_info_ = false;
logical_plan_.reset();
subschema_ctx_.reset();
das_dop_ = 0;
all_local_session_vars_.reset();
sql_stat_record_value_.reset();
udf_has_dml_stmt_ = false;
is_inner_sql_ = false;
is_batch_params_execute_ = false;
mview_ids_.reset();
enable_inc_direct_load_ = false;
enable_replace_ = false;
insert_overwrite_ = false;
online_sample_percent_ = 1.;
can_set_feedback_info_.store(true);
need_switch_to_table_lock_worker_ = false;
data_complement_gen_doc_id_ = false;
dml_table_ids_.reset();
direct_load_need_sort_ = false;
insertup_can_do_gts_opt_ = false;
px_node_policy_ = ObPxNodePolicy::INVALID;
px_node_count_ = ObPxNodeHint::UNSET_PX_NODE_COUNT;
px_node_addrs_.reset();
px_worker_share_plan_enabled_ = false;
}
void ObPhysicalPlan::destroy()
{
#ifndef NDEBUG
bit_set_.reset();
#endif
sql_expression_factory_.destroy();
expr_op_factory_.destroy();
stat_.expected_worker_map_.destroy();
stat_.minimal_worker_map_.destroy();
subschema_ctx_.destroy();
}
int ObPhysicalPlan::set_vars(const common::ObIArray<ObVarInfo> &vars)
{
int ret = OB_SUCCESS;
int64_t N = vars.count();
if (N > 0 && OB_FAIL(vars_.reserve(N))) {
OB_LOG(WARN, "fail to reserve vars", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {
const ObVarInfo &var_info = vars.at(i);
ObVarInfo clone_var_info;
if (OB_FAIL(var_info.deep_copy(allocator_, clone_var_info))) {
LOG_WARN("fail to deep copy var info", K(ret), K(var_info));
} else if (OB_FAIL(vars_.push_back(clone_var_info))) {
// deep_copy when only ObString objects were written, ObString objects can be completely released when allocator_ is destructed, therefore there is no need to call the destructor of ObString here
LOG_WARN("fail to push back vars", K(ret), K(clone_var_info));
}
}
return ret;
}
int ObPhysicalPlan::init_params_info_str()
{
int ret = common::OB_SUCCESS;
int64_t N = params_info_.count();
int64_t buf_len = N * ObParamInfo::MAX_STR_DES_LEN + 1;
int64_t pos = 0;
char *buf = (char *)allocator_.alloc(buf_len);
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
SQL_PC_LOG(WARN, "fail to alloc memory for param info", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < N; i++) {
if (N - 1 != i) {
if (OB_FAIL(databuff_printf(buf, buf_len, pos, "{%d,%d,%d,%d,%d},",
params_info_.at(i).flag_.need_to_check_type_,
params_info_.at(i).flag_.need_to_check_bool_value_,
params_info_.at(i).flag_.expected_bool_value_,
params_info_.at(i).scale_,
params_info_.at(i).type_))) {
SQL_PC_LOG(WARN, "fail to buff_print param info", K(ret));
}
} else {
if (OB_FAIL(databuff_printf(buf, buf_len, pos, "{%d,%d,%d,%d,%d}",
params_info_.at(i).flag_.need_to_check_type_,
params_info_.at(i).flag_.need_to_check_bool_value_,
params_info_.at(i).flag_.expected_bool_value_,
params_info_.at(i).scale_,
params_info_.at(i).type_))) {
SQL_PC_LOG(WARN, "fail to buff_print param info", K(ret));
}
}
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(ob_write_string(allocator_, ObString(pos, buf), stat_.param_infos_))) {
SQL_PC_LOG(WARN, "fail to deep copy param infos", K(ret));
}
}
return ret;
}
int ObPhysicalPlan::set_field_columns(const ColumnsFieldArray &fields)
{
int ret = OB_SUCCESS;
ObField field;
WITH_CONTEXT(mem_context_) {
int64_t N = fields.count();
if (N > 0 && OB_FAIL(field_columns_.reserve(N))) {
OB_LOG(WARN, "fail to reserve field column", K(ret));
}
for (int i = 0; OB_SUCC(ret) && i < N; ++i) {
const ObField &ofield = fields.at(i);
LOG_DEBUG("ofield info", K(ofield));
if (!contain_paramed_column_field_ && ofield.is_paramed_select_item_) {
if (OB_ISNULL(ofield.paramed_ctx_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid paramed ctx", K(ofield.paramed_ctx_), K(i));
} else if (ofield.paramed_ctx_->param_idxs_.count() > 0) {
contain_paramed_column_field_ = true;
}
}
if (OB_FAIL(ret)) {
// do nothing
} else if (OB_FAIL(field.deep_copy(ofield, &allocator_))) {
LOG_WARN("deep copy field failed", K(ret));
} else if (OB_FAIL(field_columns_.push_back(field))) {
LOG_WARN("push back field columns failed", K(ret));
} else {
LOG_DEBUG("succ to push back field columns", K(field));
}
}
}
return ret;
}
int ObPhysicalPlan::set_param_fields(const common::ParamsFieldArray ¶ms)
{
int ret = OB_SUCCESS;
int64_t N = params.count();
WITH_CONTEXT(mem_context_) {
if(N > 0 && OB_FAIL(param_columns_.reserve(N))) {
LOG_WARN("failed to reserved param field", K(ret));
}
ObField tmp_field;
for (int i = 0; OB_SUCC(ret) && i < N; ++i) {
const ObField ¶m_field = params.at(i);
if (OB_FAIL(tmp_field.deep_copy(param_field, &allocator_))) {
LOG_WARN("deep copy field failed", K(ret));
} else if (OB_FAIL(param_columns_.push_back(tmp_field))) {
LOG_WARN("push back field columns failed", K(ret));
}
}
}
return ret;
}
int ObPhysicalPlan::set_returning_param_fields(const common::ParamsFieldArray ¶ms)
{
int ret = OB_SUCCESS;
int64_t N = params.count();
WITH_CONTEXT(mem_context_) {
if(N > 0 && OB_FAIL(returning_param_columns_.reserve(N))) {
LOG_WARN("failed to reserved returning param field", K(ret));
}
ObField tmp_field;
for (int i = 0; OB_SUCC(ret) && i < N; ++i) {
const ObField ¶m_field = params.at(i);
if (OB_FAIL(tmp_field.deep_copy(param_field, &allocator_))) {
LOG_WARN("deep copy field failed", K(ret));
} else if (OB_FAIL(returning_param_columns_.push_back(tmp_field))) {
LOG_WARN("push back field columns failed", K(ret));
}
}
}
return ret;
}
int ObPhysicalPlan::set_autoinc_params(const ObIArray<share::AutoincParam> &autoinc_params)
{
return autoinc_params_.assign(autoinc_params);
}
int ObPhysicalPlan::set_stmt_need_privs(const ObStmtNeedPrivs& stmt_need_privs)
{
int ret = OB_SUCCESS;
stmt_need_privs_.reset();
if (OB_FAIL(stmt_need_privs_.deep_copy(stmt_need_privs, allocator_))) {
LOG_WARN("Failed to deep copy ObStmtNeedPrivs", K_(stmt_need_privs));
}
return ret;
}
void ObPhysicalPlan::inc_large_querys()
{
ATOMIC_INC(&(stat_.large_querys_));
}
void ObPhysicalPlan::inc_delayed_large_querys()
{
ATOMIC_INC(&(stat_.delayed_large_querys_));
}
void ObPhysicalPlan::inc_delayed_px_querys()
{
ATOMIC_INC(&(stat_.delayed_px_querys_));
}
bool ObPhysicalPlan::is_stmt_modify_trans() const
{
return is_sfu_ || ObStmt::is_dml_write_stmt(stmt_type_);
}
int ObPhysicalPlan::init_operator_stats()
{
int ret = OB_SUCCESS;
if (OB_FAIL(op_stats_.init(&allocator_, next_phy_operator_id_))) {
LOG_WARN("fail to init op_stats", K(ret));
}
return ret;
}
void ObPhysicalPlan::update_plan_stat(const ObAuditRecordData &record,
const bool is_first,
const ObIArray<ObTableRowCount> *table_row_count_list,
const AdaptivePCConf *adpt_pc_conf)
{
const int64_t current_time = ObClockGenerator::getClock();
int64_t execute_count = 0;
if (record.is_timeout()) {
ATOMIC_INC(&(stat_.timeout_count_));
ATOMIC_AAF(&(stat_.total_process_time_), record.get_process_time());
}
if (!GCONF.enable_perf_event) { // short route
if (nullptr != adpt_pc_conf) {
update_adaptive_pc_info(record, adpt_pc_conf);
}
ATOMIC_AAF(&(stat_.elapsed_time_), record.get_elapsed_time());
ATOMIC_AAF(&(stat_.cpu_time_), record.get_elapsed_time() - record.exec_record_.wait_time_end_
- (record.exec_timestamp_.run_ts_ - record.exec_timestamp_.receive_ts_));
if (is_first) {
ATOMIC_STORE(&(stat_.hit_count_), 0);
} else {
ATOMIC_INC(&(stat_.hit_count_));
}
} else { // long route stat begin
if (nullptr != adpt_pc_conf) {
update_adaptive_pc_info(record, adpt_pc_conf);
}
execute_count = ATOMIC_AAF(&stat_.execute_times_, 1);
ATOMIC_AAF(&(stat_.total_process_time_), record.get_process_time());
ATOMIC_AAF(&(stat_.disk_reads_), record.exec_record_.get_io_read_count());
ATOMIC_AAF(&(stat_.direct_writes_), record.exec_record_.get_io_write_count());
ATOMIC_AAF(&(stat_.buffer_gets_), 2 * record.exec_record_.get_row_cache_hit()
+ 2 * record.exec_record_.get_fuse_row_cache_hit()
+ 2 * record.exec_record_.get_bloom_filter_filts()
+ record.exec_record_.get_block_cache_hit()
+ record.exec_record_.get_io_read_count());
ATOMIC_AAF(&(stat_.application_wait_time_), record.exec_record_.get_application_time());
ATOMIC_AAF(&(stat_.concurrency_wait_time_), record.exec_record_.get_concurrency_time());
ATOMIC_AAF(&(stat_.user_io_wait_time_), record.exec_record_.get_user_io_time());
ATOMIC_AAF(&(stat_.rows_processed_), record.return_rows_ + record.affected_rows_);
ATOMIC_AAF(&(stat_.elapsed_time_), record.get_elapsed_time());
ATOMIC_AAF(&(stat_.cpu_time_), record.get_elapsed_time() - record.exec_record_.wait_time_end_
- (record.exec_timestamp_.run_ts_ - record.exec_timestamp_.receive_ts_));
// ATOMIC_STORE(&(stat_.expected_worker_count_), record.expected_worker_cnt_);
if (is_first) {
ATOMIC_STORE(&(stat_.hit_count_), 0);
} else {
ATOMIC_INC(&(stat_.hit_count_));
}
if (record.get_elapsed_time() > GCONF.trace_log_slow_query_watermark) {
ATOMIC_INC(&(stat_.slow_count_));
}
int64_t slowest_usec = ATOMIC_LOAD(&stat_.slowest_exec_usec_);
if (slowest_usec < record.get_elapsed_time()) {
ATOMIC_STORE(&(stat_.slowest_exec_usec_), record.get_elapsed_time());
ATOMIC_STORE(&(stat_.slowest_exec_time_), current_time);
}
ATOMIC_STORE(&(stat_.last_active_time_), current_time);
if (stat_.is_bind_sensitive_ && execute_count > 0) {
int64_t pos = execute_count % ObPlanStat::MAX_SCAN_STAT_SIZE;
ATOMIC_STORE(&(stat_.table_scan_stat_[pos].query_range_row_count_),
record.table_scan_stat_.query_range_row_count_);
ATOMIC_STORE(&(stat_.table_scan_stat_[pos].indexback_row_count_),
record.table_scan_stat_.indexback_row_count_);
ATOMIC_STORE(&(stat_.table_scan_stat_[pos].output_row_count_),
record.table_scan_stat_.output_row_count_);
}
} // long route stat ends
if (!is_expired() && stat_.enable_plan_expiration_) {
update_plan_expired_info(record, is_first, table_row_count_list);
}
}
bool ObPhysicalPlan::check_if_is_expired_by_error(const int error_code) const
{
return common::OB_TIMEOUT == error_code
|| common::OB_TRANS_STMT_TIMEOUT == error_code
|| common::OB_SESSION_KILLED == error_code
|| common::OB_ERR_QUERY_INTERRUPTED == error_code
|| common::OB_ERR_SESSION_INTERRUPTED == error_code;
}
void ObPhysicalPlan::update_plan_expired_info(const ObAuditRecordData &record,
const bool is_first,
const ObIArray<ObTableRowCount> *table_row_count_list)
{
bool bret = false;
bool info_inited = ATOMIC_LOAD(&(stat_.first_exec_row_count_)) >= 0;
if (check_if_is_expired_by_error(record.status_)) {
set_is_expired(EXPIRED_BY_EXEC_ERROR);
LOG_INFO("query plan is expired due to execution error", K(record.status_), K(stat_));
} else if (is_first) {
ATOMIC_STORE(&(stat_.sample_times_), 0);
ATOMIC_STORE(&(stat_.first_exec_row_count_), record.exec_record_.get_memstore_read_row_count() + record.exec_record_.get_ssstore_read_row_count());
ATOMIC_STORE(&(stat_.first_exec_usec_), record.exec_timestamp_.executor_t_);
if (stat_.table_row_count_first_exec_ != NULL && table_row_count_list != NULL) {
fill_row_count_info(true, stat_.access_table_num_, stat_.table_row_count_first_exec_, *table_row_count_list);
}
} else if (!info_inited) {
/* finish evolution, init use sampling infos */
int64_t first_exec_row_count = 0;
do {
first_exec_row_count = ATOMIC_LOAD(&(stat_.first_exec_row_count_));
} while (first_exec_row_count != ATOMIC_VCAS(&(stat_.first_exec_row_count_), first_exec_row_count, 0));
if (-1 == first_exec_row_count) { // only one thread can init first exec infos by get sample_count
int64_t sample_count = ATOMIC_LOAD(&(stat_.sample_times_));
if (sample_count <= 0) {
sample_count = 1;
}
stat_.first_exec_row_count_ = stat_.sample_exec_row_count_ / sample_count;
stat_.first_exec_usec_ = stat_.sample_exec_usec_ / sample_count;
ATOMIC_STORE(&(stat_.sample_exec_row_count_), 0);
ATOMIC_STORE(&(stat_.sample_exec_usec_), 0);
ATOMIC_STORE(&(stat_.sample_times_), 0);
if (stat_.table_row_count_first_exec_ != NULL && table_row_count_list != NULL && sample_count > 0) {
int64_t max_index = std::min(stat_.access_table_num_, OB_MAX_TABLE_NUM_PER_STMT);
for (int64_t i = 0; i < max_index; ++i) {
if (stat_.table_row_count_first_exec_[i].row_count_ >= 0) {
stat_.table_row_count_first_exec_[i].row_count_ /= sample_count;
}
LOG_DEBUG("init first row stat for spm plan", K(i), K(stat_.table_row_count_first_exec_[i]));
}
}
LOG_DEBUG("init first exec info for spm plan", K(sample_count), K(stat_.first_exec_row_count_), K(stat_.first_exec_usec_));
}
} else if (stat_.table_row_count_first_exec_ != NULL && table_row_count_list != NULL
&& record.get_elapsed_time() > SLOW_QUERY_TIME_FOR_PLAN_EXPIRE
&& check_if_is_expired(record.get_elapsed_time(), stat_.access_table_num_, stat_.table_row_count_first_exec_, *table_row_count_list)) {
/* expire plan by range scan row count */
set_is_expired(EXPIRED_BY_TABLE_ACCESS_ROW_COUNT);
} else {
/* expire plan by local plan row count and dist plan exec time */
int64_t sample_count = ATOMIC_AAF(&(stat_.sample_times_), 1);
int64_t sample_exec_row_count = ATOMIC_AAF(&(stat_.sample_exec_row_count_),
record.exec_record_.get_memstore_read_row_count() + record.exec_record_.get_ssstore_read_row_count());
int64_t sample_exec_usec = ATOMIC_AAF(&(stat_.sample_exec_usec_), record.exec_timestamp_.executor_t_);
if (sample_count >= SLOW_QUERY_SAMPLE_SIZE) {
ATOMIC_STORE(&(stat_.sample_times_), 0);
ATOMIC_STORE(&(stat_.sample_exec_row_count_), 0);
ATOMIC_STORE(&(stat_.sample_exec_usec_), 0);
if (is_plan_unstable(sample_count, sample_exec_row_count, sample_exec_usec)) {
set_is_expired(EXPIRED_BY_EXEC_TIME);
if (stat_.elapsed_time_ > SLOW_QUERY_TIME_FOR_PLAN_EXPIRE * stat_.execute_times_) {
LOG_INFO("plan expired for physical plan avg elapsed_time more than 5ms", K(stat_.plan_id_),
K(stat_.elapsed_time_), K(stat_.execute_times_));
} else {
LOG_INFO("plan expired for physical plan avg elapsed_time no more than 5ms", K(stat_.plan_id_),
K(stat_.elapsed_time_), K(stat_.execute_times_));
}
}
}
}
}
void ObPhysicalPlan::fill_row_count_info(const bool is_first,
const int64_t access_table_num,
ObTableRowCount *table_row_count_first_exec,
const ObIArray<ObTableRowCount> &table_row_count_list)
{
int64_t max_index = std::min(access_table_num, std::min(table_row_count_list.count(), OB_MAX_TABLE_NUM_PER_STMT));
if (max_index <= 0) {
/* do nothing */
} else if (is_first || OB_INVALID_ID == ATOMIC_LOAD(&table_row_count_first_exec[0].op_id_)) {
for (int64_t i = 0; i < max_index; ++i) {
ATOMIC_STORE(&(table_row_count_first_exec[i].op_id_), table_row_count_list.at(i).op_id_);
ATOMIC_STORE(&(table_row_count_first_exec[i].row_count_), table_row_count_list.at(i).row_count_);
LOG_DEBUG("first add row stat", K(table_row_count_list.at(i)));
}
} else {
bool finish = false;
for (int64_t i = 0; i < max_index; ++i) {
finish = false;
for (int64_t j = 0; !finish && j < max_index; ++j) {
if (table_row_count_list.at(j).op_id_ == table_row_count_first_exec[i].op_id_) {
finish = true;
ATOMIC_AAF(&(table_row_count_first_exec[i].row_count_), table_row_count_list.at(j).row_count_);
}
}
}
}
}
bool ObPhysicalPlan::check_if_is_expired(const int64_t elapsed_time,
const int64_t access_table_num,
const ObTableRowCount *table_row_count_first_exec,
const ObIArray<ObTableRowCount> &table_row_count_list)
{
bool bret = false;
int64_t max_index = std::min(access_table_num, std::min(table_row_count_list.count(), OB_MAX_TABLE_NUM_PER_STMT));
for (int64_t i = 0; !bret && i < max_index; ++i) {
for (int64_t j = 0; !bret && j < max_index; ++j) {
// Some scenarios, such as parallel execution, the order of row information stored in the table may be different for different executions
if (table_row_count_list.at(i).op_id_ == table_row_count_first_exec[j].op_id_) {
int64_t first_exec_row_count = ATOMIC_LOAD(&table_row_count_first_exec[j].row_count_);
if (inner_check_if_is_expired(first_exec_row_count, table_row_count_list.at(i).row_count_)) {
bret = true;
LOG_INFO("plan is expired", K(first_exec_row_count),
K(table_row_count_list.at(i)),
"current_elapsed_time", elapsed_time,
"plan_stat", stat_);
}
} // for max_index end
} // for max_index end
}
return bret;
}
bool ObPhysicalPlan::is_plan_unstable(const int64_t sample_count,
const int64_t sample_exec_row_count,
const int64_t sample_exec_usec)
{
bool bret = false;
if (sample_exec_usec <= SLOW_QUERY_TIME_FOR_PLAN_EXPIRE * sample_count) {
// sample query is fast query in the average
} else if (OB_PHY_PLAN_LOCAL == plan_type_) {
int64_t first_query_range_rows = ATOMIC_LOAD(&stat_.first_exec_row_count_);
if (sample_exec_row_count <= SLOW_QUERY_ROW_COUNT_THRESOLD * sample_count) {
// the sample query does not accesses too many rows in the average
} else if (sample_exec_row_count / sample_count > first_query_range_rows * 10) {
// the average sample query range row count increases great
bret = true;
LOG_INFO("local query plan is expired due to unstable performance",
K(first_query_range_rows), K(sample_exec_row_count), K(sample_count), K(stat_));
}
} else if ( OB_PHY_PLAN_DISTRIBUTED == plan_type_) {
int64_t first_exec_usec = ATOMIC_LOAD(&stat_.first_exec_usec_);
if (sample_exec_usec / sample_count > first_exec_usec * 2) {
// the average sample query execute time increases great
bret = true;
LOG_INFO("distribute query plan is expired due to unstable performance",
K(first_exec_usec), K(sample_exec_usec), K(sample_count), K(stat_));
}
} else {
// do nothing
}
return bret;
}
/**
* Currently, 3 metrics are used to evict plans, and only when all 3 conditions are met will the plan be evicted:
* 1. The current number of rows exceeds the threshold (100 rows)
* 2. Execution time exceeds the threshold (5ms)
* 3. The ratio of table scan rows to original scan rows exceeds the threshold (2 times)
*
* The reason for setting the current row count threshold is that the table scan function does not guarantee incrementality.
* In scenarios with frequent insertions and deletions, the original table scan function might remain at a low value,
* leading to frequent plan eviction.
* Setting a threshold can significantly alleviate the frequency of plan eviction.
*/
inline bool ObPhysicalPlan::inner_check_if_is_expired(const int64_t first_exec_row_count,
const int64_t current_row_count) const
{
bool ret_bool = false;
if (first_exec_row_count < 0) {
/* do nothing */
} else if (current_row_count <= EXPIRED_PLAN_TABLE_ROW_THRESHOLD) { // 100 rows
ret_bool = false;
} else {
ret_bool = ((first_exec_row_count == 0 && current_row_count > 0)
|| (first_exec_row_count > 0 && current_row_count / first_exec_row_count > TABLE_ROW_CHANGE_THRESHOLD));
}
return ret_bool;
}
int ObPhysicalPlan::inc_concurrent_num()
{
int ret = OB_SUCCESS;
int64_t concurrent_num = 0;
int64_t new_num = 0;
bool is_succ = false;
if (max_concurrent_num_ == ObMaxConcurrentParam::UNLIMITED) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("current physical plan is unlimit", K(ret), K(max_concurrent_num_));
} else {
while(OB_SUCC(ret) && false == is_succ) {
concurrent_num = ATOMIC_LOAD(&concurrent_num_);
if (0 == max_concurrent_num_) {
ret = OB_REACH_MAX_CONCURRENT_NUM;
} else if (concurrent_num >= max_concurrent_num_) {
ret = OB_REACH_MAX_CONCURRENT_NUM;
} else {
new_num = concurrent_num + 1;
is_succ = ATOMIC_BCAS(&concurrent_num_, concurrent_num, new_num);
}
}
}
return ret;
}
void ObPhysicalPlan::dec_concurrent_num()
{
ATOMIC_DEC(&concurrent_num_);
}
int ObPhysicalPlan::set_max_concurrent_num(int64_t max_concurrent_num)
{
int ret = OB_SUCCESS;
if (max_concurrent_num < 0) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid max_concurrent_num", K(ret), K(max_concurrent_num));
} else {
ATOMIC_STORE(&max_concurrent_num_, max_concurrent_num);
}
return ret;
}
int64_t ObPhysicalPlan::get_max_concurrent_num()
{
return ATOMIC_LOAD(&max_concurrent_num_);
}
OB_SERIALIZE_MEMBER(FlashBackQueryItem,
table_id_,
time_val_);
// Because we haven't seen the handling of force_trace_log for remote execution yet, so we will not serialize it temporarily
OB_SERIALIZE_MEMBER(ObPhysicalPlan,
tenant_schema_version_, // this field is not used at runtime
phy_hint_.query_timeout_,
phy_hint_.read_consistency_,
is_sfu_,
dependency_tables_,
param_count_,
plan_type_,
signature_,
stmt_type_,
regexp_op_count_,
literal_stmt_type_,
like_op_count_,
is_ignore_stmt_,
object_id_,
stat_.sql_id_,
is_contain_inner_table_,
is_update_uniq_index_,
dummy_string_,
is_returning_,
location_type_,
use_px_,
vars_,
px_dop_,
has_nested_sql_,
flashback_query_items_,
stat_.enable_early_lock_release_,
use_pdml_,
is_new_engine_,
use_temp_table_,
batch_size_,
need_drive_dml_query_,
is_need_trans_,
ddl_schema_version_,
ddl_table_id_,
phy_hint_.monitor_,
need_serial_exec_,
contain_pl_udf_or_trigger_,
is_packed_,
has_instead_of_trigger_,
is_plain_insert_,
ddl_execution_id_,
ddl_task_id_,
stat_.plan_id_,
min_cluster_version_,
need_record_plan_info_,
enable_append_,
append_table_id_,
subschema_ctx_,
use_rich_format_,
disable_auto_memory_mgr_,
udf_has_dml_stmt_,
stat_.format_sql_id_,
mview_ids_,
enable_inc_direct_load_,
enable_replace_,
immediate_refresh_external_table_ids_,
insert_overwrite_,
online_sample_percent_,
need_switch_to_table_lock_worker_,
data_complement_gen_doc_id_,
direct_load_need_sort_,
px_parallel_rule_,
px_node_policy_,
px_node_addrs_,
px_node_count_,
px_worker_share_plan_enabled_);
int ObPhysicalPlan::set_table_locations(const ObTablePartitionInfoArray &infos,
ObSchemaGetterGuard &schema_guard)
{
int ret = OB_SUCCESS;
table_locations_.reset();
das_table_locations_.reset();
if (OB_FAIL(table_locations_.prepare_allocate_and_keep_count(infos.count(),
allocator_))) {
LOG_WARN("fail to init table location count", K(ret));
} else if (OB_FAIL(das_table_locations_.prepare_allocate_and_keep_count(infos.count(),
allocator_))) {
LOG_WARN("fail to init das table location count", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < infos.count(); ++i) {
ObTableLocation &tl = infos.at(i)->get_table_location();
const ObTableSchema *table_schema = nullptr;
if (tl.use_das()) {
if (OB_FAIL(das_table_locations_.push_back(tl))) {
LOG_WARN("fail to push das table location", K(ret), K(i));
}
} else if (OB_FAIL(table_locations_.push_back(tl))) {
LOG_WARN("fail to push table location", K(ret), K(i));
} else if (!is_external_object_id(tl.get_ref_table_id())) {
if (OB_FAIL(schema_guard.get_table_schema(MTL_ID(), tl.get_ref_table_id(), table_schema))) {
LOG_WARN("get table schema failed", K(ret), K(tl.get_ref_table_id()));
} else {
contain_index_location_ |= table_schema->is_index_table();
}
}
LOG_DEBUG("set table location", K(tl), K(tl.use_das()));
}
return ret;
}
int ObPhysicalPlan::set_location_constraints(const ObIArray<LocationConstraint> &base_constraints,
const ObIArray<ObPwjConstraint *> &strict_constraints,
const ObIArray<ObPwjConstraint *> &non_strict_constraints,
const ObIArray<ObDupTabConstraint> &dup_table_replica_cons)
{
// deep copy location constraints
int ret = OB_SUCCESS;
if (base_constraints.count() > 0) {
base_constraints_.reset();
base_constraints_.set_allocator(&allocator_);
if (OB_FAIL(base_constraints_.init(base_constraints.count()))) {
LOG_WARN("failed to init base constraints", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < base_constraints.count(); ++i) {
if (OB_FAIL(base_constraints_.push_back(base_constraints.at(i)))) {
LOG_WARN("failed to push back element", K(ret), K(base_constraints.at(i)));
} else { /*do nothing*/ }
}
}
if (OB_SUCC(ret) && strict_constraints.count() > 0) {
strict_constrinats_.reset();
strict_constrinats_.set_allocator(&allocator_);
if (OB_FAIL(strict_constrinats_.init(strict_constraints.count()))) {
LOG_WARN("failed to init strict constraints", K(ret));
} else if (OB_FAIL(strict_constrinats_.prepare_allocate(strict_constraints.count()))) {
LOG_WARN("failed to prepare allocate location constraints", K(ret));
} else {
ObPwjConstraint *cur_cons;
for (int64_t i = 0; OB_SUCC(ret) && i < strict_constraints.count(); ++i) {
if (OB_ISNULL(cur_cons = strict_constraints.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(i));
} else if (cur_cons->count() <= 0) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected empty array", K(ret));
} else {
strict_constrinats_.at(i).reset();
strict_constrinats_.at(i).set_allocator(&allocator_);
if (OB_FAIL(strict_constrinats_.at(i).init(cur_cons->count()))) {
LOG_WARN("failed to init fixed array", K(ret));
}
for (int64_t j = 0; OB_SUCC(ret) && j < cur_cons->count(); ++j) {
if (OB_FAIL(strict_constrinats_.at(i).push_back(cur_cons->at(j)))) {
LOG_WARN("failed to push back element", K(ret), K(cur_cons->at(j)));
} else { /*do nothing*/ }
}
}
}
}
}
if (OB_SUCC(ret) && non_strict_constraints.count() > 0) {
non_strict_constrinats_.reset();
non_strict_constrinats_.set_allocator(&allocator_);
if (OB_FAIL(non_strict_constrinats_.init(non_strict_constraints.count()))) {
LOG_WARN("failed to init strict constraints", K(ret));
} else if (OB_FAIL(non_strict_constrinats_.prepare_allocate(non_strict_constraints.count()))) {
LOG_WARN("failed to prepare allocate location constraints", K(ret));
} else {
ObPwjConstraint *cur_cons;
for (int64_t i = 0; OB_SUCC(ret) && i < non_strict_constraints.count(); ++i) {
if (OB_ISNULL(cur_cons = non_strict_constraints.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(i));
} else if (cur_cons->count() <= 0) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected empty array", K(ret));
} else {
non_strict_constrinats_.at(i).reset();
non_strict_constrinats_.at(i).set_allocator(&allocator_);
if (OB_FAIL(non_strict_constrinats_.at(i).init(cur_cons->count()))) {
LOG_WARN("failed to init fixed array", K(ret));
}
for (int64_t j = 0; OB_SUCC(ret) && j < cur_cons->count(); ++j) {
if (OB_FAIL(non_strict_constrinats_.at(i).push_back(cur_cons->at(j)))) {
LOG_WARN("failed to push back element", K(ret), K(cur_cons->at(j)));
} else { /*do nothing*/ }
}
}
}
}
}
if (OB_SUCC(ret) && dup_table_replica_cons.count() > 0) {
dup_table_replica_cons_.reset();
dup_table_replica_cons_.set_allocator(&allocator_);
if (OB_FAIL(dup_table_replica_cons_.init(dup_table_replica_cons.count()))) {
LOG_WARN("failed to init duplicate table constraints", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < dup_table_replica_cons.count(); ++i) {
if(OB_FAIL(dup_table_replica_cons_.push_back(dup_table_replica_cons.at(i)))) {
LOG_WARN("failed to assign element", K(ret), K(dup_table_replica_cons.at(i)));
} else { /*do nothing*/ }
}
}
}
if (OB_FAIL(ret)) {
base_constraints_.reset();
strict_constrinats_.reset();
non_strict_constrinats_.reset();
dup_table_replica_cons_.reset();
} else {
LOG_TRACE("deep copied location constraints", K(base_constraints_), K(strict_constrinats_),
K(non_strict_constrinats_), K(dup_table_replica_cons_));
}
return ret;
}
bool ObPhysicalPlan::has_same_location_constraints(const ObPhysicalPlan &r) const
{
bool is_same = true;
const ObIArray<LocationConstraint>& l_base_cons = get_base_constraints();
const ObIArray<LocationConstraint>& r_base_cons = r.get_base_constraints();
const ObIArray<ObPlanPwjConstraint>& l_non_strict_cons = get_non_strict_constraints();
const ObIArray<ObPlanPwjConstraint>& r_non_strict_cons = r.get_non_strict_constraints();
const ObIArray<ObPlanPwjConstraint>& l_strict_cons = get_strict_constraints();
const ObIArray<ObPlanPwjConstraint>& r_strict_cons = r.get_strict_constraints();
const ObIArray<ObDupTabConstraint>& l_dup_rep_cons = get_dup_table_replica_constraints();
const ObIArray<ObDupTabConstraint>& r_dup_rep_cons = r.get_dup_table_replica_constraints();
if (l_base_cons.count() != r_base_cons.count() ||
l_strict_cons.count() != r_strict_cons.count() ||
l_non_strict_cons.count() != r_non_strict_cons.count()||
l_dup_rep_cons.count() != r_dup_rep_cons.count()) {
is_same = false;
} else {
for (int64_t i = 0; is_same && i < l_base_cons.count(); i++) {
is_same = is_same && (l_base_cons.at(i) == r_base_cons.at(i));
}
for (int64_t i = 0; is_same && i < l_strict_cons.count(); i++) {
if (l_strict_cons.at(i).count() != r_strict_cons.at(i).count()) {
is_same = false;
} else {
for (int64_t j = 0; is_same && j < l_strict_cons.at(i).count(); j++) {
is_same = (l_strict_cons.at(i).at(j) == (r_strict_cons.at(i)).at(j));