Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 连表查询后.多增加一个字段都会导致一个莫名的几千万数据扫描
【TiDB Usage Environment】Production Environment
【TiDB Version】5.0.6
【Encountered Problem】SQL Performance Issue
【Reproduction Path】Operations that led to the problem
【Problem Phenomenon and Impact】
Join operation, a very common statement
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
Query statement:
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
Execution plan
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 |
However, when using only one condition, there is no range: decided by [eq(db1.fbadasset.fbaccount, db1.fbaccount.account)], keep order:false. You can see that estRows returns more than 30 million rows.
------Statement:
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
Execution plan:
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 |
It feels like just a regular join table operation query. How could it suddenly become like this, unable to query
FbAdAsset has a total of about 170 million records, FbAccount has about 958 rows
【Attachment】
Please provide the version information of each component, such as cdc/tikv, which can be obtained by executing cdc version/tikv-server --version.