分享Postgres SQL execution plan visualizer
阿新 • • 發佈:2020-09-27
在工作中,如果遇到某個SQL執行時間比較長,很多時候會考慮到SQL的執行計劃怎樣?通過分析SQL的執行計劃去分析SQL瓶頸在哪裡。
那麼運用預設的Postgresql方式是可以去分析執行計劃的。如下例:
explain analyze select * from public.sort_test where id = 10001;
其結果是:
而我這裡會給大家分享一個免費的,很好的視覺化工具去分析,特別是針對比較複雜的SQL執行計劃,會更適用。步驟如下:
-
得到SQL 執行計劃的Json 輸出格式文字,可以通過如下解決:
EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON)select * from public.sort_test where id = 10001;
得到的Json格式的執行計劃如下:
[ { "Plan": { "Node Type": "Index Scan", "Parallel Aware": false, "Scan Direction": "Forward", "Index Name": "sort_test_pkey", "Relation Name": "sort_test", "Schema": "public", "Alias": "sort_test", "Startup Cost": 0.43, "Total Cost": 8.45, "Plan Rows": 1, "Plan Width": 14, "Actual Startup Time": 0.015, "Actual Total Time": 0.016, "Actual Rows": 1, "Actual Loops": 1, "Output": [ "id", "salary" ], "Index Cond": "(sort_test.id = 10001)", "Rows Removed by Index Recheck": 0, "Shared Hit Blocks": 4, "Shared Read Blocks": 0, "Shared Dirtied Blocks": 0, "Shared Written Blocks": 0, "Local Hit Blocks": 0, "Local Read Blocks": 0, "Local Dirtied Blocks": 0, "Local Written Blocks": 0, "Temp Read Blocks": 0, "Temp Written Blocks": 0 }, "Planning Time": 0.127, "Triggers": [], "Execution Time": 0.037 }]
2.開啟視覺化工具網站:http://www.tatiyants.com/pev/
3.在頁面上點選按鈕“NEW PLAN"
4.在介面上按順序填入如下圖所示的元素,然後點選“SUBMIT"按鈕。
6. 之後,上例中的視覺化執行計劃如下圖: