How database indexes search faster

I read a rail tutorial (http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#sidebar-database_indices), but confused the explanation of the database directions, the author basically suggests that instead search O (n ) time through the list of letters (for logging in) is much faster to create an index by specifying the following example:

To understand the database index, it is useful to consider the analogy of a book index. In the book to find all occurrences of a given string, say, "foobar", you will have to scan each page for "foobar". With a book index, on the other hand, you can just look at "foobar" in the index to see all pages containing "foobar". Source : http://ruby.railstutorial.org/chapters/modeling-users#sidebar:database_indices **

So, from this example, I understand that words can be repeated in the text, so the "index page" consists of unique entries. However, on the railstutorial site, the login is set so that each email address is unique to the account, so how to make the index faster if we can have no more than one occurrence of each letter?

thank

+3
source share
3 answers

Indexing is not (a lot) about duplicates. This is about order.

When you perform a search, you want to have some kind of order that allows you (for example) to perform a binary search to search for data in logarithmic time, and not to search through each record to find you are interested in (this is not the only index type, but it is probably the most common).

, .

( ), , ( ) , . () , , , .

+4

. / . : , , ? , "" , . POI . , DB .

+4

This is faster because the index contains only the values ​​from the column in question, so it spreads over fewer pages than the full table. In addition, indexes typically include additional optimizations, such as hash tables, to limit the number of reads required.

+1
source

All Articles