pg_stat_statements का उपयोग
pg_stat_statements एक extension है जो record करता है कि हर तरह की query कितनी बार और कितनी देर चलती है।
In this page:
Syntax
CREATE EXTENSION pg_stat_statements;
SELECT query, calls, total_exec_time FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT n;
pg_stat_statements
इसे shared_preload_libraries में add करें, restart करें, और CREATE EXTENSION pg_stat_statements चलाएँ। फिर pg_stat_statements view calls, total_exec_time, mean_exec_time, और rows के साथ normalised queries दिखाता है।
Overall सबसे ज़्यादा cost करने वाली queries ढूंढने के लिए total time से sort करें, और pg_stat_statements_reset() से reset करें।
Note:
Overall सबसे expensive query अक्सर एक fast query होती है जो लाखों बार called होती है।
उदाहरण: pg_stat_statements
shop=# CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
shop=# SELECT calls, round(total_exec_time::numeric, 1) AS total_ms, round(mean_exec_time::numeric, 2) AS mean_ms, left(query, 50) AS query
shop-# FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 3;
calls | total_ms | mean_ms | query
---------+----------+---------+----------------------------------------------------
1204331 | 98213.4 | 0.08 | SELECT * FROM sessions WHERE token = $1
15200 | 44310.9 | 2.91 | SELECT o.id, c.name FROM orders o JOIN customers c
310 | 22001.2 | 70.97 | SELECT date_trunc($1, created_at), count(*) FROM ev
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- library preload करना भूल जाना
- सिर्फ सबसे slow single query देखना
- changes के बाद statistics reset न करना
चैप्टर सारांश
- Query statistics track करने वाला Extension
- shared_preload_libraries चाहिए
- total_exec_time से sort करें
- tuning के बाद reset करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: