我的删除查询在 Postgres 中运行缓慢
delete FROM Tab1 WHERE vdate =TO_DATE('20991231', 'yyyymmdd')
AND DM = 'DK' AND T_CLASS_ID ='VK'and vk_id ='SM';
解释计划是
"Delete on Tab1 (cost=24181.99..172861.78 rows=0 width=0)"
" -> Bitmap Heap Scan on Tab1 (cost=24181.99..172861.78 rows=456808 width=6)"
" Recheck Cond: ((T_class_id)::text = 'VK'::text)"
" Filter: (((dm_id)::text = 'DK'::text) AND ((VK_id)::text = 'SM'::text) AND (vdate = to_date('20991231'::text, 'yyyymmdd'::text)))"
" -> Bitmap Index Scan on idx_T_class_id (cost=0.00..24067.79 rows=1482581 width=0)"
" Index Cond: ((T_class_id)::text = 'VK'::text)"
创建索引仍然很慢。请多多指教
表格的DDL脚本是
CREATE TABLE IF NOT EXISTS public.tab1
(
ID integer NOT NULL DEFAULT nextval('id_seq'::regcs),
id_name character varying(255) COLLATE pg_catalog."default" NOT NULL,
dm_id character varying(100) COLLATE pg_catalog."default" NOT NULL,
vm_id character varying(255) COLLATE pg_catalog."default" NOT NULL,
id_cs_id character varying(100) COLLATE pg_catalog."default" NOT NULL,
st_id character varying(1) COLLATE pg_catalog."default",
id_code character varying(100) COLLATE pg_catalog."default" NOT NULL,
valid_start_date date NOT NULL,
valid_end_date date NOT NULL,
ir character varying(1) COLLATE pg_catalog."default",
CONSTRAINT id_pkey PRIMARY KEY (id_id),
CONSTRAINT fpk_id_cs FOREIGN KEY (id_cs_id)
REFERENCES public.id_cs (id_cs_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT fpk_id_dm FOREIGN KEY (dm_id)
REFERENCES public.dm (dm_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT fpk_id_vm FOREIGN KEY (vm_id)
REFERENCES public.vm (vm_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.id
OWNER to cdrdb;
-- Index: idx_id_cs_id
-- DROP INDEX IF EXISTS public.idx_id_cs_id;
CREATE INDEX IF NOT EXISTS idx_id_cs_id
ON public.id USING btree
(id_cs_id COLLATE pg_catalog."default" ASC NULLS LAST)
TABLESPACE pg_default;
-- Index: idx_id_code
-- DROP INDEX IF EXISTS public.idx_id_code;
CREATE INDEX IF NOT EXISTS idx_id_code
ON public.id USING btree
(id_code COLLATE pg_catalog."default" ASC NULLS LAST)
TABLESPACE pg_default;
-- Index: idx_id_dm_id
-- DROP INDEX IF EXISTS public.idx_id_dm_id;
CREATE INDEX IF NOT EXISTS idx_id_dm_id
ON public.id USING btree
(dm_id COLLATE pg_catalog."default" ASC NULLS LAST)
TABLESPACE pg_default;
-- Index: idx_id_vm_id
-- DROP INDEX IF EXISTS public.idx_id_vm_id;
CREATE INDEX IF NOT EXISTS idx_id_vm_id
ON public.id USING btree
(vm_id COLLATE pg_catalog."default" ASC NULLS LAST)
TABLESPACE pg_default;