PostgreSQL: เวลาเข้าถึงตารางล่าสุด


10

ฉันรับผิดชอบฐานข้อมูลขนาดใหญ่ของ PostgreSQL โดยมีตารางไม่กี่โหล ฉันสงสัยว่าตารางเหล่านี้จำนวนมากจะไม่สามารถเข้าถึงได้

วิธีที่ดีที่สุดในการตรวจสอบเมื่อครั้งล่าสุดคือการเข้าถึงตารางบางอย่าง ผมคิดว่าการเพิ่มทริกเกอร์บนDELETE, INSERTและUPDATEแต่ผมหวังว่าจะมีวิธีที่มีประสิทธิภาพมากขึ้น


คุณไม่สามารถสร้างทริกเกอร์บน selectคุณได้พิจารณาการบันทึก ?
แจ็คบอกว่าลอง topanswers.xyz

ขอบคุณแก้ไขแล้ว การบันทึกอาจเป็นวิธีแก้ปัญหา แต่มีการใช้งานฐานข้อมูลอย่างหนักและบันทึกอาจใช้พื้นที่ดิสก์จำนวนมาก
Adam Matan

คำตอบ:


9

pg_catalog.pg_statio_all_tablesเป็นเพื่อนของคุณ สิ่งที่คุณต้องทำคือสำรวจความคิดเห็น pg_statio_all_tables เป็นระยะสำหรับตารางที่เป็นปัญหา การเปลี่ยนสถิติ ~ ตารางที่ใช้งาน, สถิติที่ไม่เปลี่ยนแปลง ~ ตารางที่อาจไม่ได้ใช้ เพียงระวังว่าไม่มีใครทำselect pg_stat_reset () ;ในระหว่างการตรวจสอบของคุณ

ตัวอย่างเช่น:

test_1=# create table test_stats (col1 integer);
CREATE TABLE

test_1=# select * from pg_catalog.pg_statio_all_tables 
         where schemaname not in ('pg_catalog', 'information_schema') 
         and relname = 'test_stats';
 relid | schemaname |  relname   | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit | toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit 
-------+------------+------------+----------------+---------------+---------------+--------------+-----------------+----------------+----------------+---------------
 22957 | public     | test_stats |              0 |             0 |        [null] |       [null] |          [null] |         [null] |         [null] |        [null]
(1 row)

แทรก:

test_1=# insert into test_stats (col1) select generate_series( 1, 10000000);
INSERT 0 10000000

test_1=# select * from pg_catalog.pg_statio_all_tables
         where schemaname not in ('pg_catalog', 'information_schema') 
         and relname = 'test_stats';
 relid | schemaname |  relname   | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit | toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit 
-------+------------+------------+----------------+---------------+---------------+--------------+-----------------+----------------+----------------+---------------
 22957 | public     | test_stats |          44260 |      10088481 |        [null] |       [null] |          [null] |         [null] |         [null] |        [null]
(1 row)

เลือก:

test_1=# select count (*) from test_stats where col1 between 10000 and 50000;
 count 
-------
 40001
(1 row)

test_1=# select * from pg_catalog.pg_statio_all_tables
         where schemaname not in ('pg_catalog', 'information_schema') 
         and relname = 'test_stats';
 relid | schemaname |  relname   | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit | toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit 
-------+------------+------------+----------------+---------------+---------------+--------------+-----------------+----------------+----------------+---------------
 22957 | public     | test_stats |          85560 |      10091429 |        [null] |       [null] |          [null] |         [null] |         [null] |        [null]
(1 row)

ลบ:

test_1=# delete from test_stats where col1 between 10000 and 50000;
DELETE 40001

test_1=# select * from pg_catalog.pg_statio_all_tables
         where schemaname not in ('pg_catalog', 'information_schema') 
         and relname = 'test_stats';
 relid | schemaname |  relname   | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit | toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit 
-------+------------+------------+----------------+---------------+---------------+--------------+-----------------+----------------+----------------+---------------
 22957 | public     | test_stats |         155075 |      10136163 |        [null] |       [null] |          [null] |         [null] |         [null] |        [null]
(1 row)

update-- 2011-09-01

การทดสอบเพิ่มเติมบ่งชี้ว่าvacuumดูเหมือนว่าจะเพิ่มค่าใน pg_statio_all_tables บ้างซึ่งเป็นโชคร้ายสำหรับการใช้งานที่คุณต้องการ แม้ว่าvacuumจะไม่ได้ใช้ประโยชน์จาก pg_statio_all_tables แต่ก็ไร้ประโยชน์ แต่ก็ทำให้การตีความผลลัพธ์เป็นบิตที่คลุมเครือ

บางทีสถานที่ที่ดีกว่าในการตรวจสอบคือ pg_catalog.pg_stat_all_tables (อย่างน้อยกับ Pg รุ่นใหม่กว่า) ฉันกำลังดูรุ่น 8.4 และที่มีการนับสำหรับสิ่งอันดับแทรกอ่านอัปเดตและลบ - ISTR 8.2 ไม่มีทุกอย่างและฉันไม่รู้เกี่ยวกับ 8.3 ดังนั้น YMMV ขึ้นอยู่กับรุ่นของ Pg ที่คุณเป็น การใช้

ตัวเลือกที่สาม (สำหรับการแทรกอัปเดตและลบกิจกรรม) คือการดูการประทับเวลาของไฟล์ในไดเรกทอรี $ PGDATA / base / $ datid ชื่อไฟล์ควรแมปไปที่หมายเลขของตารางดังนั้นคุณสามารถใช้เพื่อระบุตารางที่ไม่ได้รับการแทรกการปรับปรุงหรือการลบ น่าเสียดายที่นี่ไม่ใช่ที่อยู่ตารางที่ยังคงได้รับเลือกและการใช้พื้นที่ตารางจะทำให้เกิดปัญหาเพิ่มเติม (เนื่องจากไฟล์เหล่านั้นจะไม่อยู่ภายใต้ $ PGDATA / base / $ datid) การประทับเวลาจะไม่อัปเดตจนกว่าการเปลี่ยนแปลงใด ๆ ที่รอดำเนินการจะถูกล้างออก แต่หากไฟล์ไม่ได้เปลี่ยนแปลงในเดือนนั้นอัตราต่อรองของการเปลี่ยนแปลงที่รอดำเนินการในปัจจุบันอาจจะเล็ก


3

คุณสามารถรับข้อมูลบางอย่างเกี่ยวกับการเปลี่ยนแปลงล่าสุดของตารางด้วยxminเช่น:

select max(xmin::text::bigint) from t;

แต่คุณจะต้องตระหนักถึงของโมดูโลและวิจิตรและxids แช่แข็ง ไม่มีวิธีใดในการแปลงเป็น "เวลา" แต่ถ้าคุณได้รับค่าสำหรับตารางของคุณตอนนี้ให้เปรียบเทียบในภายหลังคุณสามารถรับรายการตารางที่มีการเปลี่ยนแปลง

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.