10

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?

3
  • Try inserting null values and check to see if it was indexed? Commented Jun 21, 2018 at 19:32
  • Possible duplicate of Index for nullable column Commented Jun 21, 2018 at 19:38
  • Please do not crosspost. Also asked and answered here: dba.stackexchange.com/questions/210292 Commented Jun 21, 2018 at 20:30

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Will such index work for LEFT JOIN queries?
@Klesun If you want to know if you can use an index to identify NULLs which are generated as the result of an outer join, no. These NULLs don't exist in the table, so they cannot be indexed. But an index can speed up a join for other reasons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.