所以我有3张桌子:
Locations (LocationID [PK])
Track_Locations (TrackID [FK], LocationID [FK])
Tracks (TrackID [PK])
我正在为 iPhone 构建一个应用程序,有人告诉我 aJOIN
可能会比子查询运行得更快。这是真的?我已经四处搜索,但找不到直接的答案,并且想知道在这种情况下最好使用什么。
我在这两种情况下的查询将如下所示:
子查询:
SELECT LocationID, TimestampGPS, Longitude, Latitude, ...
FROM locations
WHERE LocationID
IN (SELECT LocationID FROM track_locations WHERE TrackID = ?)
使用JOIN
:
SELECT l.LocationID, l.TimestampGPS, l.Longitude, l.Latitude, ...
FROM locations l
JOIN track_locations tl
ON (l.LocationID = tl.LocationID)
JOIN tracks t
ON (t.TrackID = tl.TrackID)
WHERE t.TrackID = ?