【 TiDB 使用环境】测试
请问以下,t1分区表和t2表进行关联
mysql> show create table t1;
±------±---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE t1
(
fname
varchar(50) NOT NULL,
lname
varchar(50) NOT NULL,
region_code
tinyint(3) unsigned NOT NULL,
dob
date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE (region_code
)
(PARTITION p0
VALUES LESS THAN (64),
PARTITION p1
VALUES LESS THAN (128),
PARTITION p2
VALUES LESS THAN (192),
PARTITION p3
VALUES LESS THAN (MAXVALUE)) |
±------±---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> show create table t2;
±------±-----------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±-----------------------------------------------------------------------------------------------------------+
| t2 | CREATE TABLE t2
(
x
int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
±------±-----------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
分区表和普通表关联的时候无法实现分区表的分区裁剪
mysql> explain select /*+ INL_JOIN(t1, t2) */ * from t1 left join t2 on t1.region_code = t2.x where t2.x > 70 and t2.x<120;
±-------------------------------±--------±----------±-----------------------±--------------------------------------------------------+
| id | estRows | task | access object | operator info |
±-------------------------------±--------±----------±-----------------------±--------------------------------------------------------+
| HashJoin_13 | 49.00 | root | | inner join, equal:[eq(zk.t1.region_code, zk.t2.x)] |
| ├─TableReader_26(Build) | 49.00 | root | | data:Selection_25 |
| │ └─Selection_25 | 49.00 | cop[tikv] | | gt(zk.t2.x, 70), lt(zk.t2.x, 120), not(isnull(zk.t2.x)) |
| │ └─TableFullScan_24 | 201.00 | cop[tikv] | table:t2 | keep order:false |
| └─PartitionUnion_15(Probe) | 201.00 | root | | |
| ├─TableReader_17 | 63.00 | root | | data:TableFullScan_16 |
| │ └─TableFullScan_16 | 63.00 | cop[tikv] | table:t1, partition:p0 | keep order:false |
| ├─TableReader_19 | 64.00 | root | | data:TableFullScan_18 |
| │ └─TableFullScan_18 | 64.00 | cop[tikv] | table:t1, partition:p1 | keep order:false |
| ├─TableReader_21 | 64.00 | root | | data:TableFullScan_20 |
| │ └─TableFullScan_20 | 64.00 | cop[tikv] | table:t1, partition:p2 | keep order:false |
| └─TableReader_23 | 10.00 | root | | data:TableFullScan_22 |
| └─TableFullScan_22 | 10.00 | cop[tikv] | table:t1, partition:p3 | keep order:false |
±-------------------------------±--------±----------±-----------------------±--------------------------------------------------------+
13 rows in set, 1 warning (0.00 sec
单独使用分区表的时候带着分区键可以实现分区裁剪
mysql> explain select * from t1 where region_code >70 and region_code <120;
±------------------------±--------±----------±-----------------------±------------------------------------------------------+
| id | estRows | task | access object | operator info |
±------------------------±--------±----------±-----------------------±------------------------------------------------------+
| TableReader_9 | 49.00 | root | | data:Selection_8 |
| └─Selection_8 | 49.00 | cop[tikv] | | gt(zk.t1.region_code, 70), lt(zk.t1.region_code, 120) |
| └─TableFullScan_7 | 64.00 | cop[tikv] | table:t1, partition:p1 | keep order:false |
±------------------------±--------±----------±-----------------------±------------------------------------------------------+
3 rows in set (0.00 sec)
麻烦请各位老师,看下为啥?
插入数据的脚本
cat t1.sh
i=0
while [ $i -le 200 ]
do
let i++
str=“insert into zk.t1 values(‘z’,‘k’,$i,‘2023-1-15’)”
mysql -e “$str”
done
cat t2.sh
i=0
while [ $i -le 200 ]
do
let i++
str=“insert into zk.t2 values($i)”
mysql -e “$str”
done