Postgres SQL 쿼리를 느린 큰 표 AWS(RDS)

0

질문

현재는 테이블이 최소한의 행 30million,그리고 그것은 성장할 때마다 수행하려고 선택 쿼리,그것은 극단적으로 긴 시간입니다. 무엇을 최적화가 필요한 쿼리하기 전에 나는 증가는 데이터베이스의 성능?

POSTGRES 12 on AWS RDS db.t3.small, with 20GB storage

**Message Table**

id (bigint) -> pk
meta (jsonb)
snapshot_ts (integer) -> epoch timestamp
value (character varying 100)
type (character varying 50)
created (timestamp with timezone)
last_modified (timestamp with timezone)
attribute_id (bigint) -> Foreign Key
company_id (bigint) -> Foreign Key
project_id (bigint) -> Foreign Key
device_id (bigint) -> Foreign Key


EXPLAIN (analyze,buffers) SELECT COUNT(*) FROM public.message
WHERE company_id=446 AND project_id=52 AND snapshot_ts>=1637568000.0 AND snapshot_ts<=1637654399.0 AND attribute_id=458

->Aggregate  (cost=399804.26..399804.27 rows=1 width=8) (actual time=65150.696..65150.697 rows=1 loops=1)
  Buffers: shared hit=170 read=115437 dirtied=167
  I/O Timings: read=64396.424
  ->  Index Scan using message_attribute_id_6578b282 on message  (cost=0.56..399803.23 rows=411 width=0) (actual time=57752.297..65147.391 rows=8656 loops=1)
        Index Cond: (attribute_id = 458)
        Filter: ((company_id = 446) AND (project_id = 52) AND ((snapshot_ts)::numeric >= 1637568000.0) AND ((snapshot_ts)::numeric <= 1637654399.0))
        Rows Removed by Filter: 106703
        Buffers: shared hit=170 read=115437 dirtied=167
        I/O Timings: read=64396.424
Planning Time: 0.779 ms
Execution Time: 65150.730 ms

**Indexes**
indexname                       | indexdef
message_attribute_id_6578b282   | CREATE INDEX message_attribute_id_6578b282 ON public.message USING btree (attribute_id)
message_company_id_cef5ed5f     | CREATE INDEX message_company_id_cef5ed5f ON public.message USING btree (company_id)
message_device_id_b4da2571      | CREATE INDEX message_device_id_b4da2571 ON public.message USING btree (device_id)
message_pkey                    | CREATE UNIQUE INDEX message_pkey ON public.message USING btree (id)
message_project_id_7ba6787d     | CREATE INDEX message_project_id_7ba6787d ON public.message USING btree (project_id)
amazon-rds postgresql postgresql-12 sql
2021-11-24 01:48:59
1

최고의 응답

2

을 고려하면 특정한 검색어:

SELECT COUNT(*)
FROM public.message
WHERE company_id=446 
  AND project_id=52 
  AND snapshot_ts>=1637568000.0 AND snapshot_ts<=1637654399.0 
  AND attribute_id=458

다음과 같은 인덱스는 잠재력이 크게 증가하는 성능:

create index ix1 on public.message (
  company_id, project_id, attribute_id, snapshot_ts
);

그러나 유지 하는 마음에 인덱스를 만드는 30 백만 행 테이블는 약간의 시간이 걸릴 수 있습니다.

2021-11-24 03:41:16

는 것을 의미하자는 말이 여러 개 있는 경우 쿼리는 조건들을 만들 필요가 각 인덱스를 위해 각각의 경우? "(company_id,project_id,attribute_id,snapshot_ts)","(project_id,attribute_id,snapshot_ts)","(attribute_id,snapshot_ts)"
Sola

@솔라이 필요한 경우 정확한 최적의 인덱스에 대한 모든 쿼리,다음 예는 많은 수의 인덱스입니다. 하지만 당신은 아마도 멀리 얻을 수 있습니다 약간 적은 최적의 일부에 적용됩니다. 몇 참조하십시오. 질문이 있을 경우 반드시 포함해야 합니명(을 분석,버퍼)
jjanes

후 create index 내 컴퓨터에서 작업을 시작,하지만 언젠가 후,이하지 않았거 색인을 할 때 쿼리가 있습니다. 이런 일이 생산에 대한뿐만 아니라 서버.
Sola

@Sola 경우 쿼리에는 사용하지 않는,다음 최적화 프로그램은 그것을 고려하는 다른 실행 계획을 실행합니다. 먼저 확인하는 통계의 테이블을 사용하여 날짜 ANALYZE public.message. 그런 다음,문제가 지속되는 경우,주시기 바랍 검색 실행 계획 및 추가 질문입니다.
The Impaler

감사에 대한 답변했습니다. 어제하려고 할 때 인덱스(company_id,project_id,attribute_id,snapshot_ts),에서 조건을 사용하여(snapshot_ts 및 attribute_id),그것은 작업에서는 첫째,그는 하지 않습니다. 지금 나는 다른 추가 인덱스(attribute_id,snapshot_ts),이러한 목적으로,보일 다시 작업을 계속합니다.
Sola

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................