lets say I have
create table mytable(
a VARCHAR(200)
b VARCHAR(200)
c VARCHAR(200)
)
create index on mytable (b)
if I select
select a, b, c from mytable;
would it be sorted by b?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are two main types of indexes: clustering and non-clustering.
The clustering index determines physical row order in a table. Inserting into a table (or updating the respective field) causes the database engine to re-order the data so the field with the clustering index on it is sorted correctly. That’s why there can only be one clustering index on any table.
Non-clustering indexes are copies of the columns, ordered as desired. They exist separately, and physical row order is not connected to them. That’s why there can be as many non-clustering indexes as you want.
Most often a simple select on a single table returns the rows in physical order, so it would not be surprising to receive them sorted the way the the clustering index is.
However, this is not guaranteed and you should not rely on it. Always include an ORDER BY clause if the result set order is of any concern.
If you order by the clustering index, there is not much work to do for the DB engine, but your intent is clear.
If you order by a non-clustering index, there is a little more work to do for the DB, but (depending on table size and data type) it will be orders of magnitude faster than ordering by an entirely unindexed field.