English subtitles

← Indexes - Web Development

Get Embed Code
4 Languages

Showing Revision 10 created 05/25/2016 by Udacity Robot.

  1. So far, we've been doing--in our Phyton IDE--
  2. we've been doing what are called sequential scans.
  3. And a sequential scan is where you have a list of something.
  4. In our case, links.
  5. We have this list of links
  6. that have these IDs--link 1, link 2, link 3.
  7. And we've been doing things like find the link with the ID of 2,
  8. so in which case we write a loop to
  9. go over the list and find the one who has ID 2.
  10. Or you just eyeball it by hand
  11. and answer your quiz just by writing in the answer by hand,
  12. which is actually totally fine because you went through the same process.
  13. You had to look through this whole list of links by hand.
  14. Now, that works fine if you have 25 links,
  15. but if you have a million links or a billion links,
  16. iterating over that entire list is going to take some time,
  17. and we want to make our websites fast,
  18. or at least reasonably responsive.
  19. So that's going to be troublesome.
  20. So I'd like to introduce you to a new concept called Indexes.
  21. And index is just like an index in a book.
  22. They make lookups faster.
  23. One index you're probably familiar with already is the hashtable.
  24. So in Python you can have a dictionary
  25. that looks something like this--
  26. that is a mapping--a key--to a value.
  27. Let's assign this to a variable.
  28. We'll call this just index.
  29. Now, you can do very fast lookups in your code
  30. by writing something that looks like this,
  31. which we'll refer to the index key 2 of this hash table.
  32. And when you do a hashtable lookup--
  33. you learned this in CS 101--
  34. we hash this value, we find it in the hashtable,
  35. and then we return the key--
  36. or we return the value.
  37. We don't have to scan over every element in the list and see if it matches our constraint.
  38. We can jump immediately to that element.
  39. And that makes queries run much faster.
  40. Okay, so let's play around with this in the IDE a little bit
  41. and see what we can do.