mysql 采样查询 / 间隔查询 / 跳跃查询的两种实现思路
先创建一张测试表
1 CREATE TABLE `test` ( 2 `id` int(11) DEFAULT NULL, 3 `count` int(11) DEFAULT NULL 4 ) ENGINE=InnoDB DEFAULT CHARSET=utf8
插入测试数据
1 insert into test (id, count) values(1000,1),(2000,2),(3000,3),(4000,4),(5000, 5),(6000,6),(7000,7),(8000,8),(9000,9); 2 insert into test (id, count) values(1100,1),(2100,2),(3100,3),(4100,4),(5100, 5),(6100,6),(7100,7),(8100,8),(9100,9); 3 insert into test (id, count) values(1200,1),(2200,2),(3200,3),(4200,4),(5200, 5),(6200,6),(7200,7),(8200,8),(9200,9); 4 insert into test (id, count) values(1300,1),(2300,2),(3300,3),(4300,4),(5300, 5),(6300,6),(7300,7),(8300,8),(9300,9);
方法一:
mysql> SELECT id, SUM(count) FROM test GROUP BY id DIV 1000; +------+------------+ | id | SUM(count) | +------+------------+ | 1000 | 4 | | 2000 | 8 | | 3000 | 12 | | 4000 | 16 | | 5000 | 20 | | 6000 | 24 | | 7000 | 28 | | 8000 | 32 | | 9000 | 36 | +------+------------+ 9 rows in set (0.00 sec)
这种方法通过group by将id根据[1000, 2000), [2000, 3000), [3000, 4000), [4000, 5000), [5000, 6000), [6000, 7000), [7000, 8000), [9000, 10000)分组,分组后的数据可以对其进行AVG()、MAX()、MIN()、SUM()、COUNT()聚合操作。
方法二:
1 mysql> SELECT id, count FROM test WHERE id MOD 1000 = 0; 2 +------+-------+ 3 | id | count | 4 +------+-------+ 5 | 1000 | 1 | 6 | 2000 | 2 | 7 | 3000 | 3 | 8 | 4000 | 4 | 9 | 5000 | 5 | 10 | 6000 | 6 | 11 | 7000 | 7 | 12 | 8000 | 8 | 13 | 9000 | 9 | 14 +------+-------+ 15 9 rows in set (0.00 sec)
这种方法可以实现采样查询,但是缺点也很明显,比较适合采样字段连续的场景。