I've following 2 tables in postgresql database -
table1
- col1_id (integer)
- col2 (text)
- col3 (text)
- col4 (text)
- col5 (numeric(1,0))
- col6 (numeric(9,0))
- col7_created_date (timestamp),
- col8 (text)
CREATE UNIQUE INDEX col1_pkey ON table1 USING btree (col1_id),
table2
- col1_id (integer)
- tab2_col2 (text)
- tab3_col3 (text)
- tab4_col4 (integer)
- tab5_col5 (text)
CREATE UNIQUE INDEX table2_pkey ON table2 USING btree (col1_id, tab3_col3)
FOREIGN KEY (col1_id) REFERENCES table1(col1_id) ON UPDATE RESTRICT ON DELETE RESTRICT
Note: From above tables definition, you would have understood that col1_id from table1 is not only a foreign key in table2 but also a part of primary key along with tab3_col3 in table2 to support 1:Many relationship.
The problem here is table1 have 10,00,000 rows & table2 has 50,00,000 records. So my queries are taking min 5 seconds in executing, Here are my queries -
Query1:
SELECT *
FROM table1 t1
WHERE (col6 >= ?)
AND col5 IN (?)
AND (t1.col8 LIKE ? OR t1.col8 LIKE ?)
ORDER BY col7_created_date DESC
LIMIT 50
Query2 :
SELECT COUNT(*)
FROM table1 t1
LEFT JOIN table2 t2 ON t2.col1_id = t1.col1_id
WHERE t1.col7_created_date > ?
AND t2.tab4_col4 = ?
AND t2.tab3_col3 IN (?, ?)
AND a.tab2_col2 IN (?)
Key Points to understand:
- table2 should have separate id column as a primary key to have a good performance because composite key can slow performance with a hig volume of data in tables. I can work on this.
- I can also work in bringing down some old records from both table1 & table2 but as of now this is not an option for me.
- In Query1 I can't remove "col7_created_date DESC" since I want top 50 records only, with pagination controlled from business logic.
- The "LIKE" search in Query1 are created dynamically, it can be 1 or 2..
Questions:
- If I create an index on t1.col8 (Query1) will it improve performance? If so, is there an efficient way to create an index? (NOTE:
t1.col8
are comma separated text max up to 4) - What I should do to improve performance for Query1 & Query2, I am open to change a DDL. Any suggestion(s)?