Jaylen Jaylen - 1 year ago 201
SQL Question

Is it a good idea to index datetime field in mysql?

I am working on designing a large database. In my application I will have many rows for example I currently have one table with 4 million records. Most of my queries use datetime clause to select data. Is it a good idea to index datetime fields in mysql database?

Select field1, field2,.....,field15
from table where field 20 between now() and now + 30 days

I am trying to keep my database working good and queries being run smoothly

More, what idea do you think I should have to create a high efficiency database?

Answer Source

MySQL recommends using indexes for a variety of reasons including elimination of rows between conditions: http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

This makes your datetime column an excellent candidate for an index if you are going to be using it in conditions frequently in queries. If your only condition is BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 30 DAY) and you have no other index in the condition, MySQL will have to do a full table scan on every query. I'm not sure how many rows are generated in 30 days, but as long as it's less than about 1/3 of the total rows it will be more efficient to use an index on the column.

Your question about creating an efficient database is very broad. I'd say to just make sure that it's normalized and all appropriate columns are indexed (i.e. ones used in joins and where clauses).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download