postgres内部JOIN查询内存不足

时间:2016-06-11 21:55:48

标签: postgresql out-of-memory inner-join pgadmin

我正在尝试使用pgAdmin3查询数据库,我需要加入表。我使用以下代码:

SELECT table1.species, table1.trait, table1.value, table1.units, table2.id, table2.family, table2.latitude, table2.longitude, table2.species as speciescheck
  FROM table1 INNER JOIN table2
    ON table1.species = table2.species

但是我一直在运行这个错误:

  

内存不足错误

所以我试图将我的结果插入新表中,如下所示:

CREATE TABLE new_table AS
SELECT table1.species, table1.trait, table1.value, table1.units, table2.id, table2.family, table2.latitude, table2.longitude, table2.species as speciescheck
  FROM table1 INNER JOIN table2
    ON table1.species = table2.species

仍然有错误:

ERROR: could not extend file "base/17675/43101.15": No space left on device
SQL state: 53100
Hint: Check free disk space.

我非常非常新(这是我第一次处理PostgreSQL),我想我可以做些什么来优化这个查询并避免这种类型的错误。我没有数据库的权限。谁能帮忙? 提前致谢!

更新: 表1说明

-- Table: table1

-- DROP TABLE table1;

CREATE TABLE table1
(
  species character varying(100),
  trait character varying(50),
  value double precision,
  units character varying(50)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE table1
  OWNER TO postgres;
GRANT ALL ON TABLE table1 TO postgres;
GRANT SELECT ON TABLE table1 TO banco;

-- Index: speciestable1_idx

-- DROP INDEX speciestable1_idx;

CREATE INDEX speciestable1_idx
  ON table1 
  USING btree
  (species COLLATE pg_catalog."default");

-- Index: traittype_idx

-- DROP INDEX traittype_idx;

CREATE INDEX traittype_idx
  ON table1
  USING btree
  (trait COLLATE pg_catalog."default");

和table2为:

-- Table: table2 

-- DROP TABLE table2;

CREATE TABLE table2
(
  id integer NOT NULL,
  family character varying(40),
  species character varying(100),
  plotarea real,
  latitude double precision,
  longitude double precision,
  source integer,
  latlon geometry,
  CONSTRAINT table2_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE table2
  OWNER TO postgres;
GRANT ALL ON TABLE table2 TO postgres;
GRANT SELECT ON TABLE table2 TO banco;

-- Index: latlon_gist

-- DROP INDEX latlon_gist;

CREATE INDEX latlon_gist
  ON table2 
  USING gist
  (latlon);

-- Index: species_idx

-- DROP INDEX species_idx;

CREATE INDEX species_idx
  ON table2
  USING btree
  (species COLLATE pg_catalog."default");

1 个答案:

答案 0 :(得分:1)

您正在列species上的两个表之间执行连接。 不确定您的数据中有什么,但如果species是一个值远低于记录数量的列(例如,如果物种是“大象”,“长颈鹿”,并且您正在分析非洲的所有动物),加入将使每只大象与每头大象相匹配。

在大多数情况下加入两个表时,您尝试使用唯一或接近唯一的属性,例如id(不确定id在您的情况下是什么意思,但可能就是这样)。< / p>