Created
July 30, 2014 13:23
-
-
Save marshallm/a92b35bc70d48a9bf1b3 to your computer and use it in GitHub Desktop.
MySQL, Index / Fun()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELIMITER $$ | |
DROP PROCEDURE IF EXISTS myschema.create_index_if_not_exists $$ | |
CREATE PROCEDURE myschema.create_index_if_not_exists(in p_tableName VARCHAR(128), in p_indexName VARCHAR(128), in p_columnName VARCHAR(128) ) | |
BEGIN | |
PREPARE stmt FROM 'SELECT @indexCount := COUNT(1) from information_schema.statistics WHERE `table_name` = ? AND `index_name` = ?'; | |
SET @table_name = p_tableName; | |
SET @index_name = p_indexName; | |
EXECUTE stmt USING @table_name, @index_name; | |
DEALLOCATE PREPARE stmt; | |
-- select @indexCount; | |
IF( @indexCount = 0 ) THEN | |
SELECT 'Creating index'; | |
SET @createIndexStmt = CONCAT('CREATE INDEX ', p_indexName, ' ON ', p_tableName, ' ( ', p_columnName ,')'); | |
PREPARE stmt FROM @createIndexStmt; | |
EXECUTE stmt; | |
DEALLOCATE PREPARE stmt; | |
END IF; | |
END $$ | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment