Is it good practice to create index on all columns used in WHERE clause?
It is not good practice to create single-column indexes on all columns mentioned in WHERE clauses. Those indexes don't help your queries much, and they cost time and IO when you do INSERT and UPDATE operations.
It is good practice to create multicolumn indexes that match the WHERE clauses of your high-volume queries.
Your sample query
SELECT * FROM TABLE_1
WHERE COL_1 = ?
AND COL_2 = ?
AND COL_3 = ?
will benefit from a BTREE index on (COL_1, COL_2, COL_3). postgresql can random-access the index to the first row in your table that matches your WHERE clause, then retrieve the rows by scanning the index.
If your query were
SELECT * FROM TABLE_1
WHERE COL_1 = ?
AND COL_TIME >= ?
AND COL_3 = ?
you would want an index on (COL_1, COL_3, COL_TIME). Again, postgresql can random-access the index to the first eligible row, then scan the index sequentially until it gets to the last eligible row. Put the equality-match columns first in the index, then the range match (COL_TIME >= ?) column.
Design your indexes to match
- your database requirements for primary keys and other constraints.
- your high-volume queries.
Just putting single-column indexes on many columns is a n00b mistake. Ask me how I know this sometime. ;-)
Indexing can seem arcane when you first start working with it. Marcus Winand's book https://use-the-index-luke.com/ is a good place to start learning.