惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
量子位
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
博客园 - Franky
M
MIT News - Artificial intelligence
U
Unit 42
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
J
Java Code Geeks
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
T
The Blog of Author Tim Ferriss
V
V2EX

Nic Lin's Blog

謝明真 - 高效領導力的課後筆記 NFT 開發實戰!基礎智能合約入門 (3) NFT 開發實戰!基礎智能合約入門 (2) NFT 開發實戰!基礎智能合約入門 (1) 如何自我檢測 log4j CVE 漏洞 Rails 如何在資料寫入時記錄來源 IP 位置 如何經營工程師 Youtube 頻道 - Part 8 營收篇 如何經營工程師 Youtube 頻道 - Part 7 酸民文化篇 如何經營工程師 Youtube 頻道 - Part 5 設備器材篇 如何經營工程師 Youtube 頻道 - Part 4 後製剪輯篇 如何經營工程師 Youtube 頻道 - Part 3 文案企劃篇 如何經營工程師 Youtube 頻道 - Part 2 設備器材篇 如何經營工程師 Youtube 頻道 - Part 1 制訂頻道方向篇 如何經營工程師 Youtube 頻道 - Part 0 Rails 中避免 race condition 的最佳實踐(二) Rails 中避免 race condition 的最佳實踐(一) 10 分鐘整合 google sheet 做自動化開發功能週報 經營 Side Project 300 天所帶來的收穫及挑戰 我的 Youtube 影片製作流程 API 設計時必須注意的 HTTP header 底線問題 如何提升你的程式可讀性之實務技巧(三) 如何提升你的程式可讀性之實務技巧(二) 如何提升你的程式可讀性之實務技巧(一) Ruby 中使用 freeze 優化效能的時機 避免 React 中的 useEffect 無限 render 在 Rails 內輕量使用 Vue Component 的最佳實踐 如何在區域網路用 Docker 架設有 SSL 的 Gitlab 從被問到問人,那些我常問的面試問題 [Rails] 如何漂亮寫出可維護的 query (Maintainable Rails Query) 在已知長度情況下優化 slice 的性能
[Rails] 如何在 Postgres 上使用表達式索引
Nic Lin · 2018-06-09 · via Nic Lin's Blog

為了避免有許多用戶生成同樣的名稱(不區分大小寫的情況),我們在 User create 前去檢查

User.where("lower(user_name) = ?", user_name.downcase)

當 User 的資料集逐漸龐大時,為了加快這個查詢,我們可以添加一個 user_name 的 index 去加速。

但因為在 where 中使用了 lower(user_name), 所以 user_name 的 index 並不會觸發,相當時白打了。

在這種情況下,我們可以用 PostgreSQL 提供的表達式索引(Expression index)來建立

在 Rails 5 以前,如果我們要使用表達式索引,相當麻煩

def up
  execute <<-SQL
    CREATE INDEX user_lower_name_idx ON users (lower(user_name));
  SQL
end

def down
  execute <<-SQL
    DROP INDEX user_lower_name_idx;
  SQL
end

在 Rails 5 後,對表達式索引有完整的支援,我們可以直接如下撰寫:

def change
  add_index :users,
            'lower(last_name)',
            name: "index_users_on_user_name_unique",
            unique: true
end

這樣一來,User.where("lower(user_name) = ?", user_name.downcase) 就可以吃到這組 index_users_on_user_name_unique index。

這裡要注意的是,如果使用了 LIKE %keyword%,這組 index 依然無效

User.where("lower(user_name) like ?", "%#{user_name.downcase}%")

注意:LIKE %keyword% 永遠不會選擇索引掃描,但 LIKE keyword% 有可能選擇

不過 PostgreSQL 上還是有解法的,參考原文:

'The operator classes text_pattern_ops, varchar_pattern_ops, and bpchar_pattern_ops support B-tree indexes on the types text, varchar, and char respectively. The difference from the default operator classes is that the values are compared strictly character by character rather than according to the locale-specific collation rules. This makes these operator classes suitable for use by queries involving pattern matching expressions (LIKE or POSIX regular expressions) when the database does not use the standard "C" locale.'

我們可以使用 varchar_pattern_ops 來解決這個問題, Rails 5 也同樣支援了 operator classes on expression index

def change
  remove_index :users, name: :index_users_on_user_name_unique
  add_index :users,  'lower(user_name) varchar_pattern_ops',
                        name: "index_users_on_username_unique",
                        unique: true
end

參考來源