Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PostgreSQL index null values?

Tags:

postgresql

If I have a table like so:

Food
-----------------
name | price | x

Let's say I create an index on name & price, but the database has a bunch of null values for both name & price in some of the rows. Does PostgreSQL automatically skip indexing those rows?

like image 396
joslinm Avatar asked Jan 27 '23 21:01

joslinm


1 Answers

NULL values ARE indexed as well.

You can also use an index to speed up queries with a condition like

WHERE col IS NULL

Something that may come as a surprise to Oracle users is that you can have several rows with a NULL in a unique index. But that makes sense because NULL = NULL is not true.

like image 140
Laurenz Albe Avatar answered Feb 15 '23 22:02

Laurenz Albe