Bug 456029 - optimize the temp table triggers

This changeset makes the triggers work much faster which should greatly reduce
the timings of adding a visit and a bookmark.
r=sdwilsh
This commit is contained in:
Marco Bonardo 2008-10-27 18:52:21 -04:00
Родитель 3f60a72d69
Коммит 3d0d41ee56
1 изменённых файлов: 25 добавлений и 9 удалений

Просмотреть файл

@ -105,17 +105,18 @@
* copies the row from the permanent table over to the temp table if it does not * 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 * exist in the temporary table. Then, it will update the temporary table with
* the new data. * the new data.
* We use INSERT OR IGNORE to avoid looking if the place already exists in the
* temp table.
*/ */
#define CREATE_PLACES_VIEW_UPDATE_TRIGGER NS_LITERAL_CSTRING( \ #define CREATE_PLACES_VIEW_UPDATE_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_places_view_update_trigger " \ "CREATE TEMPORARY TRIGGER moz_places_view_update_trigger " \
"INSTEAD OF UPDATE " \ "INSTEAD OF UPDATE " \
"ON moz_places_view " \ "ON moz_places_view " \
"BEGIN " \ "BEGIN " \
"INSERT INTO moz_places_temp " \ "INSERT OR IGNORE INTO moz_places_temp " \
"SELECT * " \ "SELECT * " \
"FROM moz_places " \ "FROM moz_places " \
"WHERE id = OLD.id " \ "WHERE id = OLD.id; " \
"AND id NOT IN (SELECT id FROM moz_places_temp); " \
"UPDATE moz_places_temp " \ "UPDATE moz_places_temp " \
"SET url = IFNULL(NEW.url, OLD.url), " \ "SET url = IFNULL(NEW.url, OLD.url), " \
"title = IFNULL(NEW.title, OLD.title), " \ "title = IFNULL(NEW.title, OLD.title), " \
@ -134,6 +135,8 @@
* the new data into the temporary table, ensuring that the new id is one * 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 * greater than the largest id value found. It then updates moz_places_view
* with the new visit count. * with the new visit count.
* We use INSERT OR IGNORE to avoid looking if the place already exists in the
* temp table.
*/ */
#define CREATE_HISTORYVISITS_VIEW_INSERT_TRIGGER NS_LITERAL_CSTRING( \ #define CREATE_HISTORYVISITS_VIEW_INSERT_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_historyvisits_view_insert_trigger " \ "CREATE TEMPORARY TRIGGER moz_historyvisits_view_insert_trigger " \
@ -147,7 +150,12 @@
"(SELECT IFNULL(MAX(id), 0) FROM moz_historyvisits)) + 1, " \ "(SELECT IFNULL(MAX(id), 0) FROM moz_historyvisits)) + 1, " \
"NEW.from_visit, NEW.place_id, NEW.visit_date, NEW.visit_type, " \ "NEW.from_visit, NEW.place_id, NEW.visit_date, NEW.visit_type, " \
"NEW.session); " \ "NEW.session); " \
"UPDATE moz_places_view " \ "INSERT OR IGNORE INTO moz_places_temp " \
"SELECT * " \
"FROM moz_places " \
"WHERE id = NEW.place_id " \
"AND NEW.visit_type NOT IN (0, 4, 7); " \
"UPDATE moz_places_temp " \
"SET visit_count = visit_count + 1 " \ "SET visit_count = visit_count + 1 " \
"WHERE id = NEW.place_id " \ "WHERE id = NEW.place_id " \
"AND NEW.visit_type NOT IN (0, 4, 7); " /* invalid, EMBED, DOWNLOAD */ \ "AND NEW.visit_type NOT IN (0, 4, 7); " /* invalid, EMBED, DOWNLOAD */ \
@ -159,6 +167,8 @@
* It removes any entry in the temporary table, and removes any entry in the * 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 * permanent table as well. It then updates moz_places_view with the new visit
* count. * count.
* We use INSERT OR IGNORE to avoid looking if the place already exists in the
* temp table.
*/ */
#define CREATE_HISTORYVISITS_VIEW_DELETE_TRIGGER NS_LITERAL_CSTRING( \ #define CREATE_HISTORYVISITS_VIEW_DELETE_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_historyvisits_view_delete_trigger " \ "CREATE TEMPORARY TRIGGER moz_historyvisits_view_delete_trigger " \
@ -169,9 +179,14 @@
"WHERE id = OLD.id; " \ "WHERE id = OLD.id; " \
"DELETE FROM moz_historyvisits " \ "DELETE FROM moz_historyvisits " \
"WHERE id = OLD.id; " \ "WHERE id = OLD.id; " \
"UPDATE moz_places_view " \ "INSERT OR IGNORE INTO moz_places_temp " \
"SELECT * " \
"FROM moz_places " \
"WHERE id = OLD.place_id " \
"AND OLD.visit_type NOT IN (0, 4, 7); " \
"UPDATE moz_places_temp " \
"SET visit_count = visit_count - 1 " \ "SET visit_count = visit_count - 1 " \
"WHERE moz_places_view.id = OLD.place_id " \ "WHERE id = OLD.place_id " \
"AND OLD.visit_type NOT IN (0, 4, 7); " /* invalid, EMBED, DOWNLOAD */ \ "AND OLD.visit_type NOT IN (0, 4, 7); " /* invalid, EMBED, DOWNLOAD */ \
"END" \ "END" \
) )
@ -181,17 +196,18 @@
* first copies the row from the permanent table over to the temp table if 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 * does not exist in the temporary table. Then it will update the temporary
* table with the new data. * table with the new data.
* We use INSERT OR IGNORE to avoid looking if the visit already exists in the
* temp table.
*/ */
#define CREATE_HISTORYVISITS_VIEW_UPDATE_TRIGGER NS_LITERAL_CSTRING( \ #define CREATE_HISTORYVISITS_VIEW_UPDATE_TRIGGER NS_LITERAL_CSTRING( \
"CREATE TEMPORARY TRIGGER moz_historyvisits_view_update_trigger " \ "CREATE TEMPORARY TRIGGER moz_historyvisits_view_update_trigger " \
"INSTEAD OF UPDATE " \ "INSTEAD OF UPDATE " \
"ON moz_historyvisits_view " \ "ON moz_historyvisits_view " \
"BEGIN " \ "BEGIN " \
"INSERT INTO moz_historyvisits_temp " \ "INSERT OR IGNORE INTO moz_historyvisits_temp " \
"SELECT * " \ "SELECT * " \
"FROM moz_historyvisits " \ "FROM moz_historyvisits " \
"WHERE id = OLD.id " \ "WHERE id = OLD.id; " \
"AND id NOT IN (SELECT id FROM moz_historyvisits_temp); " \
"UPDATE moz_historyvisits_temp " \ "UPDATE moz_historyvisits_temp " \
"SET from_visit = IFNULL(NEW.from_visit, OLD.from_visit), " \ "SET from_visit = IFNULL(NEW.from_visit, OLD.from_visit), " \
"place_id = IFNULL(NEW.place_id, OLD.place_id), " \ "place_id = IFNULL(NEW.place_id, OLD.place_id), " \