0

I a have a table structure as below. For fetching the data from table I am having search criteria as mentioned below. I am writing a singe sql query as per requirement(sample query I mentioned below). I need to create an index for the table to cover all the search criteria. It will be helpful somebody advice me.

Table structure(columns):

  1. applicationid varchar(15),
  2. trans_tms timestamp,
  3. SSN varchar,
  4. firstname varchar,
  5. lastname varchar,
  6. DOB date,
  7. Zipcode smallint,
  8. adddetais json

Search criteria will be from API will be fall under 4 categories. All 4 categories are mandatory. At any cost I will receive 4 categories of values for against single applicant.

Search criteria:

  1. ssn&last name (last name need to use function I.e. soundex(lastname)=soundex('inputvalue').

  2. ssn & DOB

  3. ssn&zipcode

  4. firstname&lastname&DOB.

Query:

I am trying to write. Sample query is:

Select * 
from table 
where ((ssn='aaa' and soundex(lastname)=soundex('xxx') 
       or ((ssn='aaa' and dob=xxx) 
       or (ssn='aaa' and zipcode = 'xxx') 
       or (firstname='xxx' and lastname='xxx' and dob= xxxx));

For considering performance I need to create an index for the table. Might be composite. Any suggestion will be helpful.

1

1 Answer 1

0

Some Approaches I would follow:

  1. Yes, you are correct composite index/multicolumn index will give benefit in AND conditions of two columns, however, indexes would overlap on columns for given conditions. Documentation : https://www.postgresql.org/docs/10/indexes-multicolumn.html
  2. You can use a UNION instead of OR.

Reference : https://www.cybertec-postgresql.com/en/avoid-or-for-better-performance/

  1. If multiple conditions could be combined for e.g: ssn should be 'aaa' with any combination, then modifying the where clause with lesser OR is preferable.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Saurabh for response. considering above fields and I mentioned all 4 categories are mandate. I am thinking to go with composite index like below. if prior that anything needs to be taken into account. any impact based on this. I am new to Postgres too. CREATE INDEX test_dupapp_1 ON DUPAPPCHECK using btree (ssn,soundex(lastname)); CREATE INDEX test_dupapp_2 ON DUPAPPCHECK using btree (ssn,dob); CREATE INDEX test_dupapp_3 ON DUPAPPCHECK using btree (ssn,zipcode); CREATE INDEX test_dupapp_4 ON DUPAPPCHECK using btree (upper(lastname),upper(firstname),dob);

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.