【 TiDB 使用环境】生产环境 【 TiDB 版本】5.0.6 【遇到的问题】SQL性能问题 【复现路径】
做过哪些操作出现的问题`
【问题现象及影响】
连表操作,很常规的一个语句
CREATE TABLE FbAdAsset
(
Id
bigint(20) NOT NULL AUTO_INCREMENT,
date_start
datetime NOT NULL,
date_stop
datetime NOT NULL,
AssetId
varchar(128) NOT NULL,
FbAccount
varchar(128) NOT NULL,
AdCampId
varchar(128) NOT NULL,
AssetUrl
varchar(512) NOT NULL,
AssetThumbUrl
varchar(512) DEFAULT NULL,
AssetTitle
varchar(512) NOT NULL,
Amount
decimal(10,2) DEFAULT NULL,
PayNum
int(11) NOT NULL,
Installs
int(11) DEFAULT NULL,
Clicks
int(11) DEFAULT NULL,
Impressions
int(11) DEFAULT NULL,
Spend
decimal(10,2) DEFAULT NULL,
Reach
int(11) DEFAULT NULL,
Roas
decimal(10,2) NOT NULL,
IsVideo
int(11) NOT NULL,
UpdateTime
datetime NOT NULL,
CreateTime
datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (Id
) /*T![clustered_index] NONCLUSTERED */,
KEY date_start
(date_start
,date_stop
),
KEY assetid
(AssetId
),
KEY date_start_2
(date_start
,AssetId
),
KEY index_FbAccount
(FbAccount
),
KEY index_AssetTitle_IsVideo
(AssetTitle
,IsVideo
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=370170001
CREATE TABLE FbAccount
(
Id
int(11) NOT NULL AUTO_INCREMENT,
Account
varchar(128) NOT NULL,
Secret
varchar(128) DEFAULT NULL,
PageId
varchar(128) DEFAULT NULL,
AppId
varchar(128) DEFAULT NULL,
AppUrl
varchar(500) DEFAULT NULL,
CreatTime
datetime(3) NOT NULL,
ProductId
int(11) NOT NULL,
ProductName
varchar(128) NOT NULL,
Mt
int(11) DEFAULT NULL,
Token
varchar(500) DEFAULT NULL,
InsId
varchar(500) DEFAULT NULL,
Status
int(11) NOT NULL,
AutoFillAd
int(11) NOT NULL,
UpdateStatus
int(11) NOT NULL,
Chl
varchar(128) DEFAULT NULL,
Core
int(11) NOT NULL,
FbAdRuleId
int(11) NOT NULL,
AdAutoActive
int(11) NOT NULL,
StatusChangeTime
datetime(3) NOT NULL,
FbAccountType
int(11) NOT NULL,
RowVersion
bigint(20) DEFAULT NULL,
SpendCap
bigint(20) DEFAULT ‘0’,
AmountSpent
bigint(20) DEFAULT ‘0’,
PutProductId
int(11) NOT NULL DEFAULT ‘0’,
CurrentLanguage2
int(11) DEFAULT NULL,
AccountAdType
int(11) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (Id
) /*T![clustered_index] CLUSTERED */,
UNIQUE KEY FbAccount_Account_IDX
(Account
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=3810145
查询语句:
EXPLAIN
select ad.*,act.ProductId,act.Mt,act.Core,act.CurrentLanguage2 from FbAdAsset
ad
INNER JOIN FbAccount
act ON ad
.FbAccount
= act
.Account
where date_start
>= ‘2022-06-30’ AND date_start
<= ‘2022-07-07’
AND Core = 1
AND ProductId
= 3366 AND Mt = 1
limit 100
执行计划
Limit_15 | 0.67 | root | offset:0, count:100 | |
---|---|---|---|---|
└─Projection_17 | 0.67 | root | db1.fbadasset.id, db1.fbadasset.date_start, db1.fbadasset.date_stop, db1.fbadasset.assetid, db1.fbadasset.fbaccount, db1.fbadasset.adcampid, db1.fbadasset.asseturl, db1.fbadasset.assetthumburl, db1.fbadasset.assettitle, db1.fbadasset.amount, db1.fbadasset.paynum, db1.fbadasset.installs, db1.fbadasset.clicks, db1.fbadasset.impressions, db1.fbadasset.spend, db1.fbadasset.reach, db1.fbadasset.roas, db1.fbadasset.isvideo, db1.fbadasset.updatetime, db1.fbadasset.createtime, db1.fbaccount.productid, db1.fbaccount.mt, db1.fbaccount.core, db1.fbaccount.currentlanguage2 | |
└─IndexHashJoin_26 | 0.67 | root | inner join, inner:IndexLookUp_23, outer key:db1.fbaccount.account, inner key:db1.fbadasset.fbaccount, equal cond:eq(db1.fbaccount.account, db1.fbadasset.fbaccount) | |
├─TableReader_73(Build) | 0.00 | root | data:Selection_72 | |
│ └─Selection_72 | 0.00 | cop[tiflash] | eq(db1.fbaccount.core, 1), eq(db1.fbaccount.mt, 1), eq(db1.fbaccount.productid, 3366) | |
│ └─TableFullScan_71 | 958.00 | cop[tiflash] | table:act | keep order:false, stats:pseudo |
└─IndexLookUp_23(Probe) | 701967.61 | root | ||
├─IndexRangeScan_20(Build) | 37026963.56 | cop[tikv] | table:ad, index:index_FbAccount(FbAccount) | range: decided by [eq(db1.fbadasset.fbaccount, db1.fbaccount.account)], keep order:false |
└─Selection_22(Probe) | 701967.61 | cop[tikv] | ge(db1.fbadasset.date_start, 2022-06-30 00:00:00.000000), le(db1.fbadasset.date_start, 2022-07-07 00:00:00.000000) | |
└─TableRowIDScan_21 | 37026963.56 | cop[tikv] | table:ad | keep order:false |
但是 只有使用一个条件的时候,就没有这个 range: decided by [eq(db1.fbadasset.fbaccount, db1.fbaccount.account)], keep order:false 这个看到estRows 返回了 3000多万行
------语句:
EXPLAIN
select ad.*,act.ProductId,act.Mt,act.Core,act.CurrentLanguage2 from FbAdAsset
ad
INNER JOIN FbAccount
act ON ad
.FbAccount
= act
.Account
where date_start
>= ‘2022-06-30’ AND date_start
<= ‘2022-07-07’
AND Core = 1
执行计划:
Projection_10 | 672484.97 | root | db1.fbadasset.id, db1.fbadasset.date_start, db1.fbadasset.date_stop, db1.fbadasset.assetid, db1.fbadasset.fbaccount, db1.fbadasset.adcampid, db1.fbadasset.asseturl, db1.fbadasset.assetthumburl, db1.fbadasset.assettitle, db1.fbadasset.amount, db1.fbadasset.paynum, db1.fbadasset.installs, db1.fbadasset.clicks, db1.fbadasset.impressions, db1.fbadasset.spend, db1.fbadasset.reach, db1.fbadasset.roas, db1.fbadasset.isvideo, db1.fbadasset.updatetime, db1.fbadasset.createtime, db1.fbaccount.productid, db1.fbaccount.mt, db1.fbaccount.core, db1.fbaccount.currentlanguage2 | |
---|---|---|---|---|
└─HashJoin_42 | 672484.97 | root | inner join, equal:[eq(db1.fbaccount.account, db1.fbadasset.fbaccount)] | |
├─TableReader_66(Build) | 0.96 | root | data:Selection_65 | |
│ └─Selection_65 | 0.96 | cop[tiflash] | eq(db1.fbaccount.core, 1) | |
│ └─TableFullScan_64 | 958.00 | cop[tiflash] | table:act | keep order:false, stats:pseudo |
└─IndexLookUp_78(Probe) | 3500236.02 | root | ||
├─IndexRangeScan_76(Build) | 3500236.02 | cop[tikv] | table:ad, index:date_start_2(date_start, AssetId) | range:[2022-06-30 00:00:00,2022-07-07 00:00:00], keep order:false |
└─TableRowIDScan_77(Probe) | 3500236.02 | cop[tikv] | table:ad | keep order:false |
感觉只是一个常规的join表操作查询.怎么会突然变成这样,查询不出来
FbAdAsset 总数据大概是 1.7亿 FbAccount 大概是958行
【附件】
请提供各个组件的 version 信息,如 cdc/tikv,可通过执行 cdc version/tikv-server --version 获取。