{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Done.\n", "Done.\n" ] }, { "data": { "text/plain": [ "[]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%load_ext sql\n", "%sql sqlite:///complaint.db\n", "import time\n", "%sql drop index if exists state_index;\n", "%sql drop index if exists state_product_index;\n", "%sql drop index if exists helpful_index;\n", "%sql analyze" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Activity 8: Indexes\n", "===========\n", "\n", "Let's play with the [consumer complaint database](https://catalog.data.gov/dataset/consumer-complaint-database) from data.gov" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(sqlite3.OperationalError) no such table: complaints [SQL: 'select count(*) from complaints;']\n" ] } ], "source": [ "%sql select count(*) from complaints;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "%sql select * from complaints limit 5;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1: Query without an index\n", "\n", "First, let's start off by writing a query to find the **counts of the top 5 Product, State pairs** in the complaints database (return the product and state as well as the count). Use the single-line syntax for simple timing so we can see how long the query takes:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%time %sql # YOUR QUERY HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exerise 2: Single search key index\n", "\n", "Now create a _single-key_ index such that the above query is faster! The syntax to create an index in SQL is:\n", "> DROP INDEX IF EXISTS index_name;\n", "> CREATE INDEX index_name ON table(attributes);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%%sql\n", "# CREATE YOUR INDEX HERE" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%time %sql # TIME YOUR SAME QUERY HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3\n", "\n", "Now, create a _covering_ index for the query and then see how long it takes to run!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%%sql\n", "# CREATE YOUR INDEX HERE" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%time %sql # TIME YOUR SAME QUERY HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3(b):\n", "\n", "Use EXPLAIN to see if sqlite used/recognized your covering index. EXPLAIN is an operator that tells SQL to explain its query plan... we'll look into this in more depth later. For now, the syntax is:\n", "> EXPLAIN QUERY PLAN your_query_here;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%%sql\n", "# EXPLAIN YOUR QUERY HERE" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }