1

My main question is, in a single table, do the number of records NOT included in a WHERE clause affect query performance of SELECT, INSERT, and UPDATE?

Say I have a table with 20 million rows, and this table has an indexed error string column.

Pretend 19,950,000 of those records have 0 set for this column, and 50,000 have it set to NULL.

My query does SELECT * FROM pending_emails WHERE error IS NULL.

After some logic in my app, I then need to update those same records by ID to set their error:

UPDATE "pending_emails" SET "error" = '0' WHERE "pending_emails"."id" = 46
UPDATE "pending_emails" SET "error" = '0' WHERE "pending_emails"."id" = 50

I'm trying to determine if I can leave 'completed' records in the database without affecting performance of the active records I'm working with, or if I should delete them (not preferred).

1 Answer 1

1

Typically no. That's the purpose of indexing. You might want to consider a filtered index for this column: https://www.postgresql.org/docs/current/static/indexes-partial.html Then your index isn't even indexing the '0' rows at all.

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

4 Comments

Wow interesting. So, I have a second index on this table for: [:priority, :id] which satisfies my ORDER BY priority ASC, id ASC. Can I also use a partial index for that too?
Yup - see the example they give "Example 11-2. Setting up a Partial Index to Exclude Uninteresting Values"
Every index added to a table will slow it down, so if you are querying the '0' rows for other purposes, that would likely slow down updates for no good reason. But if the '0' rows are hardly queried (i.e. history archive), you could MOVE most of your other indexes to filtered indexes.
@CadeRoux: could you please point your link to a current version of the docs? postgresql.org/docs/current/static/indexes-partial.html

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.