您的位置:首页 > 其它

postgres批量新建索引

2014-03-07 00:00 411 查看
摘要: 通过存储过程动态批量创建索引

CREATE OR REPLACE FUNCTION create_love_index()
RETURNS int AS
$$
DECLARE
count int;
tbname text;
lan text;
types text[];
type text;
codes text[];
code text;
index_name text;
BEGIN
count := 0;
lan := 'en';
types := array['cat', 'fish'];
codes := array['0_9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
FOREACH type IN ARRAY types LOOP
FOREACH code IN ARRAY codes LOOP
tbname = 'love_' || lan || '_' || type || '_' || code;
-- idx_love_en_cat_a_created
index_name = 'idx_' || tbname || '_created';
IF NOT EXISTS(select indexname from pg_indexes WHERE
schemaname = 'cat_l_fish' AND  indexname = index_name) THEN
RAISE NOTICE 'indexname: %.', index_name;
EXECUTE 'CREATE INDEX ' || index_name || ' ON cat_l_fish.' || tbname || ' (created)';
count := count + 1;
END IF;
END LOOP;
END LOOP;

RETURN count;
END;
$$
LANGUAGE 'plpgsql' VOLATILE;

-- SELECT create_love_index();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  postgre create index