JasonG
JasonG

Reputation: 5962

How can I find a null field efficiently using index

I have a query where I'm trying to find a null field from millions of records. There will only be one or two.

The query looks like this:

SELECT  * 
FROM “table” 
WHERE “id” = $1 
AND “end_time” IS NULL  
ORDER BY “start_time” DESC LIMIT 1

How can I make this query more performant eg using indexes in the database.

Upvotes: 1

Views: 41

Answers (1)

Vao Tsun
Vao Tsun

Reputation: 51599

try a partial index, smth like:

create index iname on "table" (id, start_time) where end_time is null;

Upvotes: 1

Related Questions