MySQL BETWEEN command – performance (order)

MySQLorder-byperformancetable

I want to create a table which will contain approx 20,000 records of three fields each. Two of the fields will contain numeric values.

I want to be able to issue a SELECT BETWEEN command. One record will be returned.

I anticipate that certain records will be returned more frequently than others. Will I gain any performance increase by placing these records near the start of the table? (I'm assuming that MySQL reads a table sequentially).

Best Answer

The answer is kind of "no" but more "your question does not have an answer because it's based upon incorrect assumptions".

1) Tables are not really stored in a particular order.

2) Even if they were, how would the DB know that the question had been answered until it had finished reading the entire table. Putting things "up front" would not help.

3) What you ask (range of values) is in the realm of an index. An index has more structure and roughly the concept of "beginning and end" so would be the way to go. Build an index on the values you intend to query.

4) The way indexes are structured means that individual rows can be selected from billions with just a handful of I/O reads. However you need to have the right values indexed to match the queries. Look at the plan to see if it's working for you.