为啥同一条sql 只是查询条件IN 多了一个 查询性能相差特别大,见图描述

建立组合索引mp_id,send_time ,如果 mp_id 大一点效率就不行了。看下面的对比


idx_mp_id_send_time的执行计划:
{
“EXPLAIN ANALYZE\r\nSELECT\r\n mn.id,\r\n mn.title,\r\n IF(\r\n mn.template_url IS NOT NULL,\r\n mn.template_url,\r\n mn.screen_url\r\n ) AS cover,\r\n mn.send_time,\r\n mn.create_time,\r\n mn.media_id,\r\n mn.content,\r\n mn.news_type,\r\n mn.like_num,\r\n mn.collect_num,\r\n mn.read_num,\r\n mn.comment_num,\r\n mn.mp_id\r\nFROM\r\n mp_news mn force INDEX (idx_mp_id_send_time)\r\nWHERE\r\n mn.is_delete = 0\r\n AND mn.is_push = 1\r\n AND mn.mp_id IN (\r\n 1815578536521687042,\r\n1815582786249474049,\r\n1815648372239687682,\r\n1815662856870092802,\r\n1815669108643528705,\r\n1815952623272525825,\r\n1815954241409843201,\r\n1815961401420197890,\r\n1815961989092519937\r\n )\r\nORDER BY\r\n mn.send_time DESC,\r\n mn.id DESC\r\nLIMIT\r\n 10”: [
{
“id” : “Projection_7”,
“estRows” : “10.00”,
“actRows” : “10”,
“task” : “root”,
“access object” : “”,
“execution info” : “time:338.8ms, loops:2, RU:4186.480730, Concurrency:OFF”,
“operator info” : “liu_test.mp_news.id, liu_test.mp_news.title, if(not(isnull(liu_test.mp_news.template_url)), liu_test.mp_news.template_url, liu_test.mp_news.screen_url)->Column#27, liu_test.mp_news.send_time, liu_test.mp_news.create_time, liu_test.mp_news.media_id, liu_test.mp_news.content, liu_test.mp_news.news_type, liu_test.mp_news.like_num, liu_test.mp_news.collect_num, liu_test.mp_news.read_num, liu_test.mp_news.comment_num, liu_test.mp_news.mp_id”,
“memory” : “13.9 KB”,
“disk” : “N/A”
},
{
“id” : “└─TopN_9”,
“estRows” : “10.00”,
“actRows” : “10”,
“task” : “root”,
“access object” : “”,
“execution info” : “time:338.8ms, loops:2”,
“operator info” : “liu_test.mp_news.send_time:desc, liu_test.mp_news.id:desc, offset:0, count:10”,
“memory” : “28.5 KB”,
“disk” : “N/A”
},
{
“id” : " └─IndexLookUp_17",
“estRows” : “10.00”,
“actRows” : “500”,
“task” : “root”,
“access object” : “”,
“execution info” : “time:338.6ms, loops:3, index_task: {total_time: 211.2ms, fetch_handle: 20ms, build: 27.5µs, wait: 191.1ms}, table_task: {total_time: 2.35s, num: 24, concurrency: 8}, next: {wait_index: 1.22ms, wait_table_lookup_build: 85.6µs, wait_table_lookup_resp: 336.6ms}”,
“operator info” : “”,
“memory” : “23.9 MB”,
“disk” : “N/A”
},
{
“id” : " ├─IndexRangeScan_13(Build)“,
“estRows” : “279919.00”,
“actRows” : “280022”,
“task” : “cop[tikv]”,
“access object” : “table:mn, index:idx_mp_id_send_time(mp_id, send_time)”,
“execution info” : “time:5.58ms, loops:284, cop_task: {num: 15, max: 1.45ms, min: 265.3µs, avg: 667.4µs, p95: 1.45ms, max_proc_keys: 2016, p95_proc_keys: 2016, tot_proc: 2.46ms, tot_wait: 1.34ms, copr_cache_hit_ratio: 0.67, build_task_duration: 32µs, max_distsql_concurrency: 2}, rpc_info:{Cop:{num_rpc:15, total_time:9.78ms}}, tikv_task:{proc max:40ms, min:0s, avg: 9.4ms, p80:22ms, p95:40ms, iters:330, tasks:15}, scan_detail: {total_process_keys: 3712, total_process_keys_size: 204160, total_keys: 3717, get_snapshot_time: 973.4µs, rocksdb: {key_skipped_count: 3712, block: {cache_hit_count: 20}}}, time_detail: {total_process_time: 2.46ms, total_suspend_time: 10.5µs, total_wait_time: 1.34ms, total_kv_read_wall_time: 1ms, tikv_wall_time: 5.17ms}”,
“operator info” : “range:[1815578536521687042,1815578536521687042], [1815582786249474049,1815582786249474049], [1815648372239687682,1815648372239687682], [1815662856870092802,1815662856870092802], [1815669108643528705,1815669108643528705], [1815952623272525825,1815952623272525825], [1815954241409843201,1815954241409843201], [1815961401420197890,1815961401420197890], [1815961989092519937,1815961989092519937], keep order:false”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " └─TopN_16(Probe)”,
“estRows” : “10.00”,
“actRows” : “500”,
“task” : “cop[tikv]”,
“access object” : “”,
“execution info” : “time:2.26s, loops:36, cop_task: {num: 71, max: 212.7ms, min: 0s, avg: 57.8ms, p95: 178.4ms, max_proc_keys: 13890, p95_proc_keys: 12129, tot_proc: 3.42s, tot_wait: 9.9ms, copr_cache_hit_ratio: 0.14, build_task_duration: 6.63ms, max_distsql_concurrency: 11, max_extra_concurrency: 1, store_batch_num: 1}, rpc_info:{Cop:{num_rpc:70, total_time:4.1s}}, tikv_task:{proc max:201ms, min:0s, avg: 55.6ms, p80:116ms, p95:168ms, iters:314, tasks:71}, scan_detail: {total_process_keys: 274575, total_process_keys_size: 196659322, total_keys: 306309, get_snapshot_time: 7.01ms, rocksdb: {key_skipped_count: 182588, block: {cache_hit_count: 774082}}}, time_detail: {total_process_time: 3.42s, total_suspend_time: 426.9ms, total_wait_time: 9.9ms, total_kv_read_wall_time: 3.78s, tikv_wall_time: 4s}”,
“operator info” : “liu_test.mp_news.send_time:desc, liu_test.mp_news.id:desc, offset:0, count:10”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " └─Selection_15",
“estRows” : “226405.65”,
“actRows” : “223995”,
“task” : “cop[tikv]”,
“access object” : “”,
“execution info” : “tikv_task:{proc max:197ms, min:0s, avg: 54.7ms, p80:111ms, p95:166ms, iters:314, tasks:71}”,
“operator info” : “eq(liu_test.mp_news.is_delete, 0), eq(liu_test.mp_news.is_push, 1)”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " └─TableRowIDScan_14",
“estRows” : “279919.00”,
“actRows” : “280022”,
“task” : “cop[tikv]”,
“access object” : “table:mn”,
“execution info” : “tikv_task:{proc max:196ms, min:0s, avg: 54.5ms, p80:111ms, p95:166ms, iters:314, tasks:71}”,
“operator info” : “keep order:false”,
“memory” : “N/A”,
“disk” : “N/A”
}
]}

