Skip to content

Commit 263e7b7

Browse files
authored
statstics: avoid unnecessary try when to sync load (#56614) (#59707)
close #56472
1 parent c75e5a7 commit 263e7b7

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

pkg/statistics/handle/handle_hist.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package handle
1616

1717
import (
18-
"fmt"
18+
stderrors "errors"
1919
"math/rand"
2020
"sync"
2121
"time"
@@ -80,7 +80,6 @@ func (h *Handle) SendLoadRequests(sc *stmtctx.StatementContext, neededHistItems
8080
}
8181
}
8282
})
83-
8483
if len(remainedItems) <= 0 {
8584
return nil
8685
}
@@ -327,6 +326,10 @@ func (h *Handle) handleOneItemTask(task *NeededItemTask) (err error) {
327326
t := time.Now()
328327
needUpdate := false
329328
wrapper, err = h.readStatsForOneItem(sctx, item, wrapper)
329+
if stderrors.Is(err, errGetHistMeta) {
330+
metrics.ReadStatsHistogram.Observe(float64(time.Since(t).Milliseconds()))
331+
return nil
332+
}
330333
if err != nil {
331334
return err
332335
}
@@ -346,6 +349,8 @@ func (h *Handle) handleOneItemTask(task *NeededItemTask) (err error) {
346349
return nil
347350
}
348351

352+
var errGetHistMeta = errors.New("fail to get stats version for this histogram")
353+
349354
// readStatsForOneItem reads hist for one column/index, TODO load data via kv-get asynchronously
350355
func (h *Handle) readStatsForOneItem(sctx sessionctx.Context, item model.TableItemID, w *statsWrapper) (*statsWrapper, error) {
351356
failpoint.Inject("mockReadStatsForOnePanic", nil)
@@ -424,9 +429,11 @@ func (h *Handle) readStatsForOneItem(sctx sessionctx.Context, item model.TableIt
424429
return nil, errors.Trace(err)
425430
}
426431
if len(rows) == 0 {
427-
logutil.BgLogger().Error("fail to get stats version for this histogram, normally this wouldn't happen, please check if this column or index has a histogram record in `mysql.stats_histogram`", zap.Int64("table_id", item.TableID),
428-
zap.Int64("hist_id", item.ID), zap.Bool("is_index", item.IsIndex))
429-
return nil, errors.Trace(fmt.Errorf("fail to get stats version for this histogram, normally this wouldn't happen, please check if this column or index has a histogram record in `mysql.stats_histogram`, table_id:%v, hist_id:%v, is_index:%v", item.TableID, item.ID, item.IsIndex))
432+
logutil.BgLogger().Error("fail to get stats version for this histogram, normally this wouldn't happen, please check if this column or index has a histogram record in `mysql.stats_histogram`",
433+
zap.Int64("table_id", item.TableID),
434+
zap.Int64("hist_id", item.ID),
435+
zap.Bool("is_index", item.IsIndex))
436+
return nil, errGetHistMeta
430437
}
431438
statsVer := rows[0].GetInt64(0)
432439
if item.IsIndex {

tests/integrationtest/r/planner/core/issuetest/planner_issue.result

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,30 @@ _tidb_rowid
592592
1
593593
2
594594
3
595+
drop table if exists t1, t2, t3, t4;
596+
CREATE TABLE t1 (a int, b int, c int);
597+
CREATE TABLE t2 (a int, b int, c int);
598+
CREATE TABLE t3 (a int, b int, c int);
599+
CREATE TABLE t4 (a int, b int, c int);
600+
INSERT INTO t1 VALUES (1,3,0), (2,2,0), (3,2,0);
601+
INSERT INTO t2 VALUES (3,3,0), (4,2,0), (5,3,0);
602+
INSERT INTO t3 VALUES (1,2,0), (2,2,0);
603+
INSERT INTO t4 VALUES (3,2,0), (4,2,0);
604+
CREATE INDEX idx_b ON t2(b);
605+
SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b
606+
FROM (t3,t4)
607+
LEFT JOIN
608+
(t1,t2)
609+
ON t3.a=1 AND t3.b=t2.b AND t2.b=t4.b order by 1, 2, 3, 4, 5;
610+
a b a b a b
611+
NULL NULL 2 2 3 2
612+
NULL NULL 2 2 4 2
613+
4 2 1 2 3 2
614+
4 2 1 2 3 2
615+
4 2 1 2 3 2
616+
4 2 1 2 4 2
617+
4 2 1 2 4 2
618+
4 2 1 2 4 2
619+
show warnings;
620+
Level Code Message
621+
drop table if exists t1, t2, t3, t4;

tests/integrationtest/t/planner/core/issuetest/planner_issue.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,21 @@ insert into t values (1, 10);
460460
insert into t values (2, 20);
461461
insert into t values (3, 30);
462462
select _tidb_rowid from t where id in (1, 2, 3);
463+
# TestIssue56472
464+
drop table if exists t1, t2, t3, t4;
465+
CREATE TABLE t1 (a int, b int, c int);
466+
CREATE TABLE t2 (a int, b int, c int);
467+
CREATE TABLE t3 (a int, b int, c int);
468+
CREATE TABLE t4 (a int, b int, c int);
469+
INSERT INTO t1 VALUES (1,3,0), (2,2,0), (3,2,0);
470+
INSERT INTO t2 VALUES (3,3,0), (4,2,0), (5,3,0);
471+
INSERT INTO t3 VALUES (1,2,0), (2,2,0);
472+
INSERT INTO t4 VALUES (3,2,0), (4,2,0);
473+
CREATE INDEX idx_b ON t2(b);
474+
SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b
475+
FROM (t3,t4)
476+
LEFT JOIN
477+
(t1,t2)
478+
ON t3.a=1 AND t3.b=t2.b AND t2.b=t4.b order by 1, 2, 3, 4, 5;
479+
show warnings;
480+
drop table if exists t1, t2, t3, t4;

0 commit comments

Comments
 (0)