English subtitles

← Why do it in the database - Intro to Relational Databases

Get Embed Code
4 Languages

Showing Revision 3 created 05/24/2016 by Udacity Robot.

  1. A lot of folks when they hear about SQL
    features like order by, or limit and
  2. offset, ask the question,
    why do that in the database?
  3. I already know how to sort
    a list in Python, and
  4. I already know how to do slices.
  5. Why don't I just fetch the data back
    to my application code unsorted, and
  6. do the work there?
  7. And it's true that count is
    an awful lot like length.
  8. And limit 100 offset ten is an awful lot
  9. like taking a slice from elements
    ten to a 110 of the result list.
  10. And order by column is an awful lot
    like, sorting by a a key that's
  11. actually kind of complicated, but
    there are a couple of big differences.
  12. Speed and space.
  13. The database can generally do these
    things a lot faster than Python can.
  14. Especially when you get to
    tables with lots of rows or
  15. complicated queues that
    join several tables.
  16. And you can easily have
    a table with millions of rows,
  17. sorting a million items in
    Python can take around a second.
  18. If you're writing a web app, that's
    a second that you're user is staring at
  19. their browser,
    wondering why your app is so
  20. freaking slow, and
    it's taking up memory to do that too.
  21. In contrast, a database can generally
    do these operations much faster.
  22. There are a number of tricks you
    can use to make it faster still.
  23. The big one is called indexing.
  24. We won't deal with indexing
    directly in this course, but
  25. later on there will be some
    notes on how to apply it.
  26. In any event, doing restrictions and
    aggregations in the database,
  27. instead of in Python,
    is a good practice to get in to.
  28. So let's do more of that.