到目前为止,我已经在存储过程中拥有这个:
IF NOT EXISTS(SELECT 1 FROM information_schema.statistics WHERE TABLE_SCHEMA=DATABASE()
AND TABLE_NAME= 'food' AND (INDEX_NAME='ix_meat' OR COLUMN_NAME='meat')
)
THEN
CREATE INDEX ix_meat ON food (meat);
END IF;
IF NOT EXISTS(SELECT 1 FROM information_schema.statistics WHERE TABLE_SCHEMA=DATABASE()
AND TABLE_NAME= 'food' AND (INDEX_NAME='ix_greens' OR COLUMN_NAME='greens')
)
THEN
CREATE INDEX ix_greens ON food(greens);
END IF;
除主要食品外,其他食品中的其他食品是:
水果,部分,性别,体重
如果这些列上没有单个索引,或者它们中的任何一个都是多列索引的一部分,但在右侧(如性别,绿色),则只添加一个索引。为了确保不存在单个列索引,我需要在该列上进行行计数检查。我如何修改此脚本来做到这一点?
seq_in_index
列可知道该列在索引中的位置。您要添加索引,除非它是索引中的第一列。IF NOT EXISTS(SELECT 1 FROM information_schema.statistics WHERE TABLE_SCHEMA=DATABASE()
AND TABLE_NAME= 'food' AND (INDEX_NAME='ix_meat' OR (COLUMN_NAME='meat' AND SEQ_IN_INDEX = 1))
)
THEN
CREATE INDEX ix_meat ON food (meat);
END IF;
IF NOT EXISTS(SELECT 1 FROM information_schema.statistics WHERE TABLE_SCHEMA=DATABASE()
AND TABLE_NAME= 'food' AND (INDEX_NAME='ix_greens' OR (COLUMN_NAME='greens' AND SEQ_IN_INDEX = 1))
)
THEN
CREATE INDEX ix_greens ON food(greens);
END IF;