Created
March 26, 2013 01:20
-
-
Save onigra/5242364 to your computer and use it in GitHub Desktop.
インデックスが重複してないかチェックするSQL
This file contains hidden or 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
select | |
Z.TABLE_SCHEMA, | |
Z.TABLE_NAME, | |
Z.idx_column, | |
Z.CNT | |
from | |
( | |
select | |
Y.TABLE_SCHEMA, | |
Y.TABLE_NAME, | |
Y.idx_column, | |
count(Y.idx_column) as CNT | |
from | |
( | |
select | |
X.TABLE_SCHEMA, | |
X.TABLE_NAME, | |
X.INDEX_NAME, | |
X.NON_UNIQUE, | |
rtrim(group_concat(concat(X.COLLATION,X.COLUMN_NAME) order by X.SEQ_IN_INDEX asc SEPARATOR '')) as idx_column | |
from | |
( | |
select | |
Y.TABLE_SCHEMA, | |
Y.TABLE_NAME, | |
Y.INDEX_NAME, | |
Y.NON_UNIQUE, | |
case | |
when Y.COLLATION = 'A' then '+' | |
when Y.COLLATION = 'D' then '-' | |
else '*' | |
end as COLLATION, | |
Y.COLUMN_NAME, | |
Y.SEQ_IN_INDEX | |
from | |
( | |
select * from information_schema.STATISTICS | |
) as Y | |
where | |
Y.TABLE_SCHEMA <> 'mysql' | |
order by | |
Y.TABLE_SCHEMA, | |
Y.TABLE_NAME, | |
Y.INDEX_NAME, | |
Y.SEQ_IN_INDEX | |
) as X | |
group by | |
X.TABLE_SCHEMA, | |
X.TABLE_NAME, | |
X.INDEX_NAME, | |
X.NON_UNIQUE | |
order by | |
X.TABLE_SCHEMA, | |
X.TABLE_NAME, | |
X.INDEX_NAME | |
) as Y | |
group by | |
Y.TABLE_SCHEMA,Y.TABLE_NAME,Y.idx_column | |
) AS Z | |
where | |
Z.CNT > 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment