shealtiel shealtiel - 4 years ago 82
MySQL Question

Do indexes speed up greater than > comparison in MySQL?

I have an events tables in my db, which includes among others start_date and end_date columns.

I frequently run queries like

where start_date > 'some starting date' and end_date < 'some end date'


Will I benefit from adding an index to the start_date and end_date columns? I figure out that this is not a = comparison, but maybe anyway.

Answer Source

The MySQL optimizer will use the indexes where it thinks it is appropriate to do so:

A B-tree index can be used for column comparisons in expressions that use the =, >, >=, <, <=, or BETWEEN operators.

...

Sometimes MySQL does not use an index, even if one is available. One circumstance under which this occurs is when the optimizer estimates that using the index would require MySQL to access a very large percentage of the rows in the table. (In this case, a table scan is likely to be much faster because it requires fewer seeks.)

Source: Comparison of B-Tree and Hash Indexes

You might find these interesting:

How MySQL Uses Indexes

And this answer and this answer to Why does MySQL not use an index for a greater than comparison?.

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