麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數據庫 > PostgreSQL > 正文

Postgresql查詢效率計算初探

2020-01-31 15:20:43
字體:
來源:轉載
供稿:網友

摘要

關系數據庫很重要的一個方面是查詢速度。查詢速度的好壞,直接影響一個系統的好壞。

查詢速度一般需要通過查詢規劃來窺視執行的過程。

查詢路徑會選擇查詢代價最低的路徑執行。而這個代價是怎么算出來的呢。

主要關注的參數和表

參數:來自postgresql.conf文件,可以通過show 來查看

seq_page_cost = 1.0     # measured on an arbitrary scalerandom_page_cost = 4.0     # same scale as abovecpu_tuple_cost = 0.01     # same scale as abovecpu_index_tuple_cost = 0.005   # same scale as abovecpu_operator_cost = 0.0025    # same scale as aboveparallel_tuple_cost = 0.1    # same scale as aboveparallel_setup_cost = 1000.0   # same scale as above 

表(視圖): pg_class(主要關注relpages, reltuples), pg_stats

分析簡單的查詢的成本計算過程

建立模擬數據,插入100000條數據進入一個表

create table test(id int, info text);insert into test(id, info) select i, md5(i::text) from generate_series(1, 100000) t(i); 

沒有索引的情況

分析全表查詢的成本計算過程

postgres=# analyze test;  #防止沒有分析postgres=# explain select * from test;       QUERY PLAN       ------------------------------------------------------------- Seq Scan on test (cost=0.00..1834.00 rows=100000 width=37) 

1.查詢pg_class表,查看test表的page數量和行數

postgres=# select t.relpages, t.reltuples from pg_class t where t.relname = 'test'; relpages | reltuples ----------+-----------  834 | 100000 

成本為1834.00是怎么算出來的?

2.這個過程,實際上是順序掃描了834個page,節點發射了100000行

3.查看配置參數

seq_page_cost = 1.0 cpu_tuple_cost = 0.01 

4.得出的結果就是

postgres=# select 834 * 1.0 + 100000 * 0.01; ?column? ---------- 1834.00 

5.得出來的查詢成本就是 1834.00。和上面的查詢計劃算出來的一致。

全表加入條件的成本計算過程

postgres=# explain select * from test where id = 100;      QUERY PLAN      -------------------------------------------------------- Seq Scan on test (cost=0.00..2084.00 rows=1 width=37) Filter: (id = 100) 

成本 2084.00是怎么算出來的?

1.查詢pg_class表, pages,tuples和上面的例子一樣

2.這個過程就是順序test表,發射100000行,然后通過云存過濾了100000行

3.查看過濾運算一行的代價

cpu_operator_cost = 0.0025 

4.得出的結果是

postgres=# select 834 * 1.0 + 100000 * 0.01 + 100000 * 0.0025; ?column? ----------- 2084.0000

加入索引的情況

```create index on test(id);```

對比下面的四種情況

Index Only Scan

postgres=# explain select id from test where id = 100;                 QUERY PLAN                 ----------------------------------------------------------------------------- Index Only Scan using test_id_idx on test (cost=0.29..8.31 rows=1 width=4)  Index Cond: (id = 100) 

Index Scan

postgres=# explain select * from test where id = 100;                QUERY PLAN                ------------------------------------------------------------------------- Index Scan using test_id_idx on test (cost=0.29..8.31 rows=1 width=37)  Index Cond: (id = 100) 

Index Scan

postgres=# explain select * from test where id < 100;                 QUERY PLAN                 ---------------------------------------------------------------------------- Index Scan using test_id_idx on test (cost=0.29..10.11 rows=104 width=37)  Index Cond: (id < 100) 

把數據亂序插入

truncate table test;insert into test(id, info) select i, md5(i::text) from generate_series(1, 1000000) t(i) order by random();
postgres=# explain select * from test where id < 100;                 QUERY PLAN                 ---------------------------------------------------------------------------- Bitmap Heap Scan on test (cost=5.22..380.64 rows=102 width=37)  Recheck Cond: (id < 100)  -> Bitmap Index Scan on test_id_idx (cost=0.00..5.19 rows=102 width=0)     Index Cond: (id < 100)

結論

  • 有索引的時候,成本會大大減少。
  • 執行計劃跟數據的分布有很大的關系。
  • 有索引的分析相對復雜一點,可以先參考官方源碼實現。后面再補充上來

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 亚洲精品成人久久久 | 久久99国产精品视频 | 欧美另类综合 | 黄色一级片免费观看 | 老女人碰碰在线碰碰视频 | 草久影视| 日本看片一区二区三区高清 | 亚洲男人一区 | av色在线观看 | 久久国产精品免费视频 | 久久国产精 | 国产成人强伦免费视频网站 | 日本精品视频一区二区三区四区 | 精品在线免费播放 | 中文字幕在线成人 | 色播视频在线播放 | 欧美综合日韩 | 久久精品免费网站 | 午夜在线视频观看 | 中文字幕在线观看精品 | 91久久久久久久久久久久久 | 天天操很很操 | 欧美wwwwww| 国产羞羞视频在线观看免费应用 | 亚洲精品成人18久久久久 | 中国杭州少妇xxxx做受 | 欧美色视 | 毛片视频网址 | 国产免费看片 | 国产亚洲精品综合一区91 | 日本黄色免费片 | 久久精品视频一区 | 久久欧美亚洲另类专区91大神 | 中国字幕av | 国产亚洲黑人性受xxxx精品 | 污片视频在线观看 | 久色亚洲| 涩涩操| 久久久久免费精品国产小说色大师 | 爱逼爱操综合网 | 免费观看欧美一级片 |