Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
50 views

Lesson 12 Performance Optimization and Best Practices in SQL PDF

Here are the steps to create a covering index for the weather table: 1. Create the weather table and insert sample data CREATE TABLE weather (temp INT, windspeed VARCHAR(45), vapour VARCHAR(45), climate VARCHAR(45)); INSERT INTO weather VALUES (35, '120km/hr', '21', 'summer'); 2. Create a unique index on the temp column CREATE UNIQUE INDEX temp_index ON weather(temp); 3. Add a composite index on the windspeed, vapour, and climate columns ALTER TABLE weather ADD INDEX index_name (windspeed, vapour, climate); 4. View the indexes SHOW INDEX FROM weather; This

Uploaded by

Tilak Datta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Lesson 12 Performance Optimization and Best Practices in SQL PDF

Here are the steps to create a covering index for the weather table: 1. Create the weather table and insert sample data CREATE TABLE weather (temp INT, windspeed VARCHAR(45), vapour VARCHAR(45), climate VARCHAR(45)); INSERT INTO weather VALUES (35, '120km/hr', '21', 'summer'); 2. Create a unique index on the temp column CREATE UNIQUE INDEX temp_index ON weather(temp); 3. Add a composite index on the windspeed, vapour, and climate columns ALTER TABLE weather ADD INDEX index_name (windspeed, vapour, climate); 4. View the indexes SHOW INDEX FROM weather; This

Uploaded by

Tilak Datta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Performance Optimization and Best

Practices in SQL
Learning Objectives

By the end of this lesson, you will be able to:

Implement an execution plan in SQL

Compare VARCHAR, CHAR, and NVARCHAR

List the various index guidelines

Outline queries with SQL indexes


Execution Plan in SQL
Execution Plan in SQL

• Unlike other database solutions, MySQL does not


generate byte-code to execute a query result.
Instead, the query execution plan is used.

Execution plan
• The query execution plan is a list of instructions
that the query execution must follow to produce
the query result.

• It converts the source code (SQL query) into an


executable program.
Execution Plan in SQL

Problem Scenario:
A data analyst of a company wants to check the execution plan for a query to view the
performance and cost of the query.

Objective:
View the execution plan and create an index on the query to improve efficiency of the
query.
Execution Plan in SQL

Create table Emp_info

QUERY

CREATE TABLE EXE_PLAN (


ID int,
NAME varchar(255),
DESIGNATION varchar(255),
CITY varchar(255),
);
Execution Plan in SQL

Insert data in a table

QUERY
INSERT INTO sys.exe_plan (ID,NAME,DESIGNATION,CITY)
VALUES (‘3', ‘STEVE', 'LD', ‘Paris');

Select *from sys.exe_plan;


Execution Plan in SQL

QUERY

select *from sys.exe_plan where NAME='STEVE';


Execution Plan in SQL

• The image shows the execution plan, and the


red box which is in the image is due to the
Performance and high cost of the query.

• Query cost is a metric used in MySQL to


determine how expensive a query is in terms of
the overall cost of query execution.
Execution Plan in SQL

Creating an index to enhance the query performance

Select *from Emp_info where NAME='Pallavi Prakash';

QUERY
create index idx_word on exe_plan(NAME);

select *from sys.exe_plan where NAME='STEVE';


Difference Between CHAR, VARCHAR, and NVARCHAR
Difference Between CHAR, VARCHAR, and NVARCHAR

CHAR VARCHAR NVARCHAR


It's a data type with a set It's a data type with a It's a data type with a
length. changeable length. changeable length.

Non-Unicode characters are Non-Unicode characters are Unicode characters are


stored here. stored here. stored here.

Each character is given one Each character takes one byte Each character takes two
byte of space. of memory. bytes of memory.
Index Guidelines
Index Guidelines

Choosing the right columns and types for an index is a crucial part of building a useful
index.

1 Keep index keys as short as possible

Keep indexes up-to-date


4 Keep separate index keys
2

Keep selective indexes 3


Index Guidelines

The larger an index key gets, the harder it is for a database to use it. An
Keep index keys as
integer key is smaller than a character field that can carry 100 characters.
short as possible
Keep clustered indexes as short as possible.
Index Guidelines

Indexes with a limited percentage of duplicated values are the most effective.
Keep separate index With a decent index, the database will be able to ignore as many records as
keys possible.
Index Guidelines

A selective index has a lot of unique values. A unique index is the most
Keep selective indexes selective of all the indexes, because there are no duplicate values.
Index Guidelines

You will need to see existing indexes as well as delete or rename them, in
Keeping indexes up-to- addition to generating new ones. As the schema or even naming standards
date change, this is part of the database's continual maintenance cycle.
Creating a Clustered Index in SQL
Clustered Index in SQL

• A clustered index is an index that reorders


the actual storage of entries in a table.

• Each table can only have one clustered


index.
Clustered Index in SQL

Following are the essential characteristics of a clustered index:

It's an excellent choice for range


It allows us to store both data or group queries that return min,
and indexes at the same time. max, or count values.

It only has one manner of storing It always takes one or more


data, which is dependent on the key columns to create an index.
values.
Covering Queries With Indexes in SQL

A covered query is a query where all the columns in the query's result set are pulled
from non-clustered indexes.

The careful placement of indexes transforms a query into a covered query.


Clustered Index in SQL

Problem Scenario:

A data analyst wants to sort a table in order, with the help of a clustered index. Create a primary
key which acts as a clustered index in that table.

Objective:

Implement the clustered index to obtain the result.

Instructions:

Refer the emp_data table created before and perform the objectives.
Table Description

Field Name Description

EMP_ID Employee ID

FIRST_NAME First name of the employee

LAST_NAME Last name of the employee

GENDER Gender of the employee (M/F)

Designation of the employee (Junior, Senior, Lead, and Associate Data


ROLE Scientist)

DEPT Name of the department (Retail, Finance, Automotive, and Healthcare)


Table Description

Field Name Description

EXP Experience of the employee

COUNTRY Country where the employee lives

CONTINENT Continent based on the country

SALARY Salary of the employee per month

Rating for the employee (1: Not Achieving Any Goals, 2: Below
EMP_RATING Expectations, 3: Meeting Expectations, 4: Excellent Performance, 5:
Overachiever

MANAGER_ID It is the employee ID for the manager


Clustered Index in SQL

In the query below, the PRIMARY KEY is a


clustered index.
CREATE TABLE
emp_data( `EMP_ID` Varchar NOT NULL, `FIRST_NAME` varchar(45) DEFAULT NULL,
QUERY `LAST_NAME` varchar(3) DEFAULT NULL, `GENDER` varchar(20) DEFAULT NULL, `R
OLE` varchar(25) DEFAULT NULL, `DEPT` varchar(25) DEFAULT NULL, `EXP` varchar(
25) DEFAULT NULL, `COUNTRY` varchar(25) DEFAULT NULL, `CONTINENT` varchar(2
5) DEFAULT NULL, `SALARY` INT DEFAULT NULL, `EMP_RATING` INT
CREATE TABLE DEFAULT NULL, `MANAGER_ID` varchar(25) DEFAULT NULL, PRIMARY KEY (`EMP_ID
emp_data( `EMP_ID` Varchar NOT NULL, `FIRST_NAME` varchar(45) DEFAULT NULL, `LAST_NAME` var
`),index);
char(3) DEFAULT NULL, `GENDER` varchar(20) DEFAULT NULL, `ROLE` varchar(25) DEFAULT NULL, `D
EPT` varchar(25) DEFAULT NULL, `EXP` varchar(25) DEFAULT NULL, `COUNTRY` varchar(25) DEFAULT N
ULL, `CONTINENT` varchar(25) DEFAULT NULL, `SALARY` INT DEFAULT NULL, `EMP_RATING` INT
DEFAULT NULL, `MANAGER_ID` varchar(25) DEFAULT NULL, PRIMARY KEY (`EMP_ID`)//
clustered index UNIQUE KEY `SALARY` (`SALARY`) ;
Clustered Index in SQL

Output
Assisted Practice: Covering Query

Duration: 15 min

Problem Statement: You are required to create the temperature as unique, add new index columns,
and view the index of the weather table for data integrity.
Assisted Practice: Covering Query

Steps to be performed:
Step 1 : Creating the weather table and inserting values in it:
CREATE

CREATE TABLE weather ( temp INT NOT NULL, windspeed


varchar(45) NOT NULL, vapour varchar(45) NOT NULL,
climate varchar(45) NOT NULL );

- INSERT

INSERT INTO weather(temp,windspeed,vapour,climate) VALUES


('35','120km/hr','21','summer');
Assisted Practice: Covering Query

Step 2 : Querying to create unique index:

QUERY

CREATE UNIQUE INDEX temp_index ON weather(temp);

Step 3 : Querying to add new index:

QUERY

ALTER TABLE weather ADD INDEX


index_co1_col2(windspeed,vapour,climate);
Assisted Practice: Covering Query

Step 3 : Querying to show index:

QUERY

SHOW INDEX FROM weather;

Output :
Common Table Expression
Common Table Expression

Each statement or query in MySQL generates a temporary result or relation.


CREATE, INSERT, SELECT, UPDATE, DELETE, and other statements employ a
common table expression (CTE) to name the temporary results sets that exist
within the execution scope of that statement.
Common Table Expression

Following are some of the most important aspects of CTE:

WITH clause allows you The execution scope of


to define several CTEs CTE exists within the
in a single query. statement in which it is
used.

WITH clause is CTE makes code


used to define maintainability
CTE. easier.
Common Table Expression

The basic syntax of CTE in MySQL is as follows:

SYNTAX
WITH cte_name (column_names) AS (query)
SELECT * FROM cte_name;
Common Table Expression

Problem Scenario:
A data analyst wants to apply a CTE to the table with first name, last name, and the
country of the person whose salary is greater than or equal to 4000.

Objective:
Implement the CTE to obtain the required result.

Instructions:
Refer the emp_data table which was created and shown before. Perform the objectives
mentioned above.
Common Table Expression

Following emp_data table is used to show the CTE:


Common Table Expression

CTE QUERY

WITH emp_in_Germany AS
(
SELECT * FROM sys . emp_data WHERE COUNTRY = 'GERMANY'
)

SELECT FIRST_NAME , SALARY , COUNTRY from emp_in_Germany WHERE SALARY >= '4000’ order by FIRST_NAME;
Common Table Expression

Output:

WITH emp_in_Germany AS
(
SELECT * FROM sys . emp_data WHERE COUNTRY = 'GERMANY'
)

SELECT FIRST_NAME , SALARY , COUNTRY from emp_in_Germany WHERE SALARY >= '4000’ order by FIRST_NAME;
Assisted Practice: Common Table Expressions

Duration: 20 mins
Problem statement: You’ve been asked to train a junior analyst on Common Table Expressions (CTE). You’ve
been given a predefined problem to use. Using the data below, you need to guide the junior analyst to write a
query using CTE to fetch the count of unique policy number for policies in India.

CUST_ID POLICY_NO COUNTRY_ID CLAIM_AMT


C1 PO21 1 1000 COUNTRY_ID NAME
C2 PO22 1 3200
1 US
C3 PO33 2 5000
C4 PO44 3 4500 2 India
C5 PO55 2 1200
3 UK
C6 PO44 3 4500
C7 PO55 2 1200

Claim table Country table


Assisted Practice: Common Table Expressions

Steps to be performed:
Step 01: Create the CLAIM table

SQL Query
CREATE TABLE CLAIM(
CUST_ID TEXT,
POLICY_NO TEXT,
COUNTRY_ID INT,
CLAIM_AMT INT
);
Assisted Practice: Common Table Expressions

Output:
Assisted Practice: Common Table Expressions

Step 02: Create the COUNTRY table

SQL Query
CREATE TABLE COUNTRY(
COUNTRY_ID INT,
NAME TEXT);
Assisted Practice: Common Table Expressions

Output:
Assisted Practice: Common Table Expressions

Step 03: Insert records in the CLAIM table

SQL Query
INSERT INTO CLAIM(CUST_ID,POLICY_NO,COUNTRY_ID,CLAIM_AMT)
VALUES("C","PO21",1,1000),
("C2","PO22",1,3200),
("C3","PO33",2,5000),
("C4","PO44",3,4500),
("C5","PO55",2,1200),
("C6","PO44",3,4500),
("C7","PO55",2,1200);
Assisted Practice: Common Table Expressions

Output:
Assisted Practice: Common Table Expressions

Step 04: Insert records in the COUNTRY table

SQL Query
INSERT INTO COUNTRY(COUNTRY_ID, NAME)
VALUES(1,"US"),
(2,"India"),
(3,"UK");
Assisted Practice: Common Table Expressions

Output:
Assisted Practice: Common Table Expressions

Step 05: Display the CLAIM and COUNTRY tables

SQL Query
SELECT * FROM CLAIM;
SELECT * FROM COUNTRY;
Assisted Practice: Common Table Expressions

Output:
Assisted Practice: Common Table Expressions

Step 06: Write a query using CTE to fetch the count of unique policy numbers in India

SQL Query
WITH India_ID AS
(SELECT COUNTRY_ID
FROM COUNTRY
WHERE NAME = 'India')
SELECT COUNT(DISTINCT POLICY_NO)
FROM CLAIM
WHERE COUNTRY_ID IN (SELECT COUNTRY_ID FROM India_ID)
Assisted Practice: Common Table Expressions

Output:
SQL Best Practices
SQL Best Practices

The numeric functions are used to perform numeric manipulation or


mathematical operations.

Always check for NULLS in your Don’t use a query again if it does
data not serve its entire function
String Functions

SQL Best
Practices

Always keep an eye on the Avoid sub queries, and do joins


execution plan and track of the or write functions if required
time costs
SQL Best Practices

The numeric functions are used to perform numeric manipulation or


mathematical operations.

Use the right indexes for faster Improve readability and


search results maintainability, and ensure
that the correct columns
String Functions are retrieved
SQL Best
Practices

When your SQL statement has In your INSERT statements,


several sources, always utilize always use a column list
table aliases
Knowledge Check
Knowledge
Check
Which of the following is one of the best practices in SQL?
1

A. Use the right indexes for slower search results

B. Make subqueries, and do not make joins or write functions

C. Always check for NULLS in your data

D. Use a query again if it does not serve its entire function


Knowledge
Check
Which of the following is one of the best practices in SQL?
1

A. Use the right indexes for slower search results

B. Make subqueries, and do not make joins or write functions

C. Always check for NULLS in your data

D. Use a query again if it does not serve its entire function

The correct answer is: C

Always check for NULLS in your data. This is one of the main SQL practices.
Knowledge
Check
Which of the following stores Unicode characters?
2

A. CHAR

B. VARCHAR

C. NVARCHAR

D. None of the above


Knowledge
Check
Which of the following stores Unicode characters?
2

A. CHAR

B. VARCHAR

C. NVARCHAR

D. None of the above

The correct answer is: C

Unicode characters are stored in NVARCHAR.


Knowledge
Check
Which of the following are index guidelines?
3

A. Keep index keys as short as possible

B. Keep separate index keys

C. Not keeping indexes up-to-date

D. Keeping a single key for indexes


Knowledge
Check
Which of the following are index guidelines?
3

A. Keep index keys as short as possible

B. Keep separate index keys

C. Not keeping indexes up-to-date

D. Keeping a single key for indexes

The correct answers are: A,B

Index guidelines are keeping separate index keys and making them as short as possible.
Lesson-End Project: Airline Customer Expense Analysis

Problem statement:
You are working for an airline company and this company has organized an
SQL hackathon. You have been chosen to represent the LOYALTY team in the
hackathon.

Objective:
The challenge is to overcome the constraint of utilizing CTE to assess the time
period and customers based on spend patterns.
This analysis will help the company design attractive campaigns for low- and
high-spend customers, decide in which quarter deals should be completed,
etc.
Lesson-End Project: Airline Customer Expense Analysis

Tasks to be performed:

Step 01: Create a table containing the columns CustID, CustNAME, Quarter,
and TravelSpend and name it "customer_spend"

Step 02: Insert values in the customer_spend table

Step 03: Find the total spend across each quarter

Step 04: Find the average spend across all quarters


Lesson-End Project: Airline Customer Expense Analysis

Tasks to be performed:

Step 05: Using CTE, find the specific quarter(s) when the spend was better
than the average spend across all quarters

Step 06: Using CTE, find the list of customers who have spent less than the
average spend of all customers

Step 07: Using CTE, find the list of customers who have spent more than the
average spend of all customers
Key Takeaways

Clustered index always uses one or more columns for creating


an index.

Choosing the right columns and types for an index is a crucial


part of building a useful index.

A covered query is a query where all the columns in the query's


result set are pulled from non-clustered indexes.

The query execution plan is a list of instructions that the


query execution must follow to produce the query result.

You might also like