## 版本問題 11 之前default value會做insert 11 之後不會insert 只有用到 查詢到才會做 (從 PG 11 之後就不需要 rewrite 整個 table,而是先將 default value 存在 system catalog 上) ## pk ``` 背景: 有RDBMS課程學生問他跑benchmarking on postgresql,他發現用auto-inc比uuidV4更快,他想知道原因。 (謎之聲:原來這傢伙真的有售後服務的) ------------------------------------ 首先: 我跟他談過了,他的測試的方向是沒什麼大問題的,而且是開128 threads來跑insert的。 所以,uuidv4真的在benchmarking時會比auto-increment慢。 我直接先跳到結論: benchmarking和現實世界不完全相同的。 benchmarking和現實世界不完全相同的。 benchmarking和現實世界不完全相同的。 例子: graphic card A在benchmarking 時打LOL有100 average FPS,然後graphic card B就只有80 average FPS。 表面上應該買card A吧。 但是呢,card A在大部份時間都有100FPS,但半小時遊戲內有可能有觸發過熱保護。 讓你跟艾希啊,易大師啊你追我逐時沒了畫面半秒……然後你就趴在地上GG了…… 這樣子的頂級效能graphic card你買不買??? (謎之聲:這傢伙整天說電腦太爛才會打輸LOL的……) ------------------------------------ 先說auto-inc為何benchmarking會比較快: 1) auto-inc insert在PK的B+ tree index中,只會動到最右邊的那個leaf node。 然後uuidV4 insert,是會隨機的改動到PK B+tree的任一leaf node。 所以,如果有大量insert時,auto-inc只有最右邊新追加的數個PK leaf node需要進REDO log(和後續寫進storage的IO) 然後uuidV4是有大量被改動過的PK leaf node需要寫進REDO log的。 別忘記一點:即使你的8KB leaf node只改動其中一byte也好,也是需要整個8KB node都抄到REDO log的。 (不然突然斷電了有可能在storage寫到一半的data會救不回來) 所以,uuid在PK index上,是用得遠比auto-inc更多的IO的。 2) uuidv4是16 byte,而postgresql bigint是8 byte,這樣子pk index size會差了一倍的。 在benchmarking會有差別的。(雖然現實世界今天硬體這是可以無視的) 1+2的後果:就是auto-inc在benchmarking會跑得比較快是合理的。 ------------------------------------ 那麼,為何明明auto-inc跑起來比較快吃比較少的IO,現實上pg人大都喜歡用natural key / UUID而不喜歡auto-inc??? 現實上…… 1) 越近期被加入database的資料,就越有可能被拿出來改動。 用auto-inc下,所有近期的資料全在PK-index的右邊,所以右邊忙死然後左邊空閒。 這讓data contention更有可能發生,限制了「同一時間下,能有多少個平行工作一起來跑」的能力。 2) 很多transaction不會只改動single record。 作為backend人我們最最最害怕的,是locking的「滾雪球」。 當Thread A拿不到需要的東西而被卡住時(blocking),很可能Thread A他現在已經拿了的東西其他人也需要用上,觸發Thread B的blocking。 然後就到Thread C, Thread D...... 到最後就是:整個database明明有大量的CPU thread可以用,但就看起來是single thread在跑一樣。 ------------------------------------ postgresql的長期設計哲學: 寧可single thread表現慢一點,寧可resource用得多一點。 (對的,auto-inc用的IO和storage size都比uuidv4少) 但是,postgresql是希望「同一時間能做盡量多的事務」(a.k.a. 迴避data contention) 所以就跟開始時的graphic card一樣: 你是寧可80FPS穩穩跑? 還是寧可100FPS爽爽跑,但有時會黑畫面??? 別忘記一點: 用多了io嘛,就拿dxxxxxg數據跟老闆申請加大機器硬體就好。 data contention嘛,你是除了被老闆罵,其實你是沒什麼東西能真正做的。 錢是公司的,命是自己的~~~ ------------------------------------ 就最後一句: 為何20年前大家都不喜歡pg而喜歡mysql? 而今天越來越多人轉到pg??? (真正答案:因為萬惡的Oracle) 另一個答案是: 20年前硬體超級超級貴。 如果有二片graphic card,一片30 FPS但有可能畫面會黑半秒,一片15 FPS但畫面不會黑。 很多人都會賭「不是剛剛好打會戰時黑畫面」而要前者的。 今天硬體便宜。 100FPS和80FPS開始沒感覺,畫面會不會突然黑掉就當然是問題囉。 ``` ## 優化教學 instagram https://instagram-engineering.com/handling-growth-with-postgres-5-tips-from-instagram-d5d7e7ffdfcb ## explain   https://www.youtube.com/watch?v=PS-ozd3Jdxo ## message defuplication 可以用lsn的message id當作key https://medium.com/dcardlab/postgresql-%E6%8A%80%E8%A1%93%E7%AD%86%E8%A8%98-%E8%B7%9F%E7%96%BE%E7%AE%A1%E7%BD%B2%E6%B2%92%E6%9C%89%E9%97%9C%E4%BF%82%E7%9A%84cdc-218e27eb363d ## PostgreSQL 優化 看io等 https://www.youtube.com/watch?v=PS-ozd3Jdxo ## 聚合 欄位依賴性 https://blog.csdn.net/danpu0978/article/details/106775634 ## db詳解 必看 必看 https://ithelp.ithome.com.tw/articles/10320923 ## schema change 問題 https://blog.kennycoder.io/2023/12/31/Postgres-%E5%B8%B8%E8%A6%8B%E7%9A%84-schema-change-%E5%95%8F%E9%A1%8C/ ## Copy Operation https://blog.kennycoder.io/2021/09/19/Postgres-Copy-Operation-%E7%B4%B0%E7%AF%80%E6%8E%A2%E8%A8%8E/#more ## 少為人知 PostgreSQL 的功能 https://blog.kennycoder.io/2023/12/30/%E5%B0%91%E7%82%BA%E4%BA%BA%E7%9F%A5-PostgreSQL-%E7%9A%84%E5%8A%9F%E8%83%BD/ ## postgres vs mysql https://blog.kennycoder.io/2023/11/18/%E8%AB%87%E8%AB%87-Postgres-%E8%88%87-MySQL-%E7%9A%84%E5%B7%AE%E7%95%B0/   ## upgrade https://blog.kennycoder.io/2022/11/27/%E9%80%8F%E9%81%8E-pg-upgrade-%E5%BF%AB%E9%80%9F%E5%8D%87%E7%B4%9A-Postgres/ ## 分頁 https://blog.kennycoder.io/2022/08/07/Postgres-%E5%90%84%E7%A8%AE%E5%88%86%E9%A0%81%E7%9A%84%E6%96%B9%E5%BC%8F%E4%BB%8B%E7%B4%B9/ ## copy 大量insert , output優化 https://blog.kennycoder.io/2021/09/19/Postgres-Copy-Operation-%E7%B4%B0%E7%AF%80%E6%8E%A2%E8%A8%8E/#more ## index  說明用跟mysql差異 https://www.bilibili.com/video/BV1hr4y1d7hj/?spm_id_from=333.788&vd_source=f726dd30598fe01d9fbc9c5a988d6408  詳細 https://blog.kennycoder.io/2021/08/08/Postgres-%E6%B7%B1%E5%85%A5%E6%8E%A2%E8%A8%8Eindex-engine-%E7%9A%84%E8%A1%8C%E7%82%BA/#more 看了才知道會有不同index還會用到memory  特別的 計算   ## 最左  ## 建立index要小心  ## 觸發有問題 ## function  ## WAL https://ithelp.ithome.com.tw/articles/10325322 ## physical replication https://blog.kennycoder.io/2023/05/01/Postgres-%E4%B8%80%E6%96%87%E7%9C%8B%E6%87%82-physical-replication-%E5%8F%8A-logical-replication-%E6%A6%82%E5%BF%B5/ https://blog.kennycoder.io/2022/12/11/Postgres-%E8%A8%AD%E5%AE%9A-physical-replication-%E6%95%99%E5%AD%B8/ https://blog.kennycoder.io/2022/11/27/%E9%80%8F%E9%81%8E-pg-upgrade-%E5%BF%AB%E9%80%9F%E5%8D%87%E7%B4%9A-Postgres/ https://blog.kennycoder.io/2023/02/12/%E4%BB%8B%E7%B4%B9-postgres-logical-decoding-%E7%9A%84%E6%A6%82%E5%BF%B5%E8%88%87%E7%A4%BA%E7%AF%84/ https://www.modb.pro/db/52463  太多了 建議他blog想相關的看一遍 ## migrate  https://blog.kennycoder.io/2021/07/24/Postgres-zero-downtime-migration-%E8%A9%B2%E6%B3%A8%E6%84%8F%E7%9A%84%E7%B4%B0%E7%AF%80/ ## index在migrate    我猜測是因為mvcc原因 ## PG Cluster 進行主版本的升級 https://blog.kennycoder.io/2023/08/19/How-We-Achieved-Minimal-Downtime-During-Our-PostgreSQL-Database-Upgrade-%E4%B8%AD%E6%96%87%E7%89%88/  ## Copy Operation https://blog.kennycoder.io/2021/09/19/Postgres-Copy-Operation-%E7%B4%B0%E7%AF%80%E6%8E%A2%E8%A8%8E/#more ## pgpool-ll 主從 proxy https://blog.kennycoder.io/2023/01/08/pgpool-ll-stream-replication-%E6%9E%B6%E8%A8%AD%E6%95%99%E5%AD%B8/ ## 架構 https://www.bilibili.com/video/BV1C84y1f7n1/?spm_id_from=333.788&vd_source=f726dd30598fe01d9fbc9c5a988d6408 ## full text 一定要docker , mac有問題 https://stackoverflow.com/questions/38249935/pg-trgm-behaves-differently-on-ubuntu-and-mac-os-x 建立function https://juejin.cn/post/6844903718572261389 目前踩過的坑 第一 pg_tram支援中文 9版後 另外1. 2字不支援 所以要跑自定義function 建立好function再利用gin 去建立index 另外在選取index的時候要where這邊的 不然時間會很長 另外function名稱不支援大小寫 所以要用_ 在給全縣的時候才不會有問題 ## 注意事項  ## 時間加上時區轉換 https://stackoverflow.com/questions/11126037/postgresql-date-with-timezone ## -> ->> https://stackoverflow.com/questions/38777535/what-is-the-difference-between-and-in-postgres-sql  ## 時間  ## 自動欄位   ## filter https://medium.com/little-programming-joys/the-filter-clause-in-postgres-9-4-3dd327d3c852 ## 分庫 https://medium.com/nerd-for-tech/implementing-table-partitioning-partitioned-views-in-sql-server-dc1528d1dd6e 如果是sql server要自己聯合 https://docs.postgresql.tw/the-sql-language/ddl/table-partitioning 但如果是postgre都會幫你自己聯合 ## 大量刪除 https://byron0920.pixnet.net/blog/post/85759990 ## 優勢  ## auto-increment https://stackoverflow.com/questions/48446399/sql-auto-increment-pgadmin-4 注意5之後用法不一樣 ## PostgreSQL 如何估算 HashAggregate 的 Return Rows ,以及低估的後果 https://medium.com/pgsql-tw/postgresql-%E5%A6%82%E4%BD%95%E4%BC%B0%E7%AE%97-hashaggregate-%E7%9A%84-return-rows-%E4%BB%A5%E5%8F%8A%E4%BD%8E%E4%BC%B0%E7%9A%84%E5%BE%8C%E6%9E%9C-cc06719641da ## RETURNING https://docs.postgresql.tw/the-sql-language/data-manipulation/returning-data-from-modified-rows ## varchar character varying and varchar is correct. https://stackoverflow.com/questions/56977456/varchar-is-missing-on-pgadmin4-ui ## cli 登入 psql -U (userName) (databse name) \dt 列出全部table ## function coalesce() 會判斷第一個是不是null 是的話就用第二個參數 預設欄位 ## between 特別用法  ## Materialized View https://harryuan-65.medium.com/%E6%8F%90%E5%8D%87%E6%9C%8D%E5%8B%99%E6%95%88%E8%83%BD-%E6%B8%9B%E8%BC%95db%E8%B2%A0%E6%93%94-2-materialized-view-d6addb870c51  ## func https://www.sjkjc.com/postgresql-ref/array_append/ ## 運算符號 && 運算符用於檢查兩個數組是否有重疊的元素  ## ip  ## enum https://ithelp.ithome.com.tw/articles/10224450 自己認為一般就const存 如果像是coloer這種可能會有順序的在用 ## index https://blog.kennycoder.io/2021/08/08/Postgres-%E6%B7%B1%E5%85%A5%E6%8E%A2%E8%A8%8Eindex-engine-%E7%9A%84%E8%A1%8C%E7%82%BA/#more ## race Condition https://blog.kennycoder.io/2020/03/06/RDBMS-%E4%B8%8D%E5%90%8C%E7%9A%84Isolation-Level-Race-Condition%E7%A4%BA%E7%AF%84/ ###### tags: `MySql`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up