I want some Moore

Blog about stuff and things and stuff...
mostly about SQL server and .Net
posts - 155, comments - 1387, trackbacks - 33

My Links

SQLTeam.com Links

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer. I'm also a MCP and MCTS for SQL Server. 
Welcome to my blog.

Search this Blog
 

My Blog Feed via Email


Get your Google PageRank
Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

Indexes are a constant problem in understanding for beginners (and the "not so beginners") in the database world. And don't you just love the

hardcore mathematical explanation of B-Trees and their traversal. Personaly I much rather have visual props and a story to support an explanation.

Appealing visual props are even better. That's how this post originated.  So let us begin!

 

librarianSmall

 Imagine you live in a pre-computer-in-every-nook-and-crane world (around 1960's :)). 

 You wake up one morning with a huge craving to read Agatha Christie's books. So you go to the library which has a few million books.

 You walk up to the cute librarian (look left for the appealing visual prop :)) and ask her if she can show you to the Agatha Christie

 books. She goes to the huge drawer (drawer #1) of little cards which hold just basic info about each book like ISBN number, author and

 title and are sorted by ISBN number. She pulls out the first card, looks at its bookshelf id and takes you to the correct bookshelf. 

 You get your book and are on your merry way home. When you read your book you go back to the library, return

 the books and ask the cute librarian (again look left for the appealing visual prop :)) to get you all the books by Agatha Christie written

in the 1951. This time she goes again to the huge drawer of little cards (drawer #2) but these are different than the previous ones. These hold the year of

publishing and the ISBN number of the book. She writes down the ISBN numbers of all books and goes back to the first huge drawer and

looks up cards that match ISBN numbers she found earlier. Again you get your books and are on your merry way home.

You read them return them, get some more books until you muster up the courage to ask the cute librarian out and you live happily ever after.

Nice story isn't it?

 

"So what the hell am i talking about?" you might ask. Well a library is a very good analogy for indexes. How? Let's review:

library                                                              = table

drawer with ISBN Number                               = Clustered index

drawer with year and ISBN Number                = Non-Clustered index

cute librarian                                                  = a perk :)

 

So what you acctually did in this story is this:

You executed a query (i want some books with a condition) and a query processor (cute librarian) performed a Clustered index

seek on the first book hunt and a Non-Clustered index seek on the second book hunt.

It produced a shortest plan for the desired action (go to the bookshelf and return with the books) and it retured the desired data back (your books).

On both times she had to return a full row (a book) of data by locating it in the Clustered index (looking in the Drawer #1).

This operation is also known as a Bookmark Lookup and is the most expensive operation you can do with indexes short of index scan.

 

And how do those huge card drawers translate to clustered and non clustered index?

ISBN Number is the column on which the clustered index is defined. The books on the shelves are sorted accordingly. The bookshelf numbers

are pages of data in the table. The second drawer contains cards with year of publisihing and an ISBN Number. Year of publisihing is a Non-Clustrered index.

Each non-clustered index is connected to a Clustered index, thus the ISBN Number on the second drawer's cards.

 

You can see that as long as the ISBN Number is contained in the Clustered index the non clustered won't be changed.

This means that reindexing (not rebuilding) the Clustered Index won't rebuild the non clustered index as long as ISBN Number stays in

the Clustered Index.

 

But admit it! You don't care about indexes, you just care about the cute librarian :)

 

kick it on DotNetKicks.com
 

Print | posted on Tuesday, September 18, 2007 8:15 PM

Feedback

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

Thanks, Mladen, I enjoyed it!

Something missing that you may want to add:

What happens when I ask the librarian "can I see all of the books written by Agatha Christie that contain the word "monkey"?

That gives a good example what has to happen if you ask for something that seems simple, but it is not covered by an index ..... time to open up and look through every page of every Agatha Christie book!
9/18/2007 10:42 PM | Jeff

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

well i wanted to leave something for the sequel again starring the cute librarian :)
9/18/2007 10:46 PM | Mladen

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

I like the idea of the library as a metaphor for indexes--especially the librarian part <g>--but I don't think that the cards in this case sound like a clustered index. Are you really going to look up an ISBN by year (using the "nonclustered" cards), then go do a lookup into the ISBN cards for... the ISBN?

I think a slightly clearer approach based on the same metaphor might be to call the Dewey Decimal System the clustered index, and the cards alphabetized by (author's last name, book name, whatever)--all of which include the Dewey Decimal number--the nonclustered index(es). Finding your book alphabetically is a nonclustered index seek, and going through the stacks to grab the actual book based on its Dewey Decimal number is the clustered lookup.

What do you think?
9/19/2007 8:54 PM | Adam Machanic

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

hmm... that would work too, not a bad idea.
Thanx Adam!

i did have to look up what the Dewey Decimal System is :)
9/21/2007 10:19 AM | Mladen

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

nicely explained
12/26/2007 1:51 PM | Ajit Singh

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

this is the best explanation ever
1/21/2008 2:50 PM | The Name

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

It was a good explanation.
Thank you
2/12/2008 6:57 AM | Nazia

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

Really it was explained by a good exaple.
3/12/2008 10:28 AM | kanak

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

very nice explanation.. Thank you
4/14/2008 11:18 PM | George

# re: Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

There are clustered and nonclustered indexes. A clustered index is a special type of index that reorders the way records in the table are physically stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain
the data pages.
A nonclustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a nonclustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.


Indexes are a constant problem in understanding for beginners (and the "not so beginners") in the database world. And don't you just love the hardcore mathematical explanation of B-Trees and their traversal. Personally I much rather have visual props and a story to support an explanation.
5/8/2008 11:04 AM | Venkat Rama Raju.K

# What is a Clustered and a Non-Clustered index (plus an appealing visual prop)

i thing dat appealing visual prob help a lot in understanding index betr, without feeling bore...... thks
6/11/2008 2:02 PM | Jiju

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 5 and type the answer here:

Powered by: