Postgres ร้องขอแบบ sql ช้าใหญ่กับโต๊ะ(AWS RDS)

0

คำถาม

ปัจจุบันที่โต๊ะต้อนอย่างน้อยแถวของ 30million และมันกำลังเติบโตเมื่อไหร่ก็ตามที่พยายามจะทำอย่างเลือกกับการค้นหา,มันต้องใช้สมานานแล้ว สิ่งที่ต้อง the result will be artificially smoothed to hide jpeg artefacts งกับการค้นหาก่อนที่ฉันจะเพิ่มการแสดงของฐานข้อมูลหรือเปล่า

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

ความดัชนีมีศักยภาพของ greatly เพิ่มขึ้นการแสดง:

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

@sola ถ้าคุณต้องการส่วนผสมที่แน่นอนที่เหมาะสมดัชนีสำหรับทุกค้นแล้วใช่มันเป็นเรื่องของ indexes. แต่คุณอาจจะสามารถออกไปกับลือน้อยที่เหมาะสมสำหรับบางส่วนของมัน พยายามไม่กี่และมองเห็น ถ้าคุณมีคำถามมั่นใจว่าจะรวมอธิบาย(วิเคราะห์,บัฟเฟอ)
jjanes

หลังจากการสร้างดัชนีที่ของฉันท้องถิ่นเครื่องจักรแล้วมันทำงานอยู่ที่จุดเริ่มต้นแต่หลังจากช่วงมันไม่ได้กระตุ้นค indexes เมื่อมาทำอะไรกับการค้นหา. มันเกิดขึ้นในการผลิตกองเซิร์ฟเวอร์เช่นกัน
Sola

@Sola ถ้าการสืบค้นไม่ใช่การใช้ดัชนี,จากนั้นก็ optimizer มันดูเป็นต่างกันการประมวลผลแผน คนแรก,ทำให้แน่ใจว่าสถิติของโต๊ะองเธอเรื่องราวมันเป็นยังเดทกันโดยใช้ ANALYZE public.message. งั้น,ถ้ามีปัญหายัง persists ได้โปรดเก็บการประหารแผนและเพิ่มมันต้องคำถาม
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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่