sql - In a nonclustered index, how are the second, third, fourth ... columns sorted? -


i have question sql server indexes has been bugging me of late.

imagine table this:

create table telephonebook (     firstname nvarchar(50),      lastname nvarchar(50),      phonenumber nvarchar(50) ) 

with index this:

create nonclustered index ix_lastname on telephonebook (     lastname,      firstname,      phonenumber ) 

and imagine table has hundreds of thousands of rows.

let's want select last name starts b , firstname 'john'. write following query:

select      *  telephonebook  lastname 'b%'  , firstname='john' 

since index can reduce number of rows need scan because groups of lastnames start b anyway, firstname? or database scan every row starts b find ones first name 'john'?

in other words, how second, third, fourth, ... columns sorted in index? alphabetical in case well, it's pretty easy find johanna? or in sort of random or different order?

edit: why ask, because have read in above select statement, index used narrow down search records lastname starts b, index not used find of rows johanna in (and resort scanning of 'b' rows). , i'm wondering why is? not getting?

as convenient shorthand, keys of index used where clause first inequality. like wildcard considered inequality.

so, index used looking first value. however, entries scanned match on first name, still index usage.

of course, optimizer may decide not use index @ all, if decides full-table scan more appropriate.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -