帧表非常大,但我有另一个值可以将在这种情况下搜索到的帧降低到 50 以下。实际上并没有模式,每一帧都以与前一站相同的 gpstime 开始。
我不太明白您如何将搜索帧数降低到 50,但是如果您gpstime仅在 50 中搜索 10,000 个值frames,那么将这 50 个帧加载到 RAM 中并在Python,使用类似于 foobarbecue 的答案。
但是,如果您在gpstime整个表中搜索 10 个值,例如 10,000,000 frames,那么您可能不想将所有 10,000,000 帧加载到 RAM 中。
您可以通过添加以下索引来让数据库执行类似的操作...
ALTER TABLE myapp_frames ADD UNIQUE KEY my_key (start_gps, stop_gps, frame_name);
...然后使用这样的查询...
(SELECT frame_name FROM myapp_frames
WHERE 2.5 BETWEEN start_gps AND stop_gps LIMIT 1)
UNION ALL
(SELECT frame_name FROM myapp_frames
WHERE 4.5 BETWEEN start_gps AND stop_gps LIMIT 1)
UNION ALL
(SELECT frame_name FROM myapp_frames
WHERE 7.5 BETWEEN start_gps AND stop_gps LIMIT 1);
...返回...
+------------+
| frame_name |
+------------+
| Frame 2 |
| Frame 4 |
| Frame 7 |
+------------+
...并为此EXPLAIN表演...
+----+--------------+--------------+-------+---------------+--------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+--------------+-------+---------------+--------+---------+------+------+--------------------------+
| 1 | PRIMARY | myapp_frames | range | my_key | my_key | 8 | NULL | 3 | Using where; Using index |
| 2 | UNION | myapp_frames | range | my_key | my_key | 8 | NULL | 5 | Using where; Using index |
| 3 | UNION | myapp_frames | range | my_key | my_key | 8 | NULL | 8 | Using where; Using index |
| NULL | UNION RESULT | <union1,2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+--------------+-------+---------------+--------+---------+------+------+--------------------------+
...因此您可以在一个查询中执行所有查找该索引的查询,并且该索引应缓存在 RAM 中。