在工做中,若是遇到某個SQL執行時間比較長,不少時候會考慮到SQL的執行計劃怎樣?經過分析SQL的執行計劃去分析SQL瓶頸在哪裏。sql
那麼運用默認的Postgresql方式是能夠去分析執行計劃的。以下例:app
explain analyze select * from public.sort_test where id = 10001;
其結果是:ide
而我這裏會給你們分享一個免費的,很好的可視化工具去分析,特別是針對比較複雜的SQL執行計劃,會更適用。步驟以下:工具
獲得SQL 執行計劃的Json 輸出格式文本,能夠經過以下解決:oop
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"按鈕。spa
5.以後,上例中的可視化執行計劃以下圖:code
你們也能夠掃描並關注以下公衆號「TimTest」,會有更多性能測試相關內容分享。