for what are index used ?

Discussion in 'MySQL' started by w47w47, Oct 2, 2009.

  1. #1
    i read already here:

    http://dev.mysql.com/doc/refman/5.1/en/indexes.html

    and here:

    http://dev.mysql.com/doc/refman/5.1/en/mysql-indexes.html

    about it but i just don't get it. :S

    maybe someone here could explain me this a little bit easier.
     
    w47w47, Oct 2, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    They provide the database references on certain columns which make querying much more efficient.
     
    jestep, Oct 3, 2009 IP
  3. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #3
    Imagine a book that has 20 chapters and you've been told that the answer to all of your problems is in chapter 8.

    How do you get there?

    If there's an index at the beginning of the book, you might see that chapter 8 starts at page 93 so you can quickly turn to page 93. But if there's no index, you might have to turn over every page, from page 1, until you find chapter 8. So using indexes gets you the data faster.

    Indexes store the keys in a set sequence - so in the previous example, the index is the chapter numbers.

    Each index has a pointer to the actual data - so the index for chapter 8 has a pointer taking you directly to page 93.

    Now obviously this is a very simplified description of how an index works. How did you get from the index to page 93? The most obvious way is for you to look and see what the last page in the book was and then guess at the starting position. Indexes use this type of logic as well (but it's a lot more scientific and a lot more complicated).

    Effectively, indexes are small tables that hold the key (index) that you are searching on and a pointer to the actual data. In some systems, the index is small enough to read into computer memory so accessing a big table can be very fast (it is very rare that there is enough memory in a computer to hold all of the data in a big table).

    Hope this helps.
     
    rayqsl, Oct 4, 2009 IP
  4. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thank you rayqsl, i must say really a good explanation. damn... never saw it from this point of view. :> the BOOK is a really good example. :> tnx :p

    now all i have to learn is which index is for what. ;)

    just have to run some tests if i have a PRIMARY KEY or UNIQUE INDEX if the php script continues when i want to add a duplicate or if it does stop. then i know all i need too. :>
     
    w47w47, Oct 5, 2009 IP