Indices.

Slides:



Advertisements
Similar presentations
Query Methods (SQL). What is SQL A programming language for databases. SQL (structured Query Language) It allows you add, edit, delete and run queries.
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
CS 540 Database Management Systems
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
SQL Data Definition II Stanislava Armstrong 1SQL Data Definition II.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
Denny Cherry twitter.com/mrdenny.
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
Chapter 18: Modifying SAS Data Sets and Tracking Changes 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
SQL Review Tonga Institute of Higher Education. SQL Introduction SQL (Structured Query Language) a language that allows a developer to work with data.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Microsoft Office 2007 Access Chapter 3 Maintaining a Database.
M1G Introduction to Database Development 5. Doing more with queries.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Introduction MySQL won't actually execute the query, just analyse it EXPLAIN helps us understand how and when MySQL will use indexes EXPLAIN returns a.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL SERVER DAYS 2011 Table Indexing for the.NET Developer Denny Cherry twitter.com/mrdenny.
B+ Trees: An IO-Aware Index Structure Lecture 13.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Data Structures Arrays and Lists Part 2 More List Operations.
Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to.
Thinking in Sets and SQL Query Logical Processing.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
SQL Basics Review Reviewing what we’ve learned so far…….
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Creating Databases for Web applications
3 A Guide to MySQL.
Fundamentals of DBMS Notes-1.
SQL Query Getting to the data ……..
SQL – Python and Databases
Chapter 5 Introduction to SQL.
Module 11: File Structure
Indices.
INLS 623– Database Systems II– File Structures, Indexing, and Hashing
CHP - 9 File Structures.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
SQL – CRUD.
Physical Changes That Don’t Change the Logical Design
Database Management System
SQL Implementation & Administration
Applied CyberInfrastructure Concepts Fall 2017
Database Administration for the Non-DBA
Chapter 8 Working with Databases and MySQL
Chapter 11: Indexing and Hashing
Session #, Speaker Name Indexing Chapter 8 11/19/2018.
Lecture 12 Lecture 12: Indexing.
CPSC-310 Database Systems
HAVING,INDEX,COMMIT & ROLLBACK
Normalization Normalization theory is based on the observation that relations with certain properties are more effective in inserting, updating and deleting.
Lecture 2- Query Processing (continued)
Structured Query Language – The Fundamentals
M1G Introduction to Database Development
Introduction To Structured Query Language (SQL)
Indexes and more Table Creation
A – Pre Join Indexes.
Manipulating Data Lesson 3.
Chapter 11: Indexing and Hashing
Shelly Cashman: Microsoft Access 2016
SQL Server Indexing for the Client Developer
Presentation transcript:

Indices

What is an index? Like the index in the back of a book (remember those?), an index helps you find information quickly. Without an index, you would have to go through the entire book to find a particular topic. But with an index, you can look up the topic in a sorted area, and then jump to the topic you care about. Indices have no external effect on a database; they do not affect any of the other queries. But they do have a huge effect on performance. By default, every table is indexed according to its integer primary key. If there is no primary key, SQLite makes a hidden one called "rowid".

CREATE INDEX CREATE INDEX index_name ON students (name); You can add an index to a database with the above command CREATE INDEX index_name students (id, name); You can have multiple columns to sort your index on. DROP INDEX index_name; DROP INDEX IF EXISTS index_name; As always IF [NOT] EXISTS makes the CREATE/DROP a no-op if an table/view/index with that name already exists

When should you create an index? When you need to often look up rows, but not according to their primary key. An index on some columns in a table allows it to quickly get matching rows when doing a lookup on those columns. But, indices aren't always good, modifications to a table (UPDATE, INSERT, DELETE) all need to adjust the indices on the table, leading to slower performance for those operations. Also, indices take up memory, so large/multiple indices can take up valuable space in your database.

Query Planner The following content is taken from https://www.sqlite.org/queryplanner.html. SQL is a declarative language, not a procedural language, meaning you write what you want to do, not how to do it. It is the job of the SQL implementation to interprete your declaration into actual steps to yield the answer. Most of the time the implementation can do this efficiently, but sometimes it needs you to create indices to make the operations you need done faster.

The table is always ordered according to the key. fruitsforsale CREATE TABLE fruitsforsale ( fruit TEXT, state TEXT, price REAL ); INSERT INTO ... We didn't specify a INTEGER PRIMARY KEY, so a new column "rowid" was made for us as the INTEGER PRIMARY KEY. The table is always ordered according to the key.

