复制出来一张表,把后来加的虚拟列去掉之后问题复现:
select *
from test
where json_extract(trackid, '$[0]') = 9;
返回113条数据,执行计划如下:
TableReader_16 17588.00 root MppVersion: 2, data:ExchangeSender_15
└─ExchangeSender_15 17588.00 mpp[tiflash] ExchangeType: PassThrough
└─Selection_14 17588.00 mpp[tiflash] eq(json_extract(mbase.test.trackid, "$[0]"), cast(9, json BINARY))
└─TableFullScan_13 21985.00 mpp[tiflash] table:test pushed down filter:empty, keep order:false, stats:pseudo
在未开启TiFlash副本的时候此SQL执行正常。
加了order by之后的查询:
select *
from test
where json_extract(trackid, '$[0]') = 9 order by id;
返回306条数据,执行计划如下:
TableReader_24 17588.00 root data:Selection_23
└─Selection_23 17588.00 cop[tikv] eq(json_extract(mbase.test.trackid, "$[0]"), cast(9, json BINARY))
└─TableFullScan_22 21985.00 cop[tikv] table:test keep order:true, stats:pseudo
另外原来的表添加了虚拟列之后貌似TiFlash副本实际增加了此列,从而恢复了查询结果正常:
添加虚拟列SQL:
alter table test add column track_id INT(64) AS (JSON_EXTRACT(trackid, '$[0]')) after trackid;
select *
from test
where json_extract(trackid, '$[0]') = 9;
返回306条数据,执行计划如下:
TableReader_12 17586.40 root MppVersion: 2, data:ExchangeSender_11
└─ExchangeSender_11 17586.40 mpp[tiflash] ExchangeType: PassThrough
└─Selection_10 17586.40 mpp[tiflash] eq(json_extract(test.test.trackid, "$[0]"), cast(9, json BINARY))
└─TableFullScan_9 21983.00 mpp[tiflash] table:test pushed down filter:empty, keep order:false
select *
from test
where track_id = 9;
返回306条数据,执行计划如下:
Selection_12 0.00 root eq(test.test.track_id, 9)
└─TableReader_11 21983.00 root MppVersion: 2, data:ExchangeSender_10
└─ExchangeSender_10 21983.00 mpp[tiflash] ExchangeType: PassThrough
└─TableFullScan_9 21983.00 mpp[tiflash] table:test keep order:false
附原始建表语句参考:
CREATE TABLE `test` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`trackid` json NOT NULL COMMENT '赛道ID',
`name` varchar(45) NOT NULL COMMENT '名称',
`createtime` datetime NOT NULL COMMENT '创建时间',
`updatetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
UNIQUE KEY `uniq_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_id_cache] AUTO_ID_CACHE=1 */ COMMENT='信息表' /*T![placement] PLACEMENT POLICY=`storageonnvme` */