Bug 449086 - Create temporary tables for frequently used places tables

This changeset creates two temporary tables that will be used when inserting
data into moz_places and moz_historyvisits.  This will reduce the number of
writes as well as the number of fsyncs that happen.
r=dietrich
This commit is contained in:
Shawn Wilsher 2008-10-08 13:10:04 -04:00
parent bbf720ef3a
commit 871b5a0213
4 changed files with 253 additions and 4 deletions

View File

@ -878,6 +878,10 @@ nsNavHistory::InitDB(PRInt16 *aMadeChanges)
// DO NOT PUT ANY SCHEMA-MODIFYING THINGS HERE
rv = InitTempTables();
NS_ENSURE_SUCCESS(rv, rv);
rv = InitViews();
NS_ENSURE_SUCCESS(rv, rv);
rv = InitFunctions();
NS_ENSURE_SUCCESS(rv, rv);
rv = InitStatements();
@ -956,6 +960,88 @@ NS_IMETHODIMP mozStorageFunctionGetUnreversedHost::OnFunctionCall(
return NS_OK;
}
nsresult
nsNavHistory::InitTempTables()
{
nsresult rv;
// moz_places_temp
rv = mDBConn->ExecuteSimpleSQL(CREATE_MOZ_PLACES_TEMP);
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
"CREATE UNIQUE INDEX moz_places_temp_url_uniqueindex ON moz_places_temp (url)"));
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
"CREATE INDEX moz_places_temp_faviconindex ON moz_places_temp (favicon_id)"));
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
"CREATE INDEX moz_places_temp_hostindex ON moz_places_temp (rev_host)"));
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
"CREATE INDEX moz_places_temp_visitcount ON moz_places_temp (visit_count)"));
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
"CREATE INDEX moz_places_temp_frecencyindex ON moz_places_temp (frecency)"));
NS_ENSURE_SUCCESS(rv, rv);
// moz_historyvisits_temp
rv = mDBConn->ExecuteSimpleSQL(CREATE_MOZ_HISTORYVISITS_TEMP);
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
"CREATE INDEX moz_historyvisits_temp_placedateindex "
"ON moz_historyvisits_temp (place_id, visit_date)"));
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
"CREATE INDEX moz_historyvisits_temp_fromindex "
"ON moz_historyvisits_temp (from_visit)"));
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
"CREATE INDEX moz_historyvisits_temp_dateindex "
"ON moz_historyvisits_temp (visit_date)"));
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
nsresult
nsNavHistory::InitViews()
{
nsresult rv;
// moz_places_view
rv = mDBConn->ExecuteSimpleSQL(CREATE_MOZ_PLACES_VIEW);
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(CREATE_PLACES_VIEW_INSERT_TRIGGER);
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(CREATE_PLACES_VIEW_DELETE_TRIGGER);
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(CREATE_PLACES_VIEW_UPDATE_TRIGGER);
NS_ENSURE_SUCCESS(rv, rv);
// moz_historyvisits_view
rv = mDBConn->ExecuteSimpleSQL(CREATE_MOZ_HISTORYVISITS_VIEW);
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(CREATE_HISTORYVISITS_VIEW_INSERT_TRIGGER);
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(CREATE_HISTORYVISITS_VIEW_DELETE_TRIGGER);
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->ExecuteSimpleSQL(CREATE_HISTORYVISITS_VIEW_UPDATE_TRIGGER);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
nsresult
nsNavHistory::InitFunctions()
{

View File

@ -463,6 +463,8 @@ protected:
* The database was migrated to a new version.
*/
nsresult InitDB(PRInt16 *aMadeChanges);
nsresult InitTempTables();
nsresult InitViews();
nsresult InitFunctions();
nsresult InitStatements();
nsresult ForceMigrateBookmarksDB(mozIStorageConnection *aDBConn);

View File

@ -39,8 +39,8 @@
#ifndef __nsPlacesTables_h__
#define __nsPlacesTables_h__
#define CREATE_MOZ_PLACES NS_LITERAL_CSTRING( \
"CREATE TABLE moz_places ( " \
#define CREATE_MOZ_PLACES_BASE(__name, __temporary) NS_LITERAL_CSTRING( \
"CREATE " __temporary " TABLE " __name " ( " \
" id INTEGER PRIMARY KEY" \
", url LONGVARCHAR" \
", title LONGVARCHAR" \
@ -52,9 +52,18 @@
", frecency INTEGER DEFAULT -1 NOT NULL" \
")" \
)
#define CREATE_MOZ_PLACES CREATE_MOZ_PLACES_BASE("moz_places", "")
#define CREATE_MOZ_PLACES_TEMP CREATE_MOZ_PLACES_BASE("moz_places_temp", "TEMP")
#define CREATE_MOZ_PLACES_VIEW NS_LITERAL_CSTRING( \
"CREATE TEMPORARY VIEW moz_places_view AS " \
"SELECT * FROM moz_places_temp " \
"UNION ALL " \
"SELECT * FROM moz_places " \
"WHERE id NOT IN (SELECT id FROM moz_places_temp) " \
)
#define CREATE_MOZ_HISTORYVISITS NS_LITERAL_CSTRING( \
"CREATE TABLE moz_historyvisits (" \
#define CREATE_MOZ_HISTORYVISITS_BASE(__name, __temporary) NS_LITERAL_CSTRING( \
"CREATE " __temporary " TABLE " __name " (" \
" id INTEGER PRIMARY KEY" \
", from_visit INTEGER" \
", place_id INTEGER" \
@ -63,6 +72,17 @@
", session INTEGER" \
")" \
)
#define CREATE_MOZ_HISTORYVISITS \
CREATE_MOZ_HISTORYVISITS_BASE("moz_historyvisits", "")
#define CREATE_MOZ_HISTORYVISITS_TEMP \
CREATE_MOZ_HISTORYVISITS_BASE("moz_historyvisits_temp", "TEMP")
#define CREATE_MOZ_HISTORYVISITS_VIEW NS_LITERAL_CSTRING( \
"CREATE TEMPORARY VIEW moz_historyvisits_view AS " \
"SELECT * FROM moz_historyvisits_temp " \
"UNION ALL " \
"SELECT * FROM moz_historyvisits " \
"WHERE id NOT IN (SELECT id FROM moz_historyvisits_temp) " \
)
#define CREATE_MOZ_INPUTHISTORY NS_LITERAL_CSTRING( \
"CREATE TABLE moz_inputhistory (" \

View File

@ -93,4 +93,145 @@
"END" \
)
/**
* This trigger allows for an insertion into moz_places_view. It enters the new
* data into the temporary table, ensuring that the new id is one greater than
* the largest id value found.
*/
#define CREATE_PLACES_VIEW_INSERT_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_places_view_insert_trigger " \
"INSTEAD OF INSERT " \
"ON moz_places_view " \
"BEGIN " \
"INSERT INTO moz_places_temp ( " \
"id, url, title, rev_host, visit_count, hidden, typed, favicon_id, " \
"frecency " \
") " \
"VALUES (MAX((SELECT IFNULL(MAX(id), 0) FROM moz_places_temp), " \
"(SELECT IFNULL(MAX(id), 0) FROM moz_places)) + 1, " \
"NEW.url, NEW.title, NEW.rev_host, " \
"IFNULL(NEW.visit_count, 0), " /* enforce having a value */ \
"NEW.hidden, NEW.typed, NEW.favicon_id, NEW.frecency);" \
"END" \
)
/**
* This trigger allows for the deletion of a record in moz_places_view. It
* removes any entry in the temporary table, and any entry in the permanent
* table as well.
*/
#define CREATE_PLACES_VIEW_DELETE_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_places_view_delete_trigger " \
"INSTEAD OF DELETE " \
"ON moz_places_view " \
"BEGIN " \
"DELETE FROM moz_places_temp " \
"WHERE id = OLD.id; " \
"DELETE FROM moz_places " \
"WHERE id = OLD.id; " \
"END" \
)
/**
* This trigger allows for updates to a record in moz_places_view. It first
* copies the row from the permanent table over to the temp table if it does not
* exist in the temporary table. Then, it will update the temporary table with
* the new data.
*/
#define CREATE_PLACES_VIEW_UPDATE_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_places_view_update_trigger " \
"INSTEAD OF UPDATE " \
"ON moz_places_view " \
"BEGIN " \
"INSERT INTO moz_places_temp " \
"SELECT * " \
"FROM moz_places " \
"WHERE id = OLD.id " \
"AND id NOT IN (SELECT id FROM moz_places_temp); " \
"UPDATE moz_places_temp " \
"SET url = IFNULL(NEW.url, OLD.url), " \
"title = IFNULL(NEW.title, OLD.title), " \
"rev_host = IFNULL(NEW.rev_host, OLD.rev_host), " \
"visit_count = IFNULL(NEW.visit_count, OLD.visit_count), " \
"hidden = IFNULL(NEW.hidden, OLD.hidden), " \
"typed = IFNULL(NEW.typed, OLD.typed), " \
"favicon_id = IFNULL(NEW.favicon_id, OLD.favicon_id), " \
"frecency = IFNULL(NEW.frecency, OLD.frecency) " \
"WHERE id = OLD.id; " \
"END" \
)
/**
* This trigger allows for an insertion into moz_historyvisits_view. It enters
* the new data into the temporary table, ensuring that the new id is one
* greater than the largest id value found. It then updates moz_places_view
* with the new visit count.
*/
#define CREATE_HISTORYVISITS_VIEW_INSERT_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_historyvisits_view_insert_trigger " \
"INSTEAD OF INSERT " \
"ON moz_historyvisits_view " \
"BEGIN " \
"INSERT INTO moz_historyvisits_temp ( " \
"id, from_visit, place_id, visit_date, visit_type, session " \
") " \
"VALUES (MAX((SELECT IFNULL(MAX(id), 0) FROM moz_historyvisits_temp), " \
"(SELECT IFNULL(MAX(id), 0) FROM moz_historyvisits)) + 1, " \
"NEW.from_visit, NEW.place_id, NEW.visit_date, NEW.visit_type, " \
"NEW.session); " \
"UPDATE moz_places_view " \
"SET visit_count = visit_count + 1 " \
"WHERE id = NEW.place_id " \
"AND NEW.visit_type NOT IN (0, 4, 7); " /* invalid, EMBED, DOWNLOAD */ \
"END" \
)
/**
* This trigger allows for the deletion of a record in moz_historyvisits_view.
* It removes any entry in the temporary table, and removes any entry in the
* permanent table as well. It then updates moz_places_view with the new visit
* count.
*/
#define CREATE_HISTORYVISITS_VIEW_DELETE_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_historyvisits_view_delete_trigger " \
"INSTEAD OF DELETE " \
"ON moz_historyvisits_view " \
"BEGIN " \
"DELETE FROM moz_historyvisits_temp " \
"WHERE id = OLD.id; " \
"DELETE FROM moz_historyvisits " \
"WHERE id = OLD.id; " \
"UPDATE moz_places_view " \
"SET visit_count = visit_count - 1 " \
"WHERE moz_places_view.id = OLD.place_id " \
"AND OLD.visit_type NOT IN (0, 4, 7); " /* invalid, EMBED, DOWNLOAD */ \
"END" \
)
/**
* This trigger allows for updates to a record in moz_historyvisits_view. It
* first copies the row from the permanent table over to the temp table if it
* does not exist in the temporary table. Then it will update the temporary
* table with the new data.
*/
#define CREATE_HISTORYVISITS_VIEW_UPDATE_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_historyvisits_view_update_trigger " \
"INSTEAD OF UPDATE " \
"ON moz_historyvisits_view " \
"BEGIN " \
"INSERT INTO moz_historyvisits_temp " \
"SELECT * " \
"FROM moz_historyvisits " \
"WHERE id = OLD.id " \
"AND id NOT IN (SELECT id FROM moz_historyvisits_temp); " \
"UPDATE moz_historyvisits_temp " \
"SET from_visit = IFNULL(NEW.from_visit, OLD.from_visit), " \
"place_id = IFNULL(NEW.place_id, OLD.place_id), " \
"visit_date = IFNULL(NEW.visit_date, OLD.visit_date), " \
"visit_type = IFNULL(NEW.visit_type, OLD.visit_type), " \
"session = IFNULL(NEW.session, OLD.session) " \
"WHERE id = OLD.id; " \
"END" \
)
#endif // __nsPlacesTriggers_h__