我有一个使用MariaDB 10.2中的TokuDB引擎的表
该表目前约有约7亿个测量值,用于~2500个传感器。
我想在表格中获取所有不同的传感器名称。
有一个这个名字的索引。
仍然,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 |
+------+-------------+--------------+-------+---------------+---------+---------+------+--------+--------------------------+
但性能没有显着变化。