简体   繁体   English

Postgresql查询非常慢

[英]Postgresql Query is very Slow

I have a table with 300000 rows and when i run a simple query like 我有一个300000行的表,当我运行一个简单的查询,如

   select * from diario_det;

it leaves 41041 ms to return rows. 它留下41041毫秒返回行。 It's fine that? 没关系? How i can optimize the query? 我如何优化查询?

I use Postgresql 9.3 in Centos 7. 我在Centos 7中使用Postgresql 9.3。

Here's is my table 这是我的桌子

CREATE TABLE diario_det
(
  cod_empresa numeric(2,0) NOT NULL,
  nro_asiento numeric(8,0) NOT NULL,
  nro_secue_pase numeric(4,0) NOT NULL,
  descripcion_pase character varying(150) NOT NULL,
  monto_debe numeric(16,3),
  monto_haber numeric(16,3),
  estado character varying(1) NOT NULL,
  cod_pcuenta character varying(15) NOT NULL,
  cod_local numeric(2,0) NOT NULL,
  cod_centrocosto numeric(4,0) NOT NULL,
  cod_ejercicio numeric(4,0) NOT NULL,
  nro_comprob character varying(15),
  conciliado_por character varying(10),
  CONSTRAINT fk_diario_det_cab FOREIGN KEY (cod_empresa, cod_local, cod_ejercicio, nro_asiento)
      REFERENCES diario_cab (cod_empresa, cod_local, cod_ejercicio, nro_asiento) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_diario_det_pc FOREIGN KEY (cod_empresa, cod_pcuenta)
      REFERENCES plan_cuenta (cod_empresa, cod_pcuenta) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
 )
WITH (
   OIDS=TRUE
);
 ALTER TABLE diario_det
   OWNER TO postgres;

-- Index: pk_diario_det_ax

-- DROP INDEX pk_diario_det_ax;

 CREATE INDEX pk_diario_det_ax
   ON diario_det
   USING btree
   (cod_pcuenta COLLATE pg_catalog."default", cod_local, estado COLLATE pg_catalog."default");

Very roughly size of one row is 231 bytes, times 300000... It's 69300000 bytes (~69MB) that has to be transferred from server to client. 非常粗略的一行大小是231字节,乘以300000 ...它必须从服务器传输到客户端的69300000字节(~69MB)。

I think that 41 seconds is a bit long, but still the query has to be slow because of amount of data that has to be loaded from disk and transferred. 我认为41秒有点长,但由于必须从磁盘加载并传输的数据量,查询必须很慢。

You can optimise query by 您可以优化查询

  • selecting just columns you that are going to use not all of them (if you need just cod_empresa it would reduce total amount of transferred data to ~1.2MB, but server would still have to iterate trough all records - slow) 只选择列您打算使用不是所有的人(如果你只需要cod_empresa将减少传输的数据总量〜1.2MB,但服务器仍然有低谷遍历所有记录-慢)
  • filter only rows that are going to use - using WHERE on columns with indexes can really speed the query up 筛选打算使用行-使用WHERE列上带索引才能真正加快了查询

If you want to know what is happening in your query, play around with EXPLAIN and EXPLAIN EXECUTE . 如果您想知道查询中发生了什么,请使用EXPLAINEXPLAIN EXECUTE

Also, if you're running dedicated database server, be sure to configure it properly to use a lot of system resources . 此外,如果您正在运行专用数据库服务器,请确保正确配置它以使用大量系统资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM