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.

Slides:



Advertisements
Similar presentations
F28DM Indexes in Oracle 1 F28DM : Database Management Systems Indexes in Oracle Monica Farrow Room: EMG30, Ext: 4160 Material on Vision.
Advertisements

Module 6 Implementing Table Structures in SQL Server ®2008 R2.
Physical Database Design Data Migration/Conversion.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Indexes Rose-Hulman Institute of Technology Curt Clifton.
SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes.
Introduction to Structured Query Language (SQL)
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Denny Cherry twitter.com/mrdenny.
Performing Indexing and Full-Text Searching Lesson 21.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Oracle Data Block Oracle Concepts Manual. Oracle Rows Oracle Concepts Manual.
Chapter 6 Additional Database Objects
TEMPDB Capacity Planning. Indexing Advantages – Increases performance – SQL server do not have to search all the rows. – Performance, Concurrency, Required.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Constraints, Triggers and Index James Wang.
SQL Server Indexes Indexes. Overview Indexes are used to help speed search results in a database. A careful use of indexes can greatly improve search.
Table Indexing for the.NET Developer Denny Cherry twitter.com/mrdenny.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Denny Cherry twitter.com/mrdenny.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
Visual Programing SQL Overview Section 1.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
1 Notes on: Clusters Index and Cluster Creation in SQL Elisa Bertino CS Department and CERIAS Purdue University.
Indexes and Views Unit 7.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
SQL SERVER DAYS 2011 Table Indexing for the.NET Developer Denny Cherry twitter.com/mrdenny.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Chapter 5 Index and Clustering
Session 1 Module 1: Introduction to Data Integrity
Chapter 4 Indexes. Indexes Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage; composed.
Creating Indexes on Tables An index provides quick access to data in a table, based on the values in specified columns. A table can have more than one.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Database Programming Sections 12 – Sequences, Indexes, and Synonymns.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
1 Indexes ► Sort data logically to improve the speed of searching and sorting operations. ► Provide rapid retrieval of specified rows from the table without.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Indexes Part 2 What type of Indexes are there? Make sure you have the pages 2 & 3 of the Lab for Indexes in front of you before playing this presentation.
SQL Basics Review Reviewing what we’ve learned so far…….
Module 6: Creating and Maintaining Indexes. Overview Creating Indexes Understanding Index Creation Options Maintaining Indexes Introducing Statistics.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Fundamentals of DBMS Notes-1.
Web Systems & Technologies
Indexes By Adrienne Watt.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
SQL Implementation & Administration
Introduction to Execution Plans
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Session #, Speaker Name Indexing Chapter 8 11/19/2018.
Lecture 12 Lecture 12: Indexing.
Chapter 4 Indexes.
CH 4 Indexes.
Database systems Lecture 6 – Indexes
CH 4 Indexes.
Introduction to Execution Plans
Introduction to Execution Plans
Introduction to Execution Plans
Presentation transcript:

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 the W section, and search from there alphabetically. This would be an easy task because the phonebook is indexed alphabetically. However, if you were asked to search for the number (146) you would have to go through all the numbers in the phonebook until you find a match

Indexes Indexes allow the SQL server engine to perform fast, targeted data retrieval rather than simply scanning through the entire table. It is faster to search an indexed column than a non-indexed column However, an index must be updated every time the table is updated. For example, every time you insert, update or delete a row in the table, the indexes need to be updated accordingly.  Indexes make searching for data faster, but might make deleting, inserting or updating new data slower.  You should create indexes on your most searched columns only

Index Types 1.Clustered 2.Non-clustered

Clustered Index Determines how a table is written to disk. For example, if your clustered index is created on the lastName column, the rows will be stored in the file on the disk alphabetically by last name, and when you retrieve the names from the table, they will be already ordered alphabetically. You can have only one clustered index per table A clustered index is automatically created on the primary key column, however you can change it to any column in your table (including columns with non- unique values, such as the last name. But its not a good practice to apply a clustered index to a non-unique column).

Clustered Index In this example, a clustered index is created on the primary key The leaf pages of the clustered indexes are the data pages

Non-clustered Index A non-clustered index is completely separate from the data page (because the data page has already been stored based on the clustered index) On the leaf pages of the index, there are pointers to go to the data pages. The pointer from the index to a data row is called a “row locator”

Non-clustered Index Row locators In this case, the data page is not stored alphabetically The index leaf pages only point to the actual data in the data page

Best Practices Create non-clustered indexes for: Foreign key columns Columns used commonly in the following clasues: o JOIN o WHERE o ORDER BY o GROUP BY

Creating an index CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX indexName ON tableName (columnList); You can specify a clustered or non-clustered index, with non-clustered being the default type when the type is not specified You can create an index on a column with unique values by including the UNIQUE keyword. The DB engine will not allow creating a unique index on a column that already includes duplicate data A clustered index is automatically created on the primary key column A non-clustered index is automatically created on a column with a UNIQUE constraint

Creating an index CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX indexName ON tableName (column1, column2 desc); You can create an index on one column or on more than one (composite index) You can specify whether you want the index to be sorted on the column values in an ascending or descending order (the default is ascending)

Index Types Index Types: 1.Clustered (always unique) 2.Nonclustered: 1.Unique 2.Non-unique

Viewing your Indexes To view the indexes you have created on a table: SELECT name, type_desc, is_unique FROM sys.indexes WHERE OBJECT_ID(‘schemaName.tableName')= object_id; You will find the indexes you created explicitly as well as the implicitly created indexes (the primary key and the unique columns)

Example

step1 – create the table CREATE SCHEMA produce; GO CREATE TABLE produce.vegetable ( vegetableID int NOT NULL CONSTRAINT Pkproduce_vegetable PRIMARY KEY, name varchar(15) NOT NULL CONSTRAINT AKproduce_vegetable_name UNIQUE, color varchar(10) NOT NULL, consistency varchar(10) NOT NULL, filler char(4000) NOT NULL );

step2 – create the indexes CREATE INDEX Xproduce_vegetable_color ON produce.vegetable(color); CREATE INDEX Xproduce_vegetable_consistency ON produce.vegetable(consistency); CREATE UNIQUE INDEX Xproduce_vegetable_vegetableID_color ON produce.vegetable(vegetableID, color);

step3 – view the indexes created on your table If you want to view the indexes created explicitly and implicitly on this table, run the following query: SELECT name, type_desc, is_unique FROM sys.indexes WHERE OBJECT_ID(‘produce.vegetable')= object_id;

step4 – the results nametype_descis_unique PKproduce_vegetableCLUSTERED1 Akproduce_vegetable_nameNONCLUSTERED1 Xproduce_vegetable_colorNONCLUSTERED0 Xproduce_vegetable_consistencyNONCLUSTERED0 Xproduce_vegetable_vegetableID_colorNONCLUSTERED1

Deleting an index To delete an index use the DROP INDEX statement: DROP INDEX indexName ON schema.table; Example: DROP INDEX Xproduce_vegetable_consistency ON produce.vegetable;

Performance Optimization with Indexes

When searching for a value from the clustered index column, SQL Server will perform a Seek

A seek operation costs less than a scan operation, because it goes directly to the target row

When searching for a value from a column without an index, SQL Server will perform a scan

A scan operation costs more than a seek operation, because it goes through all the rows in the table ( Estimated CPU cost vs )