Need some advice on how to optimize my articles table for read operations. I have articles table where I store articles editors write. There is requirement that editors can enter an article with a date_publish set in future. These articles can not be displayed in cover page at any time until the publish_date has actually come.
So my question here is should I have an index on date_publish field for better performance? I am using MySQL database, with InnoDB engine. I store dates as unixtimestamps in unsigned INT(11) field.
I when I make a read for list articles for cover page I do something like this:
SELECT articles.* FROM articles WHERE date_publish < $time
Adding an index on the column
date_publishwould optimize the following simple query:However, if you change the query, such as add an
ORDERclause to order by a column other thandate_publish, you may need a compound (multi-column) index to optimize the query.EDIT
To be able to fully utilize an index, a “covering” index must include all columns in the
WHERE,JOINandORDERclauses, usually in that order. So, if you have a range in yourWHEREclause on date_publish, andORDER BYarticle_name, then you may wish to index on both columns (date_publish, article_name). That way MySQL can use the index for both selection and sorting.