sql - does an index provide order by itself? -


lets have

create table mytable(  varchar(200)  b varchar(200)  c varchar(200) )  create index on mytable (b) 

if select

select a, b, c mytable; 

would sorted b?

there 2 main types of indexes: clustering , non-clustering.

the clustering index determines physical row order in table. inserting table (or updating respective field) causes database engine re-order data field clustering index on sorted correctly. that's why there can 1 clustering index on table.

non-clustering indexes copies of columns, ordered desired. exist separately, , physical row order not connected them. that's why there can many non-clustering indexes want.

most simple select on single table returns rows in physical order, not surprising receive them sorted way the clustering index is.

however, not guaranteed , should not rely on it. always include order clause if result set order of concern.

if order clustering index, there not work db engine, intent clear.

if order non-clustering index, there little more work db, (depending on table size , data type) orders of magnitude faster ordering entirely unindexed field.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -