我正在使用GTFS并试图找到一种方法来加速我的内部联接。表“Trips”和“StopTimes”具有公共列“TripID”,表“StopTimes”和“Stops”具有公共列“StopID”。内部联接检索与特定Trip相对应的所有Stops,但是大约需要20秒。此外,“StopTimes”表包含900万行,而其他表包含数千行。以下代码是我的查询。
String joinQuery = "SELECT s.stop_id, s.stop_code, s.stop_name "
+ "FROM Trips as t INNER JOIN StopTimes as st ON st.trip_id = t.trip_id "
+ "INNER JOIN Stops as s ON s.stop_id = st.stop_id "
+ "WHERE t.trip_id = " + TripID + " AND t.shape_id = " + ShapeID
+ " ORDER BY st.stop_sequence";
这是在创建数据库时执行的代码。
String CREATE_TRIPS_TABLE = "CREATE VIRTUAL TABLE " + TABLE_TRIPS + " USING FTS3("
+ KEY_T_ROUTE_ID + " INTEGER,"
+ KEY_T_NAME + " TEXT,"
+ KEY_TRIP_ID + " INTEGER,"
+ KEY_DIRECTION_ID + " INTEGER,"
+ KEY_SHAPE_ID + " INTEGER);";
String CREATE_STOPS_TABLE = "CREATE VIRTUAL TABLE " + TABLE_STOPS + " USING FTS3("
+ KEY_STOP_ID + " INTEGER,"
+ KEY_STOP_CODE + " TEXT,"
+ KEY_STOP_NAME + " TEXT,"
+ KEY_STOP_LAT + " DOUBLE,"
+ KEY_STOP_LON + " DOUBLE,"
+ KEY_STOP_WHEELCHAIR + " INTEGER);";
String CREATE_STOPTIMES_TABLE = "CREATE VIRTUAL TABLE " + TABLE_STOP_TIMES + " USING FTS3("
+ KEY_S_TRIP_ID + " INTEGER,"
+ KEY_S_STOP_ID + " INTEGER,"
+ KEY_ARRIVAL_TIME + " TEXT,"
+ KEY_STOP_SEQUENCE + " INTEGER,"
+ KEY_SHAPE_DIST_TRAVELLED + " DOUBLE);";
我遵循了我在这里找到的一些建议,但我仍然无法加快查询速度,感谢所有反馈。
更新 我更改了我的表的创建方式,并为INNER JOIN查询中涉及的两个索引添加了索引,JOINS现在是即时的。
String CREATE_TRIPS_TABLE = "CREATE TABLE " + TABLE_TRIPS + "("
+ KEY_T_ID + " integer PRIMARY KEY AUTOINCREMENT,"
+ KEY_T_ROUTE_ID + " INTEGER,"
+ KEY_T_NAME + " TEXT,"
+ KEY_TRIP_ID + " INTEGER,"
+ KEY_DIRECTION_ID + " INTEGER,"
+ KEY_SHAPE_ID + " INTEGER);";
String CREATE_STOPS_TABLE = "CREATE TABLE " + TABLE_STOPS + "("
+ KEY_S_ID + " integer PRIMARY KEY AUTOINCREMENT,"
+ KEY_STOP_ID + " INTEGER,"
+ KEY_STOP_CODE + " TEXT,"
+ KEY_STOP_NAME + " TEXT,"
+ KEY_STOP_LAT + " DOUBLE,"
+ KEY_STOP_LON + " DOUBLE,"
+ KEY_STOP_WHEELCHAIR + " INTEGER);";
String CREATE_STOPTIMES_TABLE = "CREATE TABLE " + TABLE_STOP_TIMES + "("
+ KEY_ST_ID + " integer PRIMARY KEY AUTOINCREMENT,"
+ KEY_S_TRIP_ID + " INTEGER,"
+ KEY_S_STOP_ID + " INTEGER,"
+ KEY_ARRIVAL_TIME + " TEXT,"
+ KEY_STOP_SEQUENCE + " INTEGER,"
+ KEY_SHAPE_DIST_TRAVELLED + " DOUBLE);";
db.execSQL(CREATE_ROUTES_TABLE);
db.execSQL(CREATE_TRIPS_TABLE);
db.execSQL(CREATE_STOPS_TABLE);
db.execSQL(CREATE_STOPTIMES_TABLE);
db.execSQL("CREATE INDEX T_id ON Trips(trip_id)");
db.execSQL("CREATE INDEX S_t_id ON StopTimes(trip_id)");
答案 0 :(得分:0)
我发现您正在使用SQLite's FTS3 extension,旨在提高全文搜索的效果。我怀疑你发出的查询类型(以及通常针对GTFS数据运行的查询类型)实际上是在对你不利。
我建议您首先创建一个传统的关系数据库,在相应的列上包含索引,然后针对该数据库测试查询的性能。当在这种类型的应用程序中使用时,SQLite肯定能够提供良好的性能,所以我认为你会感到惊喜。一旦您使用传统技术证明了成功,您可以查看其他方法,以便在必要时更快地运行查询。
最后,请注意SQLite does not allow indices on virtual tables,我怀疑这是您的查询现在需要很长时间才能完成的具体原因。