1

I have a table with the following columns:

ID (VARCHAR)
CUSTOMER_ID (VARCHAR)
STATUS (VARCHAR) (4 different status possible)
other not relevant columns

I try to find all the lines with customer_id = and status = two different status.

The query looks like:

SELECT *
FROM my_table
WHERE customer_id = '12345678' AND status IN ('STATUS1', 'STATUS2');

The table contains about 1 mio lines. I added two indexes on customer_id and status. The query still needs about 1 second to run.

The explain plan is:

1.  Gather      
2.  Seq Scan on my_table
    Filter: (((status)::text = ANY ('{SUBMITTED,CANCELLED}'::text[])) AND ((customer_id)::text = '12345678'::text))

I ran the 'analyze my_table' after creating the indexes. What could I do to improve the performance of this quite simple query?

2
  • Could you share (in plain text) the complete results from EXPLAIN(ANALYZE, VERBOSE, BUFFERS) for this query? And why do you have a VARCHAR for everything, even the id's? Commented Aug 19, 2022 at 8:29
  • 1
    Did you try an index on (id, status)? Commented Aug 19, 2022 at 8:34

1 Answer 1

1

You need a compound (multi-column) index to help satisfy your query.

Guessing, it seems like the most selective column (the one with the most distinct values) is customer_id. status probably has only a few distinct values. So customer_id should go first in the index. Try this.

ALTER TABLE my_table ADD INDEX customer_id_status (customer_id, status);

This creates a BTREE index. A useful mental model for such an index is an old-fashioned telephone book. It's sorted in order. You look up the first matching entry in the index, then scan it sequentially for the items you want.

You may also want to try running ANALYZE my_table; to update the statistics (about selectivity) used by the query planner to choose an appropriate index.

Pro tip Avoid SELECT * if you can. Instead name the columns you want. This can help performance a lot.

Pro tip Your question said some of your columns aren't relevant to query optimization. That's probably not true; index design is a weird art. SELECT * makes it less true.

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

Comments

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.