This is very slow if you have thousands or millions of rows. fruitsforsale SELECT price FROM fruitsforsale WHERE fruit = 'Peach'; Without an index on fruit, we have to do a full table scan, meaning we must examine every row and check if fruit is equal to 'Peach'. This is very slow if you have thousands or millions of rows.

What is the Big O notation for a simple SELECT statement What is the Big O notation for a simple SELECT statement? (n is number of rows) n log(n) log(n) + log(n) Ooooh...

fruit_index CREATE INDEX fruit_index ON fruitsforsale(fruit); Now we have a index ordered by fruit so we can quickly find rows with a certain fruit value.

SELECT price FROM fruitsforsale WHERE fruit = 'Peach'; fruit_index fruitsforsale Do a binary search in fruit_index for 'Peach', then we find the rowid. Do a binary search in fruitsforsale for that rowid, then we can return the price.

What is the Big O notation for a lookup with an index? n log(n) log(n) + log(n) Ooooh...

SELECT price FROM fruitsforsale WHERE fruit = 'Orange'; fruit_index fruitsforsale Do a binary search in fruit_index for 'Orange', then we find the rowid. Do a binary search in fruitsforsale for that rowid, then we can return the price. Go back to fruit_index and check if the next row is also an orange, if so look up its row in fruitsforsale. Repeat until there are no more 'Orange's.

SELECT price FROM fruitsforsale WHERE fruit = 'Orange' AND state = 'CA'; fruit_index fruitsforsale Same process as before, but we have to exclude some of the 'Orange' rows if the state doesn't match 'CA'.

CREATE INDEX state_index ON fruitsforsale(state);

SELECT price FROM fruitsforsale WHERE fruit = 'Orange' AND state = 'CA'; state_index fruitsforsale Using the state_index instead of the fruit_index follows the same process.

If you have multiple indices, which one is faster? The index with the most rows The index with the least duplicates The index that is sorted Depends (not the diaper)

CREATE INDEX fruit_state_index ON fruitsforsale(fruit, state); Multi-column index that is sorted according to the first column (ties broken by subsequent columns.

Using the fruit_state_index allows only finding the rows we want. SELECT price FROM fruitsforsale WHERE fruit = 'Orange' AND state = 'CA'; fruit_state_index fruitsforsale Using the fruit_state_index allows only finding the rows we want.

SELECT price FROM fruitsforsale WHERE fruit = 'Peach'; fruit_state_index fruitsforsale fruit_state_index has all of utility fruit_index had, we can just ignore the state if it isn't needed.

This is called a covering index - it has all of the columns CREATE INDEX fruit_state_price_index ON fruitsforsale(fruit, state, price); fruit_state_price_index This is called a covering index - it has all of the columns used in the SELECT statement, including the output ('price').

SELECT price FROM fruitsforsale WHERE fruit='Orange' AND states = 'CA'; fruit_state_price_index A covering index for a query doesn't have to consult the original table, because all of the information is in the index. This means the second binary lookup isn't done.

What is the Big O notation for a lookup with an covering index? n log(n) log(n) + log(n) Ooooh...

SELECT price FROM fruitsforsale WHERE fruit='Orange' OR state='CA'; fruit_index state_index This is called the OR-by-UNION technique.

SELECT * FROM fruitsforsale ORDER BY fruit; If we didn't have an index, every row is passed to a sorter function.

What is the Big O notation for sorting without index? n log(n) n log(n) Ooooh...

SELECT * FROM fruitsforsale ORDER BY rowid; No sorting needed, just return the rows as they are. Can you imagine what happens with: "SELECT * FROM fruitsforsale ORDER BY rowid DESC;"

SELECT * FROM fruitsforsale ORDER BY fruit; fruits_index fruitsforsale Using the index, we don't have to pass everything to a sorted function

What is the Big O notation for sorting with an index? n log(n) n log(n) Ooooh...

SELECT price FROM fruitsforsale WHERE fruit='Orange' ORDER BY state; fruits_state_index fruitsforsale Not quite If we are sorting and filtering, using a appropriate index is very fast. Find rows with fruit='Orange' (left most column in index) Output the price for each record (they are already sorted by state).

SELECT * FROM fruitsforsale WHERE fruit='Orange' ORDER BY state; fruits_state_price_index Using the appropriate covering index, we don't even have to do a lookup on the underlying table.