an index on ColumnA, say in ASC
Does the query, select * from T where ColumnA = constant
guarantee that the results are always returned in the same order
Short answer: No.
Long answer:
Case 1: If there is only one row with that value, there is no "order" to "guarantee".
Case 2: There are multiple rows with that value. Well, the index does not specify what to do for dup ColumnA
values. InnoDB implements secondary indexes by tacking on the PRIMARY KEY
column(s). So...
Case 2a: If the Optimizer chooses to use that index, it will be in PK order.
Case 2b: If it chooses not to use that index, then the Optimizer probably did a table scan. So, again, it would be in PK order.
Case 2c: You have something else in the query (more in WHERE
, GROUP BY
, LIMIT
, HAVING
, etc) Now all bets are off.
Case 2d: (Sometime in the future.) What if the Optimizer could perform this query in multiple threads? Now the results are jumbled together.
Bottom Line: If you want an order, use ORDER BY
. Period. Full Stop.
And, don't worry too much about performance. If the Optimizer can fold the ORDER BY
into something else (usually GROUP BY
or the tacked on PK), then it will. This leads to the ORDER BY
costing nothing extra.