I have an sql query which fetch the first N rows in a table which is designed as a low-level queue.
select top N * from my_table where status = 0 order by date asc
The intention behind this query is as follows:
- First, this question is intended to be database agnostic, as my implementation will support sql server, oracle, DB2 and sybase. The sql syntax above of "top N" is just an example.
- The table can contain millions of rows.
- N is a relatively small number in comparison, e.g. 100.
- status is 0 when the row is in the queue. Later it is changed to 1 to indicate that it is in processing. After processing it is deleted. So it is expected that at least 90% of the rows in the table will be with status 0.
- rows in the table should be fetched according to their date, hence the
order by
clause.
What is the optimal index to make this query works fastest?
I initially thought the index should be on (date, status)
, but I am not sure about it anymore. Since the status column will contain mostly zeros, is there an added-value to it? Will it be sufficient to index by (date)
alone?
Or maybe it should be (status, date)
?