4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

is nullでもインデックスは使われる[PostgreSQL]

Last updated at Posted at 2021-06-19

今までis null での検索ではindexは見てないと勘違いしていた。
ふと実行計画を確認したら、どうやらindexを参照している。。。
(* users.nameにはインデックスを作成してる。)

 explain select * from users where name is null;
Bitmap Heap Scan on users
(cost=4.20..13.65 rows=6 width=37)
  Recheck Cond: (name IS NULL)
  ->  Bitmap Index Scan on index_users_on_name
(cost=0.00..4.20 rows=6 width=0)
        Index Cond: (name IS NULL)

考察

nullは値じゃないので、並び替えが必要なb-treeインデックスを貼るのは無理かと思っていた。
nullは末尾か先頭に配置することでnullを含めてインデックスを作ってるのかな?

参考

4
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?