In a MySQL database, I have two big tracking tables. Each has a date field. One date field has data type = timestamp, and the other has data type = datetime.
Both tables get thousands of inserts all day tracking site traffic, and both have infrequent (once a month) selects that are in the form of
SELECT * FROM table WHERE month(datefield) = XX and year(datefield) = #*$!X
I am the only one who runs the selects. The selects take a long time, so I am thinking about creating an index on the datefield.
Questions about this:
1. Is it useful/customary to make an index on a date field? I think I read somewhere this is unnecessary, but now I can't find much information about it.
2. How much slower are the inserts going to be if there is an index? I know they are going to be slower, so I wonder if it is advisable to make the index given that I am the only one to benefit from the performance boost?
I wouldn't create an index just for that, but maybe create an index when I need it then delete it when finished, but also I'd look at your query, you're creating an overhead using 2 functions (year(), month()) on a table scan, try using a between statement ie
SELECT * FROM table WHERE datefield BETWEEN '01-Mar-2011' and '31-Mar-2011 23:59:59'
if you think about it, it's exactly the same request as
SELECT * FROM table WHERE month(datefield) = 3 and year(datefield) = 2011