SlideShare a Scribd company logo
1 of 49
Download to read offline
Database Querying
Tech Jam -25 november 2015
Rodger oates
Database Querying
• STUFF
• COALESCE vs ISNULL
• The LIKE Predicate
• Combining Predicates
• Joining Queries
• Table Expressions
• Window Functions
A Quick Note on Query Processing
1. FROM
2. JOINS
3. WHERE
4. GROUP BY
5. CUBE | ROLLUP
6. HAVING
7. SELECT
8. DISTINCT
9. ORDER BY
10. TOP
STUFF
REPLACE
• Used to replace parts of a string
• Will perform action through whole of original string
• Format
• REPLACE(<original>, <string to replace>, <replacement>)
• Note: <string to replace> and <replacement> do not need to be equal
in length
STUFF
• Used to insert one string into another
• Replaces characters in range specified with given string
• Format:
• STUFF(<original>, <start index>, <length>, <string to stuff>)
• Examples of use:
• www.sql-server-helper.com/
For XML PATH
• Can be used with STUFF to translate a table of results into a single
result.
• Real world example:
• Comma delimited list of names
• E.g. in names of users assigned to a given site
• See: Usage #6 in www.sql-server-helper.com
COALESCE vs ISNULL
ISNULL
• T-SQL specific
• Takes two inputs
• Format:
• ISNULL(<expression 1>, <expression 2>)
• Returns <expression 1> if not null, else <expression 2>
• Note: Data type of result is same as <expression 1> regardless of
which expression is returned
COALESCE
• Standard SQL
• Takes any number of inputs
• Format:
• COALESCE(<expression 1>, <expression 2>, …, <expression n>)
• Returns first input object that is not null
• Note: Data type of result is the same as the returned expression
The LIKE Predicate
Why LIKE?
• Used to find strings that are similar to a search term
• Without wildcards, predicate would only match with the search term
in its entirety, case insensitive
• With wildcards, we can find matches where only part of the string
matches our term
• Search terms that commence with wildcard(s) do not allow SQL to
use index ordering to speed up the query
• E.g UserFullName LIKE ‘%oates%’
Wildcards in LIKE
Source: Exam 70-461Training Kit
Combining Predicates
Types of combinations
• AND
• Predicates on both sides must be true
• OR
• Either predicate must be true
• NOT
• Negation of succeeding predicate
• Note: when predicate equates to NULL, the NOT of this predicate will still be
NULL
Natural Precedence Rules for Combinations
1. NOT
2. AND
3. OR
e.g.
WHERE col1 = ‘w’ AND col2 = ‘x’ OR col3 = ‘y’ AND col4 = ‘z’
Use of parentheses
• Can use parentheses if different precedence must be given to
predicates
• Previous example would equate to:
• WHERE (col1 = ‘w’AND col2 = ‘x’) OR (col3 = ‘y’ AND col4 = ‘z’)
• However, we could use:
• WHERE col1 = ‘w’ AND (col2 = ‘x’ OR col3 = ‘y’) AND col4 = ‘z’
Joining Queries
Joining Queries
• Types of Join
• Multi-Join Queries
• EXISTS
• Set Operators
• UNION vs UNIONALL
• INTERSECT
• EXCEPT
Types of Join
• CROSS JOIN
• Cartesian product of two tables
• Does not require a link between the tables
• INNER JOIN
• Requires a predicate between the tables (specified using the ON keyword)
• Filters out any rows that do not match the ON predicate
• SELF-JOIN
• Is simply an INNER JOIN
• Joins records in a table with other records in the same table
OUTER JOINS
• Outer joins preserve at least one side of the join if there is no match
• LEFT OUTER JOIN preserves the left table
• E.g. <tableA> LEFT OUTER JOIN <table B> preserves <table A>
• RIGHT OUTER JOIN preserves the right table
• E.g. <tableA> RIGHT OUTER JOIN <table B> preserves <table B>
• FULL OUTER JOIN preserves both tables where there is no match
• Further information on joins atTechNet
A Note on NULLs within JOINs
• Note: When determining row inclusion via predicate, a NULL value on
either side of the comparison will be discarded
• NULL is not a value, it is unknown
Multi-Join Queries
• Often need to look at multiple tables within the same query
• Use joins to link each table together
• Joins are evaluated in order of definition
• Need to be careful when combining outer joins with others, as the
results may not be what you expect
Example
Source: Exam 70-461Training Kit
Why was a supplier not returned?
Source: Exam 70-461Training Kit
How to get round this
Source: Exam 70-461Training Kit
EXISTS
• Accepts a subquery as input:
• WHERE EXISTS (SELECT … FROM <table>)
• Returns true if at least one row returned
• EXISTS doesn’t need to return the result of the subquery
• Rather, true or false depending on whether rows would have been
returned by the subquery
• Can be negated:
• WHERE NOT EXISTS (SELECT … FROM <table>)
Set Operator Guidelines
• Complete rows are matched between the input sets
• Number of columns in all queries must be the same
• Column types for corresponding columns must be compatible
• Considers two NULLs to be compatible
• This is different to JOINs
• Result column names are defined in the first query
• Result ordering can only be defined after the last input query
UNION vs UNION ALL
• UNION unifies the results of two input queries
• Both input queries must have
• UNION has an implicit DISTINCT property
• No duplicates in result
• UNION ALL returns all results, including any duplicates
UNION / UNION ALL UNION ALL
INTERSECT
• Only rows that exist in all input queries are returned
• Has an implicit DISTINCT operator
EXCEPT
• Works with two input queries
• Returns rows that exist in the first query, but not the second
Table Expressions
Types of Table Expression
1. DerivedTable
2. CommonTable Expression (CTE)
3. View
4. InlineTable-Valued Function
• 1 and 2 are only visible within the scope of the current statement and
are not reusable
• 3 and 4 have their definitions stored in the database and can be
reused
Optimisation Notes
• Table expressions are not tables, but definitions
• They do not hold any data
• They interact directly with the underlying tables
• They do not have a performance impact in themselves
• The above points must be noted when using a table expression within
a query operating on a large data set
Derived Tables
• Closely resemble a subquery
• Defined in the FROM clause of the outer query
• E.g. I want the two lowest priced products per category
Source: Exam 70-461Training Kit
Common Table Expressions (CTEs)
• Derived tables cannot be nested, if this is required, the statement
must define multiple instances of the same query
• Higher risk of mistake
• This is a CTE version of the previous query
Note: we could reuse C as many times as
we want, but only within the scope of the
current statement
Source: Exam 70-461Training Kit
A Note on Scoped Tables
• If you need to refer to the same data in multiple statements for a
given query it would be better to retrieve the data into a table.You
have two options:
• Create a temporary table
• CREATETABLE #<table name>
• Remember to use a DROPTABLE #<table name> command
• Declare a table variable
• DECLARE @<table name>TABLE
• This way, you are not having to go back to the raw tables to retrieve
the same data
Views and Inline Table-Valued Functions
• Define queries in the database, which can then be reused
• Do not store any data
• Performance issues arise from how it is used
• Views do not allow input parameters
• Functions do allow input parameters
Window Functions
Window Functions
• Window Aggregate Functions
• Window Ranking Functions
Window Aggregate Functions
• Same as group aggregate functions
• SUM,COUNT,AVG, MIN, MAX
• Are applied to a window of rows defined by the OVER clause
• Do not hide row detail
• Can mix detail and aggregated elements in the same query
• Without having to define a load of columns in the group section
Example
Window Ranking Functions
• ROW_NUMBER
• Unique number based on ORDER BY clause
• RANK
• Number based on ORDER BY clause
• Assigns same number when ordering values are tied
• Numbers may therefore not be sequential
Window Ranking Functions
• DENSE_RANK
• Similar to RANK
• Numbers are sequential
• NTILE
• Arranges rows within a partition of a number of equally sizes tiles
• Number of rows per partition is calculated by
• <total number of rows> / <partition size>
• If this calculation produces a remainder, additional row(s) are assigned to tiles in
their order defined by the ORDER BY clause (see example below)
Example
• Assuming 800 rows of data, and the following query
Source: Exam 70-461Training Kit
Example
custid orderid Val rownum rnk densernk ntile100
12 10782 12.50 1 1 1 1
27 10807 18.40 2 2 2 1
66 10586 23.80 3 3 3 1
76 10767 28.00 4 4 4 1
54 10898 30.00 5 5 5 1
88 10900 33.75 6 6 6 1
48 10883 36.00 7 7 7 1
41 11051 36.00 8 7 7 1
71 10815 40.00 9 9 8 2
38 10674 45.00 10 10 9 2
53 11057 45.00 11 10 9 2
75 10271 48.00 12 12 10 2
Source: Exam 70-461Training Kit
PARTITION
• Can be applied to window ranking functions
• Each partition has its own numbering
• E.g. to get the latest child event for each parent
Resources
• STUFF: www.sql-server-helper.com
• JOINS:TechNet
• Training:
• MicrosoftVirtual Academy
• MSDN
• Reference tutorial:
• http://www.tutorialspoint.com/sql/sql_tutorial.pdf

