我有一张在 MariaDB 10.2 中使用 TokuDB 引擎的表
该表目前对约 2500 个传感器进行了约 7 亿次测量。
我想获取表中所有不同的传感器名称。
有这个名字的索引。
尽管如此,MariaDB 似乎还是进行了全表扫描,需要大约 5 分钟。
我能做些什么来加快速度?
表格是:
CREATE TABLE `sensor_data_single` (
`sensor_id` varchar(255) CHARACTER SET latin1 NOT NULL,
`timestamp` bigint(20) NOT NULL,
`value_bool` bit(1) DEFAULT NULL,
`value_boolarray` tinyblob DEFAULT NULL,
`value_bytearray` tinyblob DEFAULT NULL,
`value_date` date DEFAULT NULL,
`value_time` int(11) DEFAULT NULL,
`value_instant` datetime(6) DEFAULT NULL,
`value_double` double DEFAULT NULL,
`value_enum_code` int(11) DEFAULT NULL,
`value_enum_text` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
`value_int` int(11) DEFAULT NULL,
`value_long` bigint(20) DEFAULT NULL,
`value_string` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
`value_type` int(11) DEFAULT NULL,
PRIMARY KEY (`sensor_id`,`timestamp`),
KEY `sensor_data_ts_key` (`timestamp`) `CLUSTERING`=YES,
KEY `sensor_data_id_key` (`sensor_id`)
) ENGINE=TokuDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci `COMPRESSION`=TOKUDB_FAST
查询是一个普通的
select distinct sensor_id from sensor_data_single USE INDEX (sensor_data_id_key) order by sensor_id ASC;
尽管如此,当查询运行时,状态就像Queried about 513770000 rows
在SHOW PROCESSLIST
解释说使用了索引,但是考虑到只有〜2500个不同的值,这不应该更快吗?
+------+-------------+--------------+-------+---------------+--------------------+---------+------+-----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+--------------+-------+---------------+--------------------+---------+------+-----------+-------------+
| 1 | SIMPLE | sensordata0_ | index | NULL | sensor_data_id_key | 257 | NULL | 764382458 | Using index |
+------+-------------+--------------+-------+---------------+--------------------+---------+------+-----------+-------------+
如果我删除 sensor_id 上的索引,则说明更改为:
+------+-------------+--------------+-------+---------------+---------+---------+------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+--------------+-------+---------------+---------+---------+------+--------+--------------------------+
| 1 | SIMPLE | sensordata0_ | range | NULL | PRIMARY | 257 | NULL | 175017 | Using index for group-by |
+------+-------------+--------------+-------+---------------+---------+---------+------+--------+--------------------------+
但性能没有显着变化。