1

Given a table people e.g.:


id  name  emailed_at
1   Foo   2018-01-01
2   Bar   NULL

Is it necessary to add an index to emailed_at to have a performant SELECT * FROM people WHERE emailed_at IS NULL query?

UPDATE: The actual table has around 12 columns, hundreds of thousands of rows, and only the most recent (~ 1 day old) records should have a NULL in that field.

4
  • 2
    An index is only as helpful as its selectivity. If most records have null value for emailed_at, the index is not going to help. If only a few nulls are present, the index is going to speed things up. Commented Apr 26, 2018 at 18:45
  • How many rows does the table have? What percentage of them have a null email? Commented Apr 26, 2018 at 19:11
  • Updated with more info. Sounds like an index will help? Commented Apr 26, 2018 at 19:33
  • Create the index then check the execution plan and you will know Commented Apr 26, 2018 at 19:34

1 Answer 1

3

For two rows, there is no performance issue at all. For a large table, an index is going to help. IS NULL is the same as any other "equality" comparison. It can take advantage of an index.

The benefit of indexing is when the table spans multiple (many?) data pages. An index generally reduces the number of data pages that need to be processed. If a lot of the emailed_at records are NULL (such as 50% in your example), then any given page is probably going to have NULL values -- and the index is much less useful.

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

2 Comments

Updated with more info. Sounds like an index will help?
@jemminger . . . Yes, an index should be helpful in the case you describe.

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.