More Related Content

What's hot

What's hot (19)

SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
T sql語法之 cte 20140214
T sql語法之 cte 20140214T sql語法之 cte 20140214
T sql語法之 cte 20140214
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
How oracle query works (The SQL Optimizers)
How oracle query works (The SQL Optimizers)How oracle query works (The SQL Optimizers)
How oracle query works (The SQL Optimizers)
 
Sql
SqlSql
Sql
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Select Queries
Select QueriesSelect Queries
Select Queries
 
Displaying data from multiple tables
Displaying data from multiple tablesDisplaying data from multiple tables
Displaying data from multiple tables
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQL
 
Sql Tutorials
Sql TutorialsSql Tutorials
Sql Tutorials
 
Assignment 4
Assignment 4Assignment 4
Assignment 4
 
Array
ArrayArray
Array
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query Optimization
 
7. Using Sub Queries
7. Using Sub Queries7. Using Sub Queries
7. Using Sub Queries
 
Referential integrity
Referential integrityReferential integrity
Referential integrity
 
Assignment 3
Assignment 3Assignment 3
Assignment 3
 
Database constraints
Database constraintsDatabase constraints
Database constraints
 

Similar to Database Querying Techniques for Performance and Optimization

Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptxSherinRappai1
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptxSherinRappai
 
Practice on Practical SQL
Practice on Practical SQLPractice on Practical SQL
Practice on Practical SQLHideshi Ogoshi
 
ADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database SystemsADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database SystemsJoshua Costa
 
ExcelTipsAndTricks.pptx
ExcelTipsAndTricks.pptxExcelTipsAndTricks.pptx
ExcelTipsAndTricks.pptxJohn Donahue
 
OracleSQLraining.pptx
OracleSQLraining.pptxOracleSQLraining.pptx
OracleSQLraining.pptxRajendra Jain
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 Richie Rump
 
Unit 5 composite datatypes
Unit 5  composite datatypesUnit 5  composite datatypes
Unit 5 composite datatypesDrkhanchanaR
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-dbuncleRhyme
 
Sql coding-standard-sqlserver
Sql coding-standard-sqlserverSql coding-standard-sqlserver
Sql coding-standard-sqlserverlochaaaa
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL QueriesAchievers Tech
 

Similar to Database Querying Techniques for Performance and Optimization (20)

Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Practice on Practical SQL
Practice on Practical SQLPractice on Practical SQL
Practice on Practical SQL
 
ADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database SystemsADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database Systems
 
ExcelTipsAndTricks.pptx
ExcelTipsAndTricks.pptxExcelTipsAndTricks.pptx
ExcelTipsAndTricks.pptx
 
OracleSQLraining.pptx
OracleSQLraining.pptxOracleSQLraining.pptx
OracleSQLraining.pptx
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
 
SQL_Part1
SQL_Part1SQL_Part1
SQL_Part1
 
2..basic queries.pptx
2..basic queries.pptx2..basic queries.pptx
2..basic queries.pptx
 
Access 03
Access 03Access 03
Access 03
 
Advanced sql
Advanced sqlAdvanced sql
Advanced sql
 
Unit 5 composite datatypes
Unit 5  composite datatypesUnit 5  composite datatypes
Unit 5 composite datatypes
 
Optimizing MySQL queries
Optimizing MySQL queriesOptimizing MySQL queries
Optimizing MySQL queries
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
 
Sql coding-standard-sqlserver
Sql coding-standard-sqlserverSql coding-standard-sqlserver
Sql coding-standard-sqlserver
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Sql performance tuning
Sql performance tuningSql performance tuning
Sql performance tuning
 
MySQL JOIN & UNION
MySQL JOIN & UNIONMySQL JOIN & UNION
MySQL JOIN & UNION
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 

Recently uploaded

Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

