如果它是多列索引的一部分,则不会添加 Alter table 中的单个索引
Single index in Alter table wont get added if its part of a multi column 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')
)
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;
除主要内容外,food 中的其他列是:
fruits, portion, gender, weight
如果这些列上没有单个索引,或者如果它们中的任何一个是多列索引的一部分但位于右侧,如(性别,绿色),我只想添加一个索引。为确保单个列索引不存在,我需要对该列进行行数检查。我该如何修改这个脚本来做到这一点?
使用 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;
到目前为止,我在存储过程中有这个:
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;
除主要内容外,food 中的其他列是:
fruits, portion, gender, weight
如果这些列上没有单个索引,或者如果它们中的任何一个是多列索引的一部分但位于右侧,如(性别,绿色),我只想添加一个索引。为确保单个列索引不存在,我需要对该列进行行数检查。我该如何修改这个脚本来做到这一点?
使用 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;