이 아마 더 빨리,하지만 너무 신뢰할 수 없습니다 솔루션 것 :
지금
t=# create table t (i int);
CREATE TABLE
t=# insert into t select generate_series(1,9,1);
INSERT 0 9
t=# insert into t select generate_series(1,999999,1);
INSERT 0 999999
t=# insert into t select generate_series(1,9999999,1);
INSERT 0 9999999
조회 :
t=# select i,count(*) from t group by i having count(*) > 1 order by 2 desc,1 limit 1;
i | count
---+-------
1 | 3
(1 row)
Time: 7538.476 ms
지금 통계에서 확인 : 마지막으로
t=# analyze t;
ANALYZE
Time: 1079.465 ms
t=# with fr as (select most_common_vals::text::text[] from pg_stats where tablename = 't' and attname='i')
select count(1),i from t join fr on true where i::text = any(most_common_vals) group by i;
count | i
-------+--------
2 | 94933
2 | 196651
2 | 242894
2 | 313829
2 | 501027
2 | 757714
2 | 778442
2 | 896602
2 | 929918
2 | 979650
2 | 999259
(11 rows)
Time: 3584.582 ms
및 단지 확인 uniq가 가장 빈번한 값 중 하나만 존재한다면 :
통계가 테이블에 수집 한 후
t=# select count(1),i from t where i::text = (select (most_common_vals::text::text[])[1] from pg_stats where tablename = 't' and attname='i') group by i;
count | i
-------+------
2 | 1540
(1 row)
Time: 1871.907 ms
갱신
pg_stats
데이터 modifyed된다. 따라서 데이터 배포에 대한 통계를 새로 수집하지 않아도됩니다. 예를 들어 내 샘플에서 :
물론
t=# delete from t where i = 1540;
DELETE 2
Time: 941.684 ms
t=# select count(1),i from t where i::text = (select (most_common_vals::text::text[])[1] from pg_stats where tablename = 't' and attname='i') group by i;
count | i
-------+---
(0 rows)
Time: 1876.136 ms
t=# analyze t;
ANALYZE
Time: 77.108 ms
t=# select count(1),i from t where i::text = (select (most_common_vals::text::text[])[1] from pg_stats where tablename = 't' and attname='i') group by i;
count | i
-------+-------
2 | 41377
(1 row)
Time: 1878.260 ms
당신이 한 가장 빈번한 값 다음, 실패 확률이 감소 더에 의존하는 경우, 그러나 다시 - 같은 방법은 통계 "신선도"에 따라 달라집니다.
작동하지 않는 코드가 있습니까? 질문은 무엇입니까? –
쿼리가 1,000 만 행 데이터 집합에 2 분이 걸리면 훨씬 빠른 속도가 필요합니다. –
은 DB 측에서 처리됩니다. psycopg2가 아닙니다. –