【发布时间】:2015-05-30 00:24:05
【问题描述】:
我的数据库中有两个表大致如下所示:
运动:
Timestamp visitorID Type X Y
2012-03-02 11:02:30 379 pedestrian 32 46
2012-03-01 12:32:41 654 pedestrian 54 56
2012-03-02 07:02:16 789 pedestrian 39 52
通讯:(拨打电话)
Timestamp senderID toID GeneralLocation
2012-03-02 09:02:30 878 674 Grasslands
2012-03-02 11:30:01 456 213 Tundra
2012-03-02 07:02:12 789 654 Mountains
我运行了这个查询:
SELECT c.senderID,c.timestamp,m.timestamp,m.x,m.y
FROM communication c
JOIN movement m ON c.senderID = m.visitorID
WHERE m.timestamp = (SELECT MIN(mm.timestamp)
FROM movement mm
WHERE mm.timestamp >= c.timestamp);
这基本上找到了给定通信时间戳的最接近的移动时间戳。我想要做的是找到某个 senderID 拨打电话的位置。由于不是每个visitorID 都进行了调用,因此它首先检查c.senderID = m.visitorID。那么它会遍历communication 的每一行,并在communication 表中为每个timestamp 找到最接近的movement timestamp。
然后我使用EXPLAIN 来查看我是否正在使用我的索引......我得到了这个:
这是否意味着在第二行中我的索引没有被正确使用?我现在的索引是:
CREATE INDEX timestamp ON DBName.movement (timestamp);
CREATE INDEX ctstamp ON DBName.communication (timestamp);
CREATE INDEX SID_tstamp ON communication (senderID, timestamp);
CREATE INDEX VID_tstamp ON movement (visitorID, timestamp);
所以基本上,在Movement 和Communication 表中,每个timestamp 都有一个索引。然后每个ID 和timestamp 都有一个。
我的问题基本上是,我的索引有什么问题,我该如何修改它们以便可以使用它们?
我是 SQL 新手,因此我们将不胜感激任何帮助,谢谢!!
编辑
【问题讨论】:
标签: mysql sql indexing phpmyadmin