我有一个SQL查询,我想优化以改善结果。
查询:
SELECT
twm.id AS id,
twm.fecha_id AS fecha,
twm.nombre_busqueda AS busquedas,
twm.tipo_mencion_leyenda AS tipo_mencion,
twm.sentimiento AS sentimiento,
CASE
WHEN sum(twm.menciones) IS null THEN 0
ELSE sum(twm.menciones)
END AS menciones,
twm.tipo_source_nombre AS sources,
twm.tipo_de_site_leyenda AS typeof
FROM
dashboards_tablaagregadawebdiaria twm
WHERE
twm.corporativa = FALSE
AND twm.cuenta_id = 115
AND twm.tipo_buzz_id = 1
AND twm.fecha_id >= 20150831
AND twm.fecha_id <= 20150929
GROUP BY
twm.id,fecha,
busquedas,
tipo_mencion,
sentimiento,
sources,
typeof
解释分析的输出如下:
GroupAggregate (cost=9052.86..9528.69 rows=13595 width=50) (actual time=45.070..56.073 rows=15006 loops=1)
-> Sort (cost=9052.86..9086.85 rows=13595 width=50) (actual time=45.061..47.941 rows=15006 loops=1)
Sort Key: id, fecha_id, nombre_busqueda, tipo_mencion_leyenda, sentimiento, tipo_source_nombre, tipo_de_site_leyenda
Sort Method: external merge Disk: 1008kB
-> Bitmap Heap Scan on dashboards_tablaagregadawebdiaria twm (cost=910.51..7654.01 rows=13595 width=50) (actual time=3.491..31.813 rows=15006 loops=1)
Recheck Cond: (cuenta_id = 115)
Filter: ((NOT corporativa) AND (fecha_id >= 20150831) AND (fecha_id <= 20150929) AND (tipo_buzz_id = 1))
-> Bitmap Index Scan on dashboards_tablaagregadawebdiaria_cuenta_id (cost=0.00..907.12 rows=21175 width=0) (actual time=3.127..3.127 rows=21474 loops=1)
Index Cond: (cuenta_id = 115)
Total runtime: 57.057 ms
模型如下:
CREATE TABLE dashboards_tablaagregadawebdiaria
(
id serial NOT NULL,
busqueda_id integer NOT NULL,
fecha_id integer NOT NULL,
calidad_id integer NOT NULL,
tipo_de_site_id integer NOT NULL,
tipo_buzz_id integer NOT NULL,
visibilidad_id integer NOT NULL,
menciones integer,
tipo_menciones_id integer,
sentimiento double precision NOT NULL,
termino_encontrado character varying(200),
pais_id integer,
idioma_id integer,
source_id integer NOT NULL,
cuenta_id integer,
corporativa boolean,
nombre_busqueda character varying(150),
tipo_source_id integer,
tipo_source_nombre character varying(100),
calidad_leyenda character varying(200),
visibilidad_leyenda character varying(200),
tipo_buzz_leyenda character varying(200),
tipo_mencion_leyenda character varying(200),
tipo_de_site_leyenda character varying(200),
CONSTRAINT dashboards_tablaagregadawebdiaria_pkey PRIMARY KEY (id),
CONSTRAINT busqueda_id_refs_id_a3e56469 FOREIGN KEY (busqueda_id)
REFERENCES busquedas_busqueda (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT calidad_id_refs_id_60e9ae3b FOREIGN KEY (calidad_id)
REFERENCES busquedas_calidad (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT cuenta_id_refs_id_1473e5b2 FOREIGN KEY (cuenta_id)
REFERENCES usuarios_cuenta (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT fecha_id_refs_id_614e1caa FOREIGN KEY (fecha_id)
REFERENCES dashboards_tiempo (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT idioma_id_refs_id_dea0e30f FOREIGN KEY (idioma_id)
REFERENCES usuarios_idioma (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT pais_id_refs_id_c3a64f5e FOREIGN KEY (pais_id)
REFERENCES usuarios_pais (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT source_id_refs_id_b20099a3 FOREIGN KEY (source_id)
REFERENCES busquedas_source (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT tipo_buzz_id_refs_id_06719001 FOREIGN KEY (tipo_buzz_id)
REFERENCES dashboards_tipo_buzz (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT tipo_de_site_id_refs_id_74d8ca3e FOREIGN KEY (tipo_de_site_id)
REFERENCES dashboards_tipo_site (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT tipo_menciones_id_refs_id_6aa6306a FOREIGN KEY (tipo_menciones_id)
REFERENCES dashboards_tipo_mencion (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT tipo_source_id_refs_id_38d1acb3 FOREIGN KEY (tipo_source_id)
REFERENCES busquedas_tipo_source (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT visibilidad_id_refs_id_9f3d8b04 FOREIGN KEY (visibilidad_id)
REFERENCES dashboards_visibilidad_web (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT dashboards_tablaagregadawebdiaria_busqueda_id_5df9ff43_uniq UNIQUE (busqueda_id, fecha_id, calidad_id, tipo_de_site_id, tipo_buzz_id, visibilidad_id, tipo_menciones_id, termino_encontrado, pais_id, idioma_id, sentimiento, source_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE dashboards_tablaagregadawebdiaria
OWNER TO postgres;
提高查询时间的任何帮助或建议?我已经创建了跟随索引,但还不够。
CREATE INDEX dashboards_tablaagregadawebdiaria_cuenta_id
ON dashboards_tablaagregadawebdiaria
USING btree
(cuenta_id);
由于
答案 0 :(得分:0)
查询中只有一个表格带有std::cout
子句。最佳指数是复合指数:
std::string