幫幫忙頂一下這個文章,非常好的一個文章,非常值得了解。
來自:http://blog.chinaunix.net/uid-24774106-id-3780341.html
PostgreSQL部署上之后,經過一段時間的運行,我們比較關心那些SQL運行時間比較長,或者說那些SQL執行的特別慢,拖累的性能,只有找到這些SQL,才能有針對性地對這些SQL進行優化,提升PostgreSQL的性能。 PostgreSQL提供了pg_stat_statements來存儲SQL的運行次數,總運行時間,shared_buffer命中次數,shared_buffer read次數等統計信息。
| Name | Type | References | Description |
|---|---|---|---|
| userid | oid | pg_authid.oid | OID of user who executed the statement |
| dbid | oid | pg_database.oid | OID of database in which the statement was executed |
| query | text | Text of the statement (up to track_activity_query_size bytes) | |
| calls | bigint | Number of times executed | |
| total_time | double PRecision | Total time spent in the statement, in seconds | |
| rows | bigint | Total number of rows retrieved or affected by the statement | |
| shared_blks_hit | bigint | Total number of shared blocks hits by the statement | |
| shared_blks_read | bigint | Total number of shared blocks reads by the statement | |
| shared_blks_written | bigint | Total number of shared blocks writes by the statement | |
| local_blks_hit | bigint | Total number of local blocks hits by the statement | |
| local_blks_read | bigint | Total number of local blocks reads by the statement | |
| local_blks_written | bigint | Total number of local blocks writes by the statement | |
| temp_blks_read | bigint | Total number of temp blocks reads by the statement | |
| temp_blks_written | bigint | Total number of temp blocks writes by the statemen |
在我另一臺機器上,用pgadmin查看:
統計結果一直都在,重啟也不會清零,那么統計結果如何清零重新統計呢?執行下面SQL即可:select pg_stat_statements_reset() ; 找到最耗時的SQL,我們就能針對這些耗時的SQL,查看是否有優化的余地。參考文獻:1 More on Postgres Performance 2 PostgreSQL manual新聞熱點
疑難解答