走idx_send_time的执行计划:
{
“EXPLAIN ANALYZE\r\nSELECT\r\n mn.id,\r\n mn.title,\r\n IF(\r\n mn.template_url IS NOT NULL,\r\n mn.template_url,\r\n mn.screen_url\r\n ) AS cover,\r\n mn.send_time,\r\n mn.create_time,\r\n mn.media_id,\r\n mn.content,\r\n mn.news_type,\r\n mn.like_num,\r\n mn.collect_num,\r\n mn.read_num,\r\n mn.comment_num,\r\n mn.mp_id\r\nFROM\r\n mp_news mn \r\nWHERE\r\n mn.is_delete = 0\r\n AND mn.is_push = 1\r\n AND mn.mp_id IN (\r\n 1815578536521687042,\r\n1815582786249474049,\r\n1815648372239687682,\r\n1815662856870092802,\r\n1815669108643528705,\r\n1815952623272525825,\r\n1815954241409843201,\r\n1815961401420197890,\r\n1815961989092519937\r\n )\r\nORDER BY\r\n mn.send_time DESC,\r\n mn.id DESC\r\nLIMIT\r\n 10”: [
{
“id” : “Projection_7”,
“estRows” : “10.00”,
“actRows” : “10”,
“task” : “root”,
“access object” : “”,
“execution info” : “time:2.87ms, loops:2, RU:5.814234, Concurrency:OFF”,
“operator info” : “liu_test.mp_news.id, liu_test.mp_news.title, if(not(isnull(liu_test.mp_news.template_url)), liu_test.mp_news.template_url, liu_test.mp_news.screen_url)->Column#27, liu_test.mp_news.send_time, liu_test.mp_news.create_time, liu_test.mp_news.media_id, liu_test.mp_news.content, liu_test.mp_news.news_type, liu_test.mp_news.like_num, liu_test.mp_news.collect_num, liu_test.mp_news.read_num, liu_test.mp_news.comment_num, liu_test.mp_news.mp_id”,
“memory” : “13.9 KB”,
“disk” : “N/A”
},
{
“id” : “└─Limit_12”,
“estRows” : “10.00”,
“actRows” : “10”,
“task” : “root”,
“access object” : “”,
“execution info” : “time:2.83ms, loops:2”,
“operator info” : “offset:0, count:10”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " └─IndexLookUp_28",
“estRows” : “10.00”,
“actRows” : “10”,
“task” : “root”,
“access object” : “”,
“execution info” : “time:2.83ms, loops:1, index_task: {total_time: 2.04ms, fetch_handle: 1.93ms, build: 108.6µs, wait: 8.9µs}, table_task: {total_time: 6.42ms, num: 5, concurrency: 8}, next: {wait_index: 1.32ms, wait_table_lookup_build: 34.1µs, wait_table_lookup_resp: 1.43ms}”,
“operator info” : “”,
“memory” : “129.7 KB”,
“disk” : “N/A”
},
{
“id” : " ├─IndexFullScan_25(Build)“,
“estRows” : “20.05”,
“actRows” : “704”,
“task” : “cop[tikv]”,
“access object” : “table:mn, index:idx_send_time(send_time)”,
“execution info” : “time:2.62ms, loops:7, cop_task: {num: 3, max: 1.09ms, min: 748.5µs, avg: 942.3µs, p95: 1.09ms, max_proc_keys: 480, p95_proc_keys: 480, tot_proc: 714.5µs, tot_wait: 911.2µs, copr_cache_hit_ratio: 0.00, build_task_duration: 32.3µs, max_distsql_concurrency: 2}, rpc_info:{Cop:{num_rpc:3, total_time:2.77ms}}, tikv_task:{proc max:1ms, min:0s, avg: 333.3µs, p80:1ms, p95:1ms, iters:8, tasks:3}, scan_detail: {total_process_keys: 704, total_process_keys_size: 32384, total_keys: 707, get_snapshot_time: 831.3µs, rocksdb: {key_skipped_count: 706, block: {cache_hit_count: 16}}}, time_detail: {total_process_time: 714.5µs, total_wait_time: 911.2µs, total_kv_read_wall_time: 1ms, tikv_wall_time: 2.05ms}”,
“operator info” : “keep order:true, desc”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " └─Selection_27(Probe)”,
“estRows” : “10.00”,
“actRows” : “21”,
“task” : “cop[tikv]”,
“access object” : “”,
“execution info” : “time:5.6ms, loops:6, cop_task: {num: 1, max: 1.28ms, proc_keys: 32, tot_proc: 518.4µs, tot_wait: 266.6µs, copr_cache_hit_ratio: 0.00, build_task_duration: 33.2µs, max_distsql_concurrency: 1, max_extra_concurrency: 1}, rpc_info:{Cop:{num_rpc:1, total_time:1.26ms}}, tikv_task:{time:0s, loops:2}, scan_detail: {total_process_keys: 32, total_process_keys_size: 41559, total_keys: 34, get_snapshot_time: 244.5µs, rocksdb: {key_skipped_count: 22, block: {cache_hit_count: 75}}}, time_detail: {total_process_time: 518.4µs, total_wait_time: 266.6µs, tikv_wall_time: 978.3µs}”,
“operator info” : “eq(liu_test.mp_news.is_delete, 0), eq(liu_test.mp_news.is_push, 1), in(liu_test.mp_news.mp_id, 1815578536521687042, 1815582786249474049, 1815648372239687682, 1815662856870092802, 1815669108643528705, 1815952623272525825, 1815954241409843201, 1815961401420197890, 1815961989092519937)”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " └─TableRowIDScan_26",
“estRows” : “20.05”,
“actRows” : “32”,
“task” : “cop[tikv]”,
“access object” : “table:mn”,
“execution info” : “tikv_task:{time:0s, loops:2}”,
“operator info” : “keep order:false”,
“memory” : “N/A”,
“disk” : “N/A”
}
]}

{
“EXPLAIN ANALYZE\r\nSELECT\r\n mn.id,\r\n mn.title,\r\n IF(\r\n mn.template_url IS NOT NULL,\r\n mn.template_url,\r\n mn.screen_url\r\n ) AS cover,\r\n mn.send_time,\r\n mn.create_time,\r\n mn.media_id,\r\n mn.content,\r\n mn.news_type,\r\n mn.like_num,\r\n mn.collect_num,\r\n mn.read_num,\r\n mn.comment_num,\r\n mn.mp_id\r\nFROM\r\n mp_news mn \r\nWHERE\r\n mn.is_delete = 0\r\n AND mn.is_push = 1\r\n AND mn.mp_id IN (\r\n 1815578536521687042,\r\n1815582786249474049,\r\n1815648372239687682,\r\n1815662856870092802,\r\n1815669108643528705,\r\n1815952623272525825,\r\n1815954241409843201,\r\n1815961401420197890,\r\n1815961989092519937\r\n )\r\nORDER BY\r\n mn.send_time DESC,\r\n mn.id DESC\r\nLIMIT\r\n 10”: [
{
“id” : “Projection_7”,
“estRows” : “10.00”,
“actRows” : “10”,
“task” : “root”,
“access object” : “”,
“execution info” : “time:2.11ms, loops:2, RU:5.676026, Concurrency:OFF”,
“operator info” : “liu_test.mp_news.id, liu_test.mp_news.title, if(not(isnull(liu_test.mp_news.template_url)), liu_test.mp_news.template_url, liu_test.mp_news.screen_url)->Column#27, liu_test.mp_news.send_time, liu_test.mp_news.create_time, liu_test.mp_news.media_id, liu_test.mp_news.content, liu_test.mp_news.news_type, liu_test.mp_news.like_num, liu_test.mp_news.collect_num, liu_test.mp_news.read_num, liu_test.mp_news.comment_num, liu_test.mp_news.mp_id”,
“memory” : “13.9 KB”,
“disk” : “N/A”
},
{
“id” : “└─Limit_12”,
“estRows” : “10.00”,
“actRows” : “10”,
“task” : “root”,
“access object” : “”,
“execution info” : “time:2.08ms, loops:2”,
“operator info” : “offset:0, count:10”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " └─IndexLookUp_29",
“estRows” : “10.00”,
“actRows” : “10”,
“task” : “root”,
“access object” : “”,
“execution info” : “time:2.08ms, loops:1, index_task: {total_time: 1.89ms, fetch_handle: 1.75ms, build: 123.1µs, wait: 14.5µs}, table_task: {total_time: 3.83ms, num: 5, concurrency: 8}, next: {wait_index: 1.05ms, wait_table_lookup_build: 30.2µs, wait_table_lookup_resp: 949.6µs}”,
“operator info” : “”,
“memory” : “127.3 KB”,
“disk” : “N/A”
},
{
“id” : " ├─Selection_27(Build)“,
“estRows” : “12.36”,
“actRows” : “629”,
“task” : “cop[tikv]”,
“access object” : “”,
“execution info” : “time:1.84ms, loops:7, cop_task: {num: 3, max: 890.7µs, min: 531.9µs, avg: 719.7µs, p95: 890.7µs, max_proc_keys: 480, p95_proc_keys: 480, tot_proc: 892.3µs, tot_wait: 159.4µs, copr_cache_hit_ratio: 0.00, build_task_duration: 27.1µs, max_distsql_concurrency: 2}, rpc_info:{Cop:{num_rpc:3, total_time:2.1ms}}, tikv_task:{proc max:1ms, min:0s, avg: 333.3µs, p80:1ms, p95:1ms, iters:8, tasks:3}, scan_detail: {total_process_keys: 704, total_process_keys_size: 45056, total_keys: 707, get_snapshot_time: 68.2µs, rocksdb: {key_skipped_count: 706, block: {cache_hit_count: 18, read_count: 1, read_byte: 4.87 KB, read_time: 8.02µs}}}, time_detail: {total_process_time: 892.3µs, total_wait_time: 159.4µs, total_kv_read_wall_time: 1ms, tikv_wall_time: 1.46ms}”,
“operator info” : “in(liu_test.mp_news.mp_id, 1815578536521687042, 1815582786249474049, 1815648372239687682, 1815662856870092802, 1815669108643528705, 1815952623272525825, 1815954241409843201, 1815961401420197890, 1815961989092519937)”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " │ └─IndexFullScan_25”,
“estRows” : “20.05”,
“actRows” : “704”,
“task” : “cop[tikv]”,
“access object” : “table:mn, index:idx_send_time_id_mp_id(send_time, id, mp_id)”,
“execution info” : “tikv_task:{proc max:1ms, min:0s, avg: 333.3µs, p80:1ms, p95:1ms, iters:8, tasks:3}”,
“operator info” : “keep order:true, desc, stats:partial[idx_send_time_id_mp_id:missing]”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " └─Selection_28(Probe)“,
“estRows” : “10.00”,
“actRows” : “21”,
“task” : “cop[tikv]”,
“access object” : “”,
“execution info” : “time:2.9ms, loops:6, cop_task: {num: 1, max: 825.5µs, proc_keys: 21, tot_proc: 430.1µs, tot_wait: 36.9µs, copr_cache_hit_ratio: 0.00, build_task_duration: 28.4µs, max_distsql_concurrency: 1, max_extra_concurrency: 1}, rpc_info:{Cop:{num_rpc:1, total_time:799.6µs}}, tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 21, total_process_keys_size: 16982, total_keys: 24, get_snapshot_time: 17.5µs, rocksdb: {key_skipped_count: 15, block: {cache_hit_count: 53}}}, time_detail: {total_process_time: 430.1µs, total_wait_time: 36.9µs, tikv_wall_time: 622µs}”,
“operator info” : “eq(liu_test.mp_news.is_delete, 0), eq(liu_test.mp_news.is_push, 1)”,
“memory” : “N/A”,
“disk” : “N/A”
},
{
“id” : " └─TableRowIDScan_26”,
“estRows” : “12.36”,
“actRows” : “21”,
“task” : “cop[tikv]”,
“access object” : “table:mn”,
“execution info” : “tikv_task:{time:0s, loops:1}”,
“operator info” : “keep order:false, stats:partial[idx_send_time_id_mp_id:missing]”,
“memory” : “N/A”,
“disk” : “N/A”
}
]}

这不就快了吗, 2ms 就出来了,你换换 in 参数试试

一定要 send_time在第一吗 ,我试过mp_id 第一不太行

我大概明白了 主要还是要排序去查

是的,要利用上索引的顺序性,等条件放前边,范围条件和多值条件放后边

这个是因为目前mp_id的数量小, 颗粒度远没有send_time大

大佬 有一个问题 请教下,为啥对于mysql 并不存在这种情况 TIDB会呢,是不是跟底层的存储计算有关系

不是不存在,而是 Mysql 单机架构下不明显,TiDB 这种分布式架构下,回表涉及网络交互,回表多的话会比较明显

受教了 ,你这个
image
回答让我恍然大悟

问下,limit和top有很大区别吗?理解就是limit多了个起始位置

limit是已经排好序的,top没有

OK,测试了下,确实是的,感谢

此话题已在最后回复的 7 天后被自动关闭。不再允许新回复。