-- =========================================================
-- SISPROM360 - Modo Desenho CTM conectado ao novo DDL mun_jequie
-- Esta versão NÃO usa mun_jequie.jequie_2025 nos endpoints do Modo Desenho.
-- Tabelas usadas: ctm_lotes, ctm_construcoes, ctm_componentes e historico_edicao_geom.
-- =========================================================

-- Lotes CTM com geometria
CREATE INDEX IF NOT EXISTS idx_ctm_lotes_id_lote
ON mun_jequie.ctm_lotes USING btree (id_lote);

CREATE INDEX IF NOT EXISTS idx_ctm_lotes_seq
ON mun_jequie.ctm_lotes USING btree (sequencial_contribuinte);

CREATE INDEX IF NOT EXISTS idx_ctm_lotes_inscricao
ON mun_jequie.ctm_lotes USING btree (inscricao);

CREATE INDEX IF NOT EXISTS idx_ctm_lotes_status_conf
ON mun_jequie.ctm_lotes USING btree (status_conf);

CREATE INDEX IF NOT EXISTS idx_ctm_lotes_situacao
ON mun_jequie.ctm_lotes USING btree (situacao);

CREATE INDEX IF NOT EXISTS sidx_ctm_lotes_geom
ON mun_jequie.ctm_lotes USING gist (geom);

-- Construções
CREATE INDEX IF NOT EXISTS idx_ctm_construcoes_seq
ON mun_jequie.ctm_construcoes USING btree (sequencial_contribuinte);

CREATE INDEX IF NOT EXISTS idx_ctm_construcoes_id_const
ON mun_jequie.ctm_construcoes USING btree (id_const);

CREATE INDEX IF NOT EXISTS sidx_ctm_construcoes_geom
ON mun_jequie.ctm_construcoes USING gist (geom);

-- Componentes
CREATE INDEX IF NOT EXISTS idx_ctm_componentes_seq
ON mun_jequie.ctm_componentes USING btree (sequencial_contribuinte);

CREATE INDEX IF NOT EXISTS idx_ctm_componentes_id_comp
ON mun_jequie.ctm_componentes USING btree (id_comp);

CREATE INDEX IF NOT EXISTS sidx_ctm_componentes_geom
ON mun_jequie.ctm_componentes USING gist (geom);

-- Histórico
CREATE INDEX IF NOT EXISTS idx_historico_edicao_geom_seq
ON mun_jequie.historico_edicao_geom USING btree (sequencial_contribuinte);

CREATE INDEX IF NOT EXISTS idx_historico_edicao_geom_criado
ON mun_jequie.historico_edicao_geom USING btree (criado_em);

CREATE INDEX IF NOT EXISTS idx_historico_edicao_geom_entidade
ON mun_jequie.historico_edicao_geom USING btree (entidade, id_entidade);

ANALYZE mun_jequie.ctm_lotes;
ANALYZE mun_jequie.ctm_construcoes;
ANALYZE mun_jequie.ctm_componentes;
ANALYZE mun_jequie.historico_edicao_geom;
