Skip to content

Commit

Permalink
Add some indexes to SQLite tables
Browse files Browse the repository at this point in the history
Added LOCATION_KEY index bath weather_values and weather_conditions tables.
LOCATION_KEY is used selects/joins with both tables.

In example case this has no real performance effect,
but it's always good habit to have indexes for most commonly used queries.
  • Loading branch information
Jukka Pihl committed Jun 27, 2015
1 parent 9edef5d commit 5bb4a20
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public class WeatherDatabaseHelper extends SQLiteOpenHelper {
+ WeatherContract.WeatherValuesEntry.COLUMN_EXPIRATION_TIME
+ " REAL)";

/**
* index for LOCATION_EKY
*/

private static final String CREATE_INDEX_WEATHER_VALUES_LOCATION_KEY_IDX =
"CREATE INDEX " + WeatherContract.WeatherValuesEntry.WEATHER_VALUES_TABLE_NAME + "_" + WeatherContract.WeatherValuesEntry.COLUMN_LOCATION_KEY + "_idx"
+ " ON "
+ WeatherContract.WeatherValuesEntry.WEATHER_VALUES_TABLE_NAME
+ "("
+ WeatherContract.WeatherValuesEntry.COLUMN_LOCATION_KEY
+ ")";


/**
* SQL statement used to create the Weather Conditions table.
*/
Expand All @@ -83,6 +96,20 @@ public class WeatherDatabaseHelper extends SQLiteOpenHelper {
+ WeatherContract.WeatherConditionsEntry.COLUMN_ICON
+ " TEXT) ";


/**
* index for weather conditions LOCATION_KEY, which is used for join queries to
*/


private static final String CREATE_INDEX_WEATHER_CONDITIONS_LOCATION_KEY_IDX =
"CREATE INDEX " + WeatherContract.WeatherConditionsEntry.WEATHER_CONDITIONS_TABLE_NAME + "_" + WeatherContract.WeatherConditionsEntry.COLUMN_LOCATION_KEY + "_idx"
+ " ON "
+ WeatherContract.WeatherConditionsEntry.WEATHER_CONDITIONS_TABLE_NAME
+ "("
+ WeatherContract.WeatherConditionsEntry.COLUMN_LOCATION_KEY
+ ")";

/**
* Constructor - initialize database name and version, but don't
* actually construct the database (which is done in the
Expand All @@ -109,6 +136,10 @@ public void onCreate(SQLiteDatabase db) {
// Create the tables.
db.execSQL(CREATE_TABLE_WEATHER_VALUES);
db.execSQL(CREATE_TABLE_WEATHER_CONDITIONS);

// create index for LOCATION_KEY in both tables. (used in most queries)
db.execSQL(CREATE_INDEX_WEATHER_VALUES_LOCATION_KEY_IDX);
db.execSQL(CREATE_INDEX_WEATHER_CONDITIONS_LOCATION_KEY_IDX);
}

/**
Expand Down

0 comments on commit 5bb4a20

Please sign in to comment.