문제

I've just found an interesting partial index in my db:

CREATE INDEX orders_idx
  ON orders
  USING btree
  (status)
  WHERE status IS NULL;

As you see it's completely non-selective and imho completely not usefull, or am I missing something?

도움이 되었습니까?

해결책

If you have queries where you filter by status is null, (as mentioned in the comments,) or need to run maintenance on the entries where the status is null, this index may be useful.

For example from a project I used to work on (that didn't have this type of index,) I had an import process that keeps a log/queue. It sets status to queued when starting, running while in progress, and sets status to complete when finished - but sets it to null if there is an error. That will need to be cleaned up, because items in other tables will not have been correctly set to the values needed. I needed to routinely query on status is null to diagnose specific problems, or run cleanup scripts so the database wasn't full of partial imports.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top