Database Querying Techniques for Performance and Optimization

  • 1. Database Querying Tech Jam -25 november 2015 Rodger oates
  • 2. Database Querying • STUFF • COALESCE vs ISNULL • The LIKE Predicate • Combining Predicates • Joining Queries • Table Expressions • Window Functions
  • 3. A Quick Note on Query Processing 1. FROM 2. JOINS 3. WHERE 4. GROUP BY 5. CUBE | ROLLUP 6. HAVING 7. SELECT 8. DISTINCT 9. ORDER BY 10. TOP
  • 5. REPLACE • Used to replace parts of a string • Will perform action through whole of original string • Format • REPLACE(<original>, <string to replace>, <replacement>) • Note: <string to replace> and <replacement> do not need to be equal in length
  • 6. STUFF • Used to insert one string into another • Replaces characters in range specified with given string • Format: • STUFF(<original>, <start index>, <length>, <string to stuff>) • Examples of use: • www.sql-server-helper.com/
  • 7. For XML PATH • Can be used with STUFF to translate a table of results into a single result. • Real world example: • Comma delimited list of names • E.g. in names of users assigned to a given site • See: Usage #6 in www.sql-server-helper.com
  • 9. ISNULL • T-SQL specific • Takes two inputs • Format: • ISNULL(<expression 1>, <expression 2>) • Returns <expression 1> if not null, else <expression 2> • Note: Data type of result is same as <expression 1> regardless of which expression is returned
  • 10. COALESCE • Standard SQL • Takes any number of inputs • Format: • COALESCE(<expression 1>, <expression 2>, …, <expression n>) • Returns first input object that is not null • Note: Data type of result is the same as the returned expression
  • 12. Why LIKE? • Used to find strings that are similar to a search term • Without wildcards, predicate would only match with the search term in its entirety, case insensitive • With wildcards, we can find matches where only part of the string matches our term • Search terms that commence with wildcard(s) do not allow SQL to use index ordering to speed up the query • E.g UserFullName LIKE ‘%oates%’
  • 13. Wildcards in LIKE Source: Exam 70-461Training Kit
  • 15. Types of combinations • AND • Predicates on both sides must be true • OR • Either predicate must be true • NOT • Negation of succeeding predicate • Note: when predicate equates to NULL, the NOT of this predicate will still be NULL
  • 16. Natural Precedence Rules for Combinations 1. NOT 2. AND 3. OR e.g. WHERE col1 = ‘w’ AND col2 = ‘x’ OR col3 = ‘y’ AND col4 = ‘z’
  • 17. Use of parentheses • Can use parentheses if different precedence must be given to predicates • Previous example would equate to: • WHERE (col1 = ‘w’AND col2 = ‘x’) OR (col3 = ‘y’ AND col4 = ‘z’) • However, we could use: • WHERE col1 = ‘w’ AND (col2 = ‘x’ OR col3 = ‘y’) AND col4 = ‘z’
  • 19. Joining Queries • Types of Join • Multi-Join Queries • EXISTS • Set Operators • UNION vs UNIONALL • INTERSECT • EXCEPT
  • 20. Types of Join • CROSS JOIN • Cartesian product of two tables • Does not require a link between the tables • INNER JOIN • Requires a predicate between the tables (specified using the ON keyword) • Filters out any rows that do not match the ON predicate • SELF-JOIN • Is simply an INNER JOIN • Joins records in a table with other records in the same table
  • 21. OUTER JOINS • Outer joins preserve at least one side of the join if there is no match • LEFT OUTER JOIN preserves the left table • E.g. <tableA> LEFT OUTER JOIN <table B> preserves <table A> • RIGHT OUTER JOIN preserves the right table • E.g. <tableA> RIGHT OUTER JOIN <table B> preserves <table B> • FULL OUTER JOIN preserves both tables where there is no match • Further information on joins atTechNet
  • 22. A Note on NULLs within JOINs • Note: When determining row inclusion via predicate, a NULL value on either side of the comparison will be discarded • NULL is not a value, it is unknown
  • 23. Multi-Join Queries • Often need to look at multiple tables within the same query • Use joins to link each table together • Joins are evaluated in order of definition • Need to be careful when combining outer joins with others, as the results may not be what you expect
  • 25. Why was a supplier not returned? Source: Exam 70-461Training Kit
  • 26. How to get round this Source: Exam 70-461Training Kit
  • 27. EXISTS • Accepts a subquery as input: • WHERE EXISTS (SELECT … FROM <table>) • Returns true if at least one row returned • EXISTS doesn’t need to return the result of the subquery • Rather, true or false depending on whether rows would have been returned by the subquery • Can be negated: • WHERE NOT EXISTS (SELECT … FROM <table>)
  • 28. Set Operator Guidelines • Complete rows are matched between the input sets • Number of columns in all queries must be the same • Column types for corresponding columns must be compatible • Considers two NULLs to be compatible • This is different to JOINs • Result column names are defined in the first query • Result ordering can only be defined after the last input query
  • 29. UNION vs UNION ALL • UNION unifies the results of two input queries • Both input queries must have • UNION has an implicit DISTINCT property • No duplicates in result • UNION ALL returns all results, including any duplicates
  • 30. UNION / UNION ALL UNION ALL
  • 31. INTERSECT • Only rows that exist in all input queries are returned • Has an implicit DISTINCT operator
  • 32. EXCEPT • Works with two input queries • Returns rows that exist in the first query, but not the second
  • 34. Types of Table Expression 1. DerivedTable 2. CommonTable Expression (CTE) 3. View 4. InlineTable-Valued Function • 1 and 2 are only visible within the scope of the current statement and are not reusable • 3 and 4 have their definitions stored in the database and can be reused
  • 35. Optimisation Notes • Table expressions are not tables, but definitions • They do not hold any data • They interact directly with the underlying tables • They do not have a performance impact in themselves • The above points must be noted when using a table expression within a query operating on a large data set
  • 36. Derived Tables • Closely resemble a subquery • Defined in the FROM clause of the outer query • E.g. I want the two lowest priced products per category Source: Exam 70-461Training Kit
  • 37. Common Table Expressions (CTEs) • Derived tables cannot be nested, if this is required, the statement must define multiple instances of the same query • Higher risk of mistake • This is a CTE version of the previous query Note: we could reuse C as many times as we want, but only within the scope of the current statement Source: Exam 70-461Training Kit
  • 38. A Note on Scoped Tables • If you need to refer to the same data in multiple statements for a given query it would be better to retrieve the data into a table.You have two options: • Create a temporary table • CREATETABLE #<table name> • Remember to use a DROPTABLE #<table name> command • Declare a table variable • DECLARE @<table name>TABLE • This way, you are not having to go back to the raw tables to retrieve the same data
  • 39. Views and Inline Table-Valued Functions • Define queries in the database, which can then be reused • Do not store any data • Performance issues arise from how it is used • Views do not allow input parameters • Functions do allow input parameters
  • 41. Window Functions • Window Aggregate Functions • Window Ranking Functions
  • 42. Window Aggregate Functions • Same as group aggregate functions • SUM,COUNT,AVG, MIN, MAX • Are applied to a window of rows defined by the OVER clause • Do not hide row detail • Can mix detail and aggregated elements in the same query • Without having to define a load of columns in the group section
  • 44. Window Ranking Functions • ROW_NUMBER • Unique number based on ORDER BY clause • RANK • Number based on ORDER BY clause • Assigns same number when ordering values are tied • Numbers may therefore not be sequential
  • 45. Window Ranking Functions • DENSE_RANK • Similar to RANK • Numbers are sequential • NTILE • Arranges rows within a partition of a number of equally sizes tiles • Number of rows per partition is calculated by • <total number of rows> / <partition size> • If this calculation produces a remainder, additional row(s) are assigned to tiles in their order defined by the ORDER BY clause (see example below)
  • 46. Example • Assuming 800 rows of data, and the following query Source: Exam 70-461Training Kit
  • 47. Example custid orderid Val rownum rnk densernk ntile100 12 10782 12.50 1 1 1 1 27 10807 18.40 2 2 2 1 66 10586 23.80 3 3 3 1 76 10767 28.00 4 4 4 1 54 10898 30.00 5 5 5 1 88 10900 33.75 6 6 6 1 48 10883 36.00 7 7 7 1 41 11051 36.00 8 7 7 1 71 10815 40.00 9 9 8 2 38 10674 45.00 10 10 9 2 53 11057 45.00 11 10 9 2 75 10271 48.00 12 12 10 2 Source: Exam 70-461Training Kit
  • 48. PARTITION • Can be applied to window ranking functions • Each partition has its own numbering • E.g. to get the latest child event for each parent
  • 49. Resources • STUFF: www.sql-server-helper.com • JOINS:TechNet • Training: • MicrosoftVirtual Academy • MSDN • Reference tutorial: • http://www.tutorialspoint.com/sql/sql_tutorial.pdf