我有一张桌子,上面有720万个元组,看起来像这样:
table public.methods
column | type | attributes
--------+-----------------------+----------------------------------------------------
id | integer | not null DEFAULT nextval('methodkey'::regclass)
hash | character varying(32) | not null
string | character varying | not null
method | character varying | not null
file | character varying | not null
type | character varying | not null
Indexes:
"methods_pkey" PRIMARY KEY, btree (id)
"methodhash" btree (hash)
现在,我想选择一些值,但是查询速度非常慢:
db=# explain
select hash, string, count(method)
from methods
where hash not in
(select hash from nostring)
group by hash, string
order by count(method) desc;
QUERY PLAN
----------------------------------------------------------------------------------------
Sort (cost=160245190041.10..160245190962.07 rows=368391 width=182)
Sort Key: (count(methods.method))
-> GroupAggregate (cost=160245017241.77..160245057764.73 rows=368391 width=182)
-> Sort (cost=160245017241.77..160245026451.53 rows=3683905 width=182)
Sort Key: methods.hash, methods.string
-> Seq Scan on methods (cost=0.00..160243305942.27 rows=3683905 width=182)
Filter: (NOT (SubPlan 1))
SubPlan 1
-> Materialize (cost=0.00..41071.54 rows=970636 width=33)
-> Seq Scan on nostring (cost=0.00..28634.36 rows=970636 width=33)
该hash
列是的md5哈希,string
并具有索引。因此,我认为我的问题是整个表是按ID而不是按哈希排序的,因此需要花一些时间先对其进行排序然后再对其进行分组?
该表nostring
仅包含我不想拥有的哈希列表。但是我需要两个表都具有所有值。因此,不能删除它们。
其他信息:所有列均不能为null(已在表定义中修复),并且我正在使用PostgreSQL 9.2。
NULL
列中值的百分比是method
多少?上有重复品string
吗?