我有一个(足球)游戏数据库,其中包含时段(例如上半场和下半场)、事件(例如目标、警告)和位置(您在比赛之前和比赛期间所在的位置)的子表。
为了显示父游戏表,我使用了带有适当参数的 CursorLoader,如下所示:
public Loader<Cursor> onCreateLoader(final int id, final Bundle args) {
...
if ((mGamesDB.isOpen()) && (id == GAMES_CURSOR_ID)) {
return createGamesCursorLoader();
}
return null;
}
private Loader<Cursor> createGamesCursorLoader() {
//Because we don't want to create a ContentProvider for now, we use the technique suggested here:
//https://stackoverflow.com/questions/18326954/how-to-read-an-sqlite-db-in-android-with-a-cursorloader
return new CursorLoader(getBaseContext(),null, GamesContract.Games.PROJECTION,
null, null, GamesContract.Games.ORDER_BY) {
@Override
public Cursor loadInBackground() {
if (mGamesDB.isOpen()) {
return mGamesDB.query(
GamesContract.Games.TABLE_NAME,
GamesContract.Games.PROJECTION,
null, null,
null, null,
GamesContract.Games.ORDER_BY
);
}
else return null;
}
};
}
这一切都很好。但是,一旦我开始遍历 Games 游标(调用 onLoadFinished 时),我需要使用当前 GameID 为 Periods、Events 和 Locations 创建子查询。所以我这样做:
private Game buildGameFromDB(final Cursor gameCursor) {
if (!mGamesDB.isOpen() || (gameCursor == null) || gameCursor.isClosed() ) return null;
final WatchGame game = new WatchGame(gameCursor.getString(GamesContract.Games.COLUMN_ID_INDEX),
gameCursor.getLong(GamesContract.Games.COLUMN_ACTUAL_START_MILLIS_INDEX),
gameCursor.getLong(GamesContract.Games.COLUMN_ACTUAL_END_MILLIS_INDEX),
gameCursor.getInt(GamesContract.Games.COLUMN_HOME_TEAM_COLOR_INDEX),
gameCursor.getInt(GamesContract.Games.COLUMN_AWAY_TEAM_COLOR_INDEX),
gameCursor.getInt(GamesContract.Games.COLUMN_HOME_TEAM_SCORE_INDEX),
gameCursor.getInt(GamesContract.Games.COLUMN_AWAY_TEAM_SCORE_INDEX));
//FIXME: Ugly nested queries on the main UI thread
final String[] periodsWhereArgs = {game.getmGameID()};
final Cursor periodsCursor = mGamesDB.query(GamesContract.Periods.TABLE_NAME, GamesContract.Periods.PROJECTION,
GamesContract.Periods.WHERE, periodsWhereArgs,
null, null, GamesContract.Periods.ORDER_BY);
while (periodsCursor.moveToNext()) {
final Period period = new Period(
periodsCursor.getInt(GamesContract.Periods.COLUMN_PERIOD_NUM_INDEX),
periodsCursor.getLong(GamesContract.Periods.COLUMN_ACTUAL_START_MILLIS_INDEX),
periodsCursor.getLong(GamesContract.Periods.COLUMN_ACTUAL_END_MILLIS_INDEX),
periodsCursor.getFloat(GamesContract.Periods.COLUMN_START_BATTERY_PCT_INDEX),
periodsCursor.getFloat(GamesContract.Periods.COLUMN_END_BATTERY_PCT_INDEX),
periodsCursor.getString(GamesContract.Periods.COLUMN_GOOGLE_ACCOUNT_NAME_INDEX),
periodsCursor.getInt(GamesContract.Periods.COLUMN_NUM_LOCATIONS_INDEX),
periodsCursor.getInt(GamesContract.Periods.COLUMN_NUM_LOCATIONS_IN_FIT_INDEX),
periodsCursor.getInt(GamesContract.Periods.COLUMN_CALORIES_INDEX),
periodsCursor.getInt(GamesContract.Periods.COLUMN_STEPS_INDEX),
periodsCursor.getInt(GamesContract.Periods.COLUMN_DISTANCE_METRES_INDEX),
periodsCursor.getLong(GamesContract.Periods.COLUMN_WALKING_MILLIS_INDEX),
periodsCursor.getLong(GamesContract.Periods.COLUMN_RUNNING_MILLIS_INDEX),
periodsCursor.getLong(GamesContract.Periods.COLUMN_SPRINTING_MILLIS_INDEX)
);
game.addPeriod(period);
}
periodsCursor.close();
...
虽然游戏和时段的数量不会很大(可能是 100 次),但每场比赛可能有 50 个事件,每场比赛有 2000 个地点。
我怎样才能更有效地做到这一点?我想到的可能性是:
- 一个大型的多连接查询,然后我必须对其进行排序。我对这种类型的 SQL 很满意,假设 SQLite 能有效地处理它。我不喜欢这个主要是因为时间段、事件、位置和子表,所以我实际上会去规范化并造成巨大的混乱。
- 将期间、事件等的 selectionArgs 扩展为我拥有的 10 或 100 个游戏的动态列表
- 不知何故提高了我所拥有的东西的效率并将这些变成了异步查询
任何建议或指示表示赞赏。