How can I optimize this query?
You can make a coverage index:
CREATE INDEX ix_table1_456__123 ON table1 (col4, col5, col6) INCLUDE (col1, col2, col3)
and the query doesnβt even have to search the table.
If I add indexes for each column in the where clause, will this have any meaning?
This is likely to improve the query compared to no indexes at all, but creating a composite index with coverage is likely to be better.
However, if each of your columns has high power (i.e., or next to UNIQUE ), then creating separate indexes can even improve the query compared to a composite index.
This is especially true if some of the columns are large (for example, VARCHAR(400) ), and another, small column has high power.
If I have 10 columns in the where clause, should all of these 10 columns have an index in them?
If you have columns 10 , there is, as I said, a trade-off between increased key size (which degrades performance) and increased selectivity.
If, say, the first columns of 3 unique or almost unique, then adding additional columns will not increase selectivity, but will increase the key size.
The index will become larger, which will require additional time to search in it.
You cannot create an index in all columns 10 if columns 3 offer selectivity that is high enough since moving a larger index will be more expensive than reading some additional keys.
You can read this article on your blog: