【 TiDB 使用环境】
v5.2.1
同样的表结构total_keys差异巨大,导致执行时间大概几百ms,而且执行时间极其不稳定。
看了文档说是旧版本过多,疑问是 gc是10分钟一回收,然后看了这张表的update 15分钟 的次数是500多,并且是按照id更新的。 怎么会出现100W的旧版本。
建表语句,数据量大概200W
CREATE TABLE `device` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`access` varchar(20) DEFAULT NULL,
`aid` int(11) DEFAULT NULL,
`apk_first_install_time` bigint(20) DEFAULT NULL,
`app_name` varchar(20) DEFAULT NULL,
`app_package` varchar(255) DEFAULT NULL,
`app_type` varchar(20) DEFAULT NULL,
`app_version` varchar(20) DEFAULT NULL,
`appkey` varchar(64) DEFAULT NULL,
`apptheme` varchar(20) DEFAULT NULL,
`ban_at` timestamp NULL DEFAULT NULL,
`ban_count` int(11) DEFAULT NULL,
`bind_phone` varchar(11) DEFAULT NULL,
`cdid` varchar(64) DEFAULT NULL,
`channel` varchar(40) DEFAULT NULL,
`channel_id` varchar(10) DEFAULT NULL,
`city` varchar(10) DEFAULT NULL,
`clientudid` varchar(255) DEFAULT NULL,
`cookie` longtext DEFAULT NULL,
`cpu_abi` varchar(255) DEFAULT NULL,
`cpu_support64` varchar(255) DEFAULT NULL,
`create_at` timestamp NULL DEFAULT NULL,
`device_brand` varchar(255) DEFAULT NULL,
`device_id` varchar(255) DEFAULT NULL,
`device_manufacturer` varchar(255) DEFAULT NULL,
`device_model` varchar(255) DEFAULT NULL,
`device_platform` varchar(255) DEFAULT NULL,
`device_token` varchar(255) DEFAULT NULL,
`display_density` varchar(255) DEFAULT NULL,
`display_name` varchar(255) DEFAULT NULL,
`dpi` int(11) DEFAULT NULL,
`git_hash` varchar(255) DEFAULT NULL,
`install_id` varchar(255) DEFAULT NULL,
`is_ban` tinyint(1) DEFAULT NULL,
`is_reject` tinyint(1) DEFAULT NULL,
`is_system_app` int(11) DEFAULT NULL,
`language` varchar(255) DEFAULT NULL,
`mac` varchar(255) DEFAULT NULL,
`magic_tag` varchar(255) DEFAULT NULL,
`manifest_version_code` int(11) DEFAULT NULL,
`minor_status` varchar(255) DEFAULT NULL,
`not_request_sender` int(11) DEFAULT NULL,
`oaid` varchar(255) DEFAULT NULL,
`oaid_may_support` tinyint(1) DEFAULT NULL,
`openudid` varchar(255) DEFAULT NULL,
`os` varchar(255) DEFAULT NULL,
`os_api` int(11) DEFAULT NULL,
`os_version` varchar(255) DEFAULT NULL,
`pick_count` int(11) DEFAULT NULL,
`pre_installed_channel` varchar(255) DEFAULT NULL,
`region` varchar(255) DEFAULT NULL,
`rejected_at` timestamp NULL DEFAULT NULL,
`release_build` varchar(255) DEFAULT NULL,
`resolution` varchar(255) DEFAULT NULL,
`rom` varchar(255) DEFAULT NULL,
`rom_version` varchar(255) DEFAULT NULL,
`sdk_flavor` varchar(255) DEFAULT NULL,
`sdk_target_version` int(11) DEFAULT NULL,
`sdk_version` varchar(255) DEFAULT NULL,
`sig_hash` varchar(255) DEFAULT NULL,
`sn` varchar(255) DEFAULT NULL,
`ssmix` varchar(255) DEFAULT NULL,
`timezone` int(11) DEFAULT NULL,
`tz_name` varchar(255) DEFAULT NULL,
`tz_offset` int(11) DEFAULT NULL,
`udid` varchar(255) DEFAULT NULL,
`update_version_code` int(11) DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`user_agent` varchar(255) DEFAULT NULL,
`version_code` int(11) DEFAULT NULL,
`version_name` varchar(255) DEFAULT NULL,
`is_simulate` tinyint(1) DEFAULT NULL,
`ban_type` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `by_is_ban` (`is_ban`),
KEY `by_pick_count` (`pick_count`),
KEY `idx_is_ban_pick_count` (`is_ban`,`pick_count`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=3160124;
SELECT
id
FROM
device device0_
WHERE
device0_.is_ban = 0
ORDER BY
device0_.pick_count ASC
LIMIT
1;
explain信息
Projection_7 1.00 1 root time:329.2ms, loops:2, Concurrency:OFF aweme_encryption.device.id, aweme_encryption.device.access, aweme_encryption.device.aid, aweme_encryption.device.apk_first_install_time, aweme_encryption.device.app_name, aweme_encryption.device.app_package, aweme_encryption.device.app_type, aweme_encryption.device.app_version, aweme_encryption.device.appkey, aweme_encryption.device.apptheme, aweme_encryption.device.ban_at, aweme_encryption.device.ban_count, aweme_encryption.device.ban_type, aweme_encryption.device.bind_phone, aweme_encryption.device.cdid, aweme_encryption.device.channel, aweme_encryption.device.channel_id, aweme_encryption.device.city, aweme_encryption.device.clientudid, aweme_encryption.device.cookie, aweme_encryption.device.cpu_abi, aweme_encryption.device.cpu_support64, aweme_encryption.device.create_at, aweme_encryption.device.device_brand, aweme_encryption.device.device_id, aweme_encryption.device.device_manufacturer, aweme_encryption.device.device_model, aweme_encryption.device.device_platform, aweme_encryption.device.device_token, aweme_encryption.device.display_density, aweme_encryption.device.display_name, aweme_encryption.device.dpi, aweme_encryption.device.git_hash, aweme_encryption.device.install_id, aweme_encryption.device.is_ban, aweme_encryption.device.is_reject, aweme_encryption.device.is_simulate, aweme_encryption.device.is_system_app, aweme_encryption.device.language, aweme_encryption.device.mac, aweme_encryption.device.magic_tag, aweme_encryption.device.manifest_version_code, aweme_encryption.device.minor_status, aweme_encryption.device.not_request_sender, aweme_encryption.device.oaid, aweme_encryption.device.oaid_may_support, aweme_encryption.device.openudid, aweme_encryption.device.os, aweme_encryption.device.os_api, aweme_encryption.device.os_version, aweme_encryption.device.pick_count, aweme_encryption.device.pre_installed_channel, aweme_encryption.device.region, aweme_encryption.device.rejected_at, aweme_encryption.device.release_build, aweme_encryption.device.resolution, aweme_encryption.device.rom, aweme_encryption.device.rom_version, aweme_encryption.device.sdk_flavor, aweme_encryption.device.sdk_target_version, aweme_encryption.device.sdk_version, aweme_encryption.device.sig_hash, aweme_encryption.device.sn, aweme_encryption.device.ssmix, aweme_encryption.device.timezone, aweme_encryption.device.tz_name, aweme_encryption.device.tz_offset, aweme_encryption.device.udid, aweme_encryption.device.update_version_code, aweme_encryption.device.updated_at, aweme_encryption.device.user_agent, aweme_encryption.device.version_code, aweme_encryption.device.version_name 39.0 KB N/A
└─IndexLookUp_26 1.00 1 root time:329.1ms, loops:2, index_task: {total_time: 328ms, fetch_handle: 328ms, build: 5.61µs, wait: 7.1µs}, table_task: {total_time: 1.64s, num: 1, concurrency: 5} limit embedded(offset:0, count:1) 47.0 KB N/A
├─Limit_25(Build) 1.00 1 cop[tikv] time:327.9ms, loops:1, cop_task: {num: 3, max: 295.6ms, min: 1.19ms, avg: 109.3ms, p95: 295.6ms, max_proc_keys: 1, p95_proc_keys: 1, tot_proc: 282ms, tot_wait: 44ms, rpc_num: 3, rpc_time: 327.8ms, copr_cache_hit_ratio: 0.33}, tikv_task:{proc max:284ms, min:1ms, p80:284ms, p95:284ms, iters:3, tasks:3}, scan_detail: {total_process_keys: 1, total_keys: 925178, rocksdb: {delete_skipped_count: 10, key_skipped_count: 925204, block: {cache_hit_count: 538, read_count: 0, read_byte: 0 Bytes}}} offset:0, count:1 N/A N/A
│ └─IndexRangeScan_23 1.00 1 cop[tikv] table:device0_, index:idx_is_ban_pick_count(is_ban, pick_count) tikv_task:{proc max:284ms, min:1ms, p80:284ms, p95:284ms, iters:3, tasks:3} range:[0,0], keep order:true N/A N/A
└─TableRowIDScan_24(Probe) 1.00 1 cop[tikv] table:device0_ time:893.6µs, loops:2, cop_task: {num: 1, max: 807.3µs, proc_keys: 1, rpc_num: 1, rpc_time: 799.9µs, copr_cache_hit_ratio: 0.00}, tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 1, total_keys: 4, rocksdb: {delete_skipped_count: 0, key_skipped_count: 3, block: {cache_hit_count: 8, read_count: 0, read_byte: 0 Bytes}}} keep order:false, stats:pseudo N/A N/A
copy了一张表同样的explain
Projection_7 1.00 1 root time:6.41ms, loops:2, Concurrency:OFF aweme_encryption.device_copy1.id, aweme_encryption.device_copy1.access, aweme_encryption.device_copy1.aid, aweme_encryption.device_copy1.apk_first_install_time, aweme_encryption.device_copy1.app_name, aweme_encryption.device_copy1.app_package, aweme_encryption.device_copy1.app_type, aweme_encryption.device_copy1.app_version, aweme_encryption.device_copy1.appkey, aweme_encryption.device_copy1.apptheme, aweme_encryption.device_copy1.ban_at, aweme_encryption.device_copy1.ban_count, aweme_encryption.device_copy1.ban_type, aweme_encryption.device_copy1.bind_phone, aweme_encryption.device_copy1.cdid, aweme_encryption.device_copy1.channel, aweme_encryption.device_copy1.channel_id, aweme_encryption.device_copy1.city, aweme_encryption.device_copy1.clientudid, aweme_encryption.device_copy1.cookie, aweme_encryption.device_copy1.cpu_abi, aweme_encryption.device_copy1.cpu_support64, aweme_encryption.device_copy1.create_at, aweme_encryption.device_copy1.device_brand, aweme_encryption.device_copy1.device_id, aweme_encryption.device_copy1.device_manufacturer, aweme_encryption.device_copy1.device_model, aweme_encryption.device_copy1.device_platform, aweme_encryption.device_copy1.device_token, aweme_encryption.device_copy1.display_density, aweme_encryption.device_copy1.display_name, aweme_encryption.device_copy1.dpi, aweme_encryption.device_copy1.git_hash, aweme_encryption.device_copy1.install_id, aweme_encryption.device_copy1.is_ban, aweme_encryption.device_copy1.is_reject, aweme_encryption.device_copy1.is_simulate, aweme_encryption.device_copy1.is_system_app, aweme_encryption.device_copy1.language, aweme_encryption.device_copy1.mac, aweme_encryption.device_copy1.magic_tag, aweme_encryption.device_copy1.manifest_version_code, aweme_encryption.device_copy1.minor_status, aweme_encryption.device_copy1.not_request_sender, aweme_encryption.device_copy1.oaid, aweme_encryption.device_copy1.oaid_may_support, aweme_encryption.device_copy1.openudid, aweme_encryption.device_copy1.os, aweme_encryption.device_copy1.os_api, aweme_encryption.device_copy1.os_version, aweme_encryption.device_copy1.pick_count, aweme_encryption.device_copy1.pre_installed_channel, aweme_encryption.device_copy1.region, aweme_encryption.device_copy1.rejected_at, aweme_encryption.device_copy1.release_build, aweme_encryption.device_copy1.resolution, aweme_encryption.device_copy1.rom, aweme_encryption.device_copy1.rom_version, aweme_encryption.device_copy1.sdk_flavor, aweme_encryption.device_copy1.sdk_target_version, aweme_encryption.device_copy1.sdk_version, aweme_encryption.device_copy1.sig_hash, aweme_encryption.device_copy1.sn, aweme_encryption.device_copy1.ssmix, aweme_encryption.device_copy1.timezone, aweme_encryption.device_copy1.tz_name, aweme_encryption.device_copy1.tz_offset, aweme_encryption.device_copy1.udid, aweme_encryption.device_copy1.update_version_code, aweme_encryption.device_copy1.updated_at, aweme_encryption.device_copy1.user_agent, aweme_encryption.device_copy1.version_code, aweme_encryption.device_copy1.version_name 39.0 KB N/A
└─IndexLookUp_26 1.00 1 root time:6.11ms, loops:2, index_task: {total_time: 3.43ms, fetch_handle: 3.41ms, build: 12.7µs, wait: 1.9µs}, table_task: {total_time: 18.8ms, num: 1, concurrency: 5} limit embedded(offset:0, count:1) 47.0 KB N/A
├─Limit_25(Build) 1.00 1 cop[tikv] time:3.13ms, loops:1, cop_task: {num: 1, max: 2.97ms, proc_keys: 1, tot_proc: 2ms, rpc_num: 1, rpc_time: 2.96ms, copr_cache_hit_ratio: 0.00}, tikv_task:{time:1ms, loops:1}, scan_detail: {total_process_keys: 1, total_keys: 2, rocksdb: {delete_skipped_count: 0, key_skipped_count: 1, block: {cache_hit_count: 0, read_count: 2, read_byte: 29.8 KB}}} offset:0, count:1 N/A N/A
│ └─IndexRangeScan_23 1.00 1 cop[tikv] table:device0_, index:idx_is_ban_pick_count(is_ban, pick_count) tikv_task:{time:1ms, loops:1} range:[0,0], keep order:true N/A N/A
└─TableRowIDScan_24(Probe) 1.00 1 cop[tikv] table:device0_ time:1.89ms, loops:2, cop_task: {num: 1, max: 1.55ms, proc_keys: 0, rpc_num: 1, rpc_time: 1.53ms, copr_cache_hit_ratio: 1.00}, tikv_task:{time:18ms, loops:1} keep order:false, stats:pseudo N/A N/A
奇怪的是total_keys数量差距很大