How to sort by an given array in many different ways (in Rails with ActiveRecord with Postgres)
## 1. Use sort_by and prioritized array https://stackoverflow.com/questions/1680627/activerecord-findarray-of-ids-preserving-order/268...
System Design 101 - SQL and NoSQL
### SQL and NoSQL **SQL** => Relational databases store data in rows and columns. 就是有 tables, row, columns 的 database **NoSQL** => 泛指不是 ...
System Design 101 - Sharding or Data Partitioning
> Data partitioning (also known as sharding) is a technique to break up a big database (DB) into many smaller parts. Sharding 就是把 DB 切成...
Database ACID - Atomicity, Consistency, Isolation, and Durability
> ACID,是指資料庫管理系統(DBMS)在寫入或更新資料的過程中,為保證事務(transaction)是正確可靠的,所必須具備的四個特性:原子性(atomicity,或稱不可分割性)、一致性(consistency)、隔離性(isolation,又稱獨立性)、持久性(...
TSDB - Time series database
第一次聽說這種 database,記錄一下: - 數據結構簡單 - 資料量大 - 寫入多於讀取 (95%~99% Write) - 照時間順序寫入,幾乎不會有 update 操作(讀取也是) - bulk delete,通常都直接刪一個區間內的資料,不會刪單一資料(讀取...
Harmless migration file in Rails could be harmeful
討論一個看起來很正常的 migration file 可能造成的問題,ex: ```rb class MyMigration < ActiveRecord::Migration[5.2] def change add_reference :payments, ...
How to test with Salesforce Heroku Connect - Part 2
In my previous post [How to test with Salesforce Heroku Connect ](https://waynechu.cc/posts/290-how-to-test-with-salesforce-heroku-conne...
Precision and Scale in database
位數 (Precision) 是指數字中總共的位數。 小數位數 (Scale) 則是指數字中小數點右方的位數。 例如 123.45 的位數是 5,小數位數是 2。 precision 又稱精度 https://docs.microsoft.com/zh-tw/sql/...
Rails lock! and with_lock
Basically, `ActiveRecord::Locking::Pessimistic#with_lock` will yield your code inside transaction but `ActiveRecord::Locking::Pessimistic...
Order By NULL 時的情形
Default 值: | NULL Ordering Behavior/ Database Types | MARIADB/MYSQL | SQLSERVER | HSQLDB | DB2 | ORACLE | POSTGRESQL | | --- | --- | ---...
How to test with Salesforce Heroku Connect
## What is Heroku Connect? This is a tool provided by Salesforce and Heroku, it will automatically sync all your salesforce tables int...
Dump production database to local with Rails
```rb # lib/tasks/db/pull.rake # # Usage # # dump the development db # rake db:dump # # dump the production db # RAILS_ENV=production r...
How to Create Postgres Indexes Concurrently in ActiveRecord Migrations
之前的文章提到要避免 deployment downtime 其中一個方式是 create index concurrently http://everyday1percent.blogspot.com/2018/05/rails-deployment-downtime....
PG::ConnectionBad: FATAL: Peer authentication failed for user XXX
sudo vim /etc/postgresql/9.5/main/pg_hba.conf 加一行: ``` local all your_user_name trust `...
How rails store your migration history
有時候手動操作 database 刪掉了 table,於是乎 rails migration 跟 database 就對不起來了,要怎麼樣騙 rails 說上一個 create table 的 migration file 沒跑過呢?how does rails trac...
釐清 DatabaseCleaner strategy 跟 clean_with
```rb config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) end ``` ### 釐清...
用 foreign_key 確保資料相依正確性 (referential integrity)
Rails 4.2 以後開始支援 database 的 foreign key,很好的文章: https://robots.thoughtbot.com/referential-integrity-with-foreign-keys 我們會用 ```rb class...
PG::ConnectionBad: could not connect to server
今天遇到一個問題 我安裝 postgresapp 並執行了 server 但是 rails 找不到並噴了下面的 error: ``` ➜ rails db:create could not connect to server: No such file or dir...
database 到底要不要對 boolean 值打 index 呢?
真實情況是: it depends but depends on what? 主要可以根據以下兩個基本的原則來 depends boolean index 會有效能上幫助的時機: 1. 跟其他欄位一起打 index 2. 某一部分的 records 暫少數的時候,...
reddit 上的 rails performance 總結
The Complete Guide to Rails Performance 這本書 https://www.railsspeed.com/ 的總結: * Measure. If you have a serious project a cost of NewRelic...
DATETIME 和 TIMESTAMP 的差別
這邊說的是 database type 幾個結論 1. datetime 存了 date 和 time,可能用到 8 bit 2. datetime 可以支援的 range 比較廣,從 1000~9999 years 都可以 3. timestamp 則是存從 epoc...
postgresql explain
可以看 sql 的複雜度和 cost,很方便的 sql command https://www.postgresql.org/docs/9.4/static/using-explain.html
什麼是 btree (balance tree) (b-)
常常看到 postgresql 的 index 都是用 btree 的方式 index,但一直沒時間去研究什麼是 btree,最近發現一個不錯的維信號用漫畫的方式解釋各種演算法相關的東西,剛好看到 b- b+ 的介紹,該是時候學習一下了~ 所謂的 b- 其實唸作 ba...
B+ tree (B plus tree)
前一篇學習了 什麼是 B- balance tree,立馬再來補習一下 B+ 其實 B+ tree 就是 B- 的升級版 主要的差別在於「子節點有母節點的資訊」,並且「出現在子節點中的母節點元素都是子節點中最大的元素」,不囉唆,看圖:  self.class.connection.execute "update accoun...