From 3062e6bd8de1dd4246f9fd5e1db0e8bf788105c8 Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Sat, 10 Dec 2016 20:23:19 +0000 Subject: [PATCH] Add index which nerfs a query run on reservation Previously doing a full table scan which was executing in about 660ms, now taking about 10ms. --- sql/patches/patch17-idx-pend-reserved.sql | 63 +++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 sql/patches/patch17-idx-pend-reserved.sql diff --git a/sql/patches/patch17-idx-pend-reserved.sql b/sql/patches/patch17-idx-pend-reserved.sql new file mode 100644 index 000000000..b54a8607e --- /dev/null +++ b/sql/patches/patch17-idx-pend-reserved.sql @@ -0,0 +1,63 @@ +-- ----------------------------------------------------------------------------- +-- Hey! +-- +-- This is a new patch-creation script which SHOULD stop double-patching and +-- running patches out-of-order. +-- +-- If you're running patches, please close this file, and run this from the +-- command line: +-- $ mysql -u USERNAME -p SCHEMA < patchXX-this-file.sql +-- where: +-- USERNAME = a user with CREATE/ALTER access to the schema +-- SCHEMA = the schema to run the changes against +-- patch-XX-this-file.sql = this file +-- +-- If you are writing patches, you need to copy this template to a numbered +-- patch file, update the patchversion variable, and add the SQL code to upgrade +-- the database where indicated below. + +DROP PROCEDURE IF EXISTS SCHEMA_UPGRADE_SCRIPT; +DELIMITER ';;' +CREATE PROCEDURE SCHEMA_UPGRADE_SCRIPT() BEGIN + -- ------------------------------------------------------------------------- + -- Developers - set the number of the schema patch here! + -- ------------------------------------------------------------------------- + DECLARE patchversion INT DEFAULT 17; + -- ------------------------------------------------------------------------- + -- working variables + DECLARE currentschemaversion INT DEFAULT 0; + DECLARE lastversion INT; + + -- check the schema has a version table + IF NOT EXISTS (SELECT * FROM information_schema.tables WHERE table_name = 'schemaversion' AND table_schema = DATABASE()) THEN + SIGNAL SQLSTATE '45000' SET message_text = 'Please ensure patches are run in order! This database does not have a schemaversion table.'; + END IF; + + -- get the current version + SELECT version INTO currentschemaversion FROM schemaversion; + + -- check schema is not ahead of this patch + IF currentschemaversion >= patchversion THEN + SIGNAL SQLSTATE '45000' SET message_text = 'This patch has already been applied!'; + END IF; + + -- check schema is up-to-date + SET lastversion = patchversion - 1; + IF currentschemaversion != lastversion THEN + SET @message_text = CONCAT('Please ensure patches are run in order! This patch upgrades to version ', patchversion, ', but the database is not version ', lastversion); + SIGNAL SQLSTATE '45000' SET message_text = @message_text; + END IF; + + -- ------------------------------------------------------------------------- + -- Developers - put your upgrade statements here! + -- ------------------------------------------------------------------------- + + create index pend_reserved on request (reserved); + + -- ------------------------------------------------------------------------- + -- finally, update the schema version to indicate success + UPDATE schemaversion SET version = patchversion; +END;; +DELIMITER ';' +CALL SCHEMA_UPGRADE_SCRIPT(); +DROP PROCEDURE IF EXISTS SCHEMA_UPGRADE_SCRIPT; \ No newline at end of file