class: center, middle, inverse, title-slide # Database Querying with SQL ## Searching efficiently ### Ben Baumer ### SDS 192April 22nd, 2020(
http://beanumber.github.io/sds192/lectures/mdsr_sql_05-keys.html
) --- class: center, middle, inverse # Keys and Indexes --- background-image: url(http://static.libsyn.com/p/assets/c/2/1/b/c21be1062e52949b/Unused-Indexes-vs-Foreign-Keys.png) background-size: contain --- ## Keys and Indexes .footnote[https://en.wikipedia.org/wiki/Primary_key] - Keys: - `PRIMARY KEY`: unique, non-`NULL`, only one per table - `UNIQUE KEY`: unique, may include `NULL` - [`FOREIGN KEY`](https://en.wikipedia.org/wiki/Foreign_key): references primary key in another table -- - Indexes: - No constraints, just about speed - Take up space on disk - Will they be used? -- - A `PRIMARY KEY` is always indexed --- background-image: url(https://i.imgur.com/pDq0n.png) background-size: contain --- ## A DB without indexes is like Dory .center[![](https://i.ytimg.com/vi/rKjxJqIQTsE/maxresdefault.jpg)] --- ## An index is a lookup table .center[![](http://ptgmedia.pearsoncmg.com/images/bok_0672324423/elementLinks/16fig01.gif)] .footnote[https://en.wikipedia.org/wiki/Database_index] --- ## Consider a book index... .pull-left[ ![](https://fiverr-res.cloudinary.com/images/q_auto,f_auto/gigs/31028005/original/de16fccf3ce3ba8a301e5c1d3ff05afee248c768/create-your-back-of-book-index.jpg) ] -- .pull-right[ - Helps us avoid checking each row (i.e., [table scan](https://en.wikipedia.org/wiki/Full_table_scan)) - Takes time to build! - Good index will reduce search time from `\(O(n)\)` to `\(O(log(n))\)` .footnote[https://en.wikipedia.org/wiki/Big_O_notation] ] --- ## What would be a good index for this class? .pull-left[ ![](https://media.giphy.com/media/SqQtIZBo6q2I0/giphy.gif) ] .pull-right[ - 60 students - last name? - home institution? - first name? - class year? ] --- ## So why not just build indexes on all the columns? .pull-left[ ![](https://media.giphy.com/media/9oI5rWZ3qcacGuiuMb/giphy.gif) ] .pull-right[ - Takes up space on disk - Takes time to build - Slows down inserts - Not always that big of an improvement - What if index is length `\(n\)`? Or 1? ] --- background-image: url(http://static.libsyn.com/p/assets/c/2/1/b/c21be1062e52949b/Unused-Indexes-vs-Foreign-Keys.png) background-size: contain