Sie sind auf Seite 1von 37

https://mva.microsoft.com/en-US/training-courses/querying-with-transactsql-10530?

l=TjT07f87_9804984382
https://mva.microsoft.com/en-US/training-courses/developing-microsoft-sql-server-databases8482?l=QAfn5xXz_5204984382
https://mva.microsoft.com/en-US/training-courses/querying-microsoft-sql-server-2012-databasesjump-start-8241?l=WFbhCtJy_7204984382

https://www.mssqltips.com/sqlservertutorial/2250/graphical-query-plan-tutorial/

https://www.mssqltips.com/sqlservertutorial/3200/sql-server-query-performance-guidelinestutorial/

SQL OVERVIEW

Nilesh Desai
(OASIS)

What is SQL?

It is a Structure Query Language


It is embedded in programming language, or
used in 4GL like VB etc.
Originally developed by System-R Project of
IBM (1974)
Industry standard for relation database
(SQL92 is an ANSI/ISO standard)

Structured Query
Language

Data Definition Language (DDL)


Data Manipulation Language (DML)
Database Control Language (DCL)

SQL : Data Definition


Language

Creating Table
Delete Table
Alter Table etc..

SQL : Data Manipulation


Language

Insert
Delete
Update
Queries
- Simple selection
- Advance Query (Aggregation etc.)
- Nested Query
- Views

SQL : Database Control


Language

Define Access rights


Concurrency Control

Write Scalable SQL

Agenda

What is scalability
Indexing
SQL Tuning Tips

What is Scalability?

Scalability

The potential for a business or an aspect of a business to continue to


function effectively as its size increases.

CPU Scalability:
The application is able to utilize additional processors and increase
business throughput linearly or close thereto .

- Response time Scalability


Response time are consistent when additional users are added to the
system. For e.g. if It take the average of 5 sec. to enter an expense
with 10 user on the system, It should not take more than 5 second to
perform the same transaction with 100 user.

What is Scalability?

Scalability
-

Data Scalability:
Processing 100,000 lines should take at most double the time of
processing 50,000 lines.

Response Time

Scalability
Non Scalable

Scalable

Number of User

Indexing

What is an Index?
Different Type of Indexes
When to user different type of indexes
How do Indexes works?
How to mange Indexes?

What is an Index?

Index Provide fast access to data


Index permit data to be organized for
optional performance
Index are 4KB
Each Index has corresponding row in the
sysindexes table
Index using B- Tree Structure format

Type of Index

Cluster Index
Non-cluster Index

Cluster Index

Each table has only one clustered Index


The data is physically order on the
clustering key
The indid ID is 1 in sysindexes
Each Clustered row must be unique
Primary key constraints creates unique
clustered key

When Clustered index Used?

Primary Key Searched extensively


Queries that return large result set
Column frequently used in queries
Column with strong selectivity
Column used in order by queries
Column used in group by queries
Column used in table joined

Non-Clustered Index

Does not affect Table data order

Leaf node and data pages are separate

Each table can have up to 249 nonclustered indexes


The indid ID in sysindexes are 2-250
Creating of non-clustered index does not
rearrange data

When Non-Clustered Index


Used?

Queries that return small result set


Columns used in aggregate functions
Foreign Keys

How Clustered
Indexes Work
True

Ali

True
Mathew

Select last, first


from member
where last = Ricardo

Ricardo >= Ali


Ricardo >=Mathew

Ali
Page 140

George

Mathew Ricardo >=Mathew


Sam
True

Ricardo >= Sam

..

False

Ali
Brown

George

Mathew

Sam

Hall

Mathew

Sam

Frank

James

Ota

William

Frank

James

Ricardo

William

Page
120

Page

Page

Page

100

110

130

Non-Clustered
Index
Ali
George

select last, first


from member
where last = Ricardo
Ricardo>=
Non
True
Ali
Leaf

Ali

Mathew

Mathew
Sam

Ricardo>=
Mathew
True

Ricardo>=
Sam
False

Ali - 48057

George 48058

Mathew - 48064

Sam - 48065

Brown- 48052

Hall - 48059

Mathew - 48083

Sam - 48063

Frank - 48053

James - 48063

Ota - 48071

William - 48054

Frank - 48084

James - 48072

Ricardo - 48073

William - 48082

Brown - 48052

Ali - 4057

James- 48063

Ota - 48071 William - 48082

Frank- 48053 George- 48058 Mathew- 48064 James - 48072 Mathew- 48083
William - 48054 Hall - 48059

..

Leve
l

Sam - 48065 Ricardo - 48073 Frank - 48084


.

Ricardo, Rodriguez

..

Leaf
Level

Data
Page

What to Index

Large Table
Column that are frequently used in queries
Columns with strong selectivity
Columns used in aggregate functions
Column used in group by queries
Column used in order by queries

What Not to Index

Small Tables
Columns that are rarely used in queries
Columns with poor selectivity
Tables with many modification, but few
actual queries

What can not be Indexed?

Columns with the following data type can


not be indexed
Text
ntext
bit

SQL Tuning

Some Tips to write a SQL & concept to


understand
Microsoft Tuning Technique

SQL Tips 1

WHERE Clauses

Do not used non index column in WHERE clause

Some technique to write SQL (WHERE Clause)

Tips 2 : Avoid Having Clause

Avoid using HAVING clause in SELECT statements. The HAVING


clause filters selected rows only after all rows have been fetched.
Using a where clause help reduced overheads in sorting, summing,
etc. HAVING clause should only be used when column with
summary operations applied to them are restricted by the clause.
Do Not Use
SELECT region, AVG (loc_size) FROM location GROUP BY region
HAVING region != SYDNEY AND region != PERTH

USE
SELECT region, AVG (loc_size) FROM location WHERE
region != SYDNEY AND region != PERTH GROUP BY region

Tips 3

Minimum Table Lockup

Minimum the number of table lockups (Subquery blocks) in


queries, particularly if your statements includes subquery
SELECTS or Multicolumn UPDATE

Tips 4

JOIN Vs EXISTS Vs IN

Consider the alternatives EXISTS,IN and table JOINS when doing


multiple table joins. None of these are consistently faster; it
depends on your data
How to choose between EXISTS and IN for sub-Queries

Use an EXISTS when u want drive from the outer-query and you
want to perform a simple existences check based on the outer-rows.
Use an IN when you want to drive from the sub-queries.

Tips 5 : Avoid DISTINCT

Avoid joins that required the DISTINCT qualifier in the SELECT list
in queries which are used to determine information at the owner
end of a one to many relationship. The DISTINCT operator
cause to fetch all rows satisfying the table join and the sort and
filter out duplicate values. EXISTS is faster alternative because
the optimizer realizes when the subquery has been satisfied once,
there is no need to proceed further and the next matching raw can
be fetched.

Do Not Use

SELECT DISTINCT J.job_id, job_desc FROM jobs J, employee e WHERE


J.job_id = E.job_id ORDER BY J.job_id
USE

SELECT job_id,job_desc FROM jobs WHERE EXISTS (SELECT job_id


FROM employee WHERE job_id = jobs.JOB_ID)

Tips 6

Union All Vs Union

Consider whether a UNION ALL will suffice in place of a UNION. The


UNION force all rows returned to be sort and merged and duplicate to
be filtered before the first row returned. A UNION ALL simply returned all
rows including duplicates and dose not have any sort, merged or filter. If
your table are mutually exclusive (include no duplicate records), if u
dont care if duplicate are returned the UNION ALL is much more
efficient.

OR Operator

Use UNION operator instead of OR Operator.

Do Not USE

SELECT * FROM employee WHERE (job_id = 5) OR (job_id=6)

USE

SELECT * FROM employee WHERE job_id = 5


UNION
SELECT * FROM employee WHERE job_id = 6

General Performance Tips

Use Views and Stored Procedures instated of heavy-duty queries.


Try to Avoid using SQL Server Cursor, Whenever possible.
Try to use CONSTRAINTS instead of TRIGGERS, Whenever possible
Use TABLE variables instead of TEMP tables.
Include SET NOCOUNT ON statement in all Stored Procedure.
If you need to return total tables row count, you can use
alternative way instead of SELECT COUNT(*) statement
Used As
SELECT rows FROM sysindexes WHERE id =
OBJECT_ID(table_Name) and indid < 2

The specific topics that will be covered in this tip are as follows:
Query writing:

How Join Order Can Affect the Query Plan

Remove Function Calls From the SELECT List

Avoid Using <> in WHERE Clause

Avoid Using Functions in WHERE Clause

Avoid Using Wildcard Characters to Start Search Criteria

Use a Derived Table in Place of IN Predicate With Aggregate Functions


Indexing:

Make Sure All JOIN Columns are Indexed

Use WHERE, JOIN, ORDER BY, SELECT Column Order When Creating Indexes

Make Sure All Tables Have a Clustered Index Defined


Schema design:

Use DELETE CASCADE Option to Handle Child Key Removal in Foreign Key Relationships

Denormalize For Performance

The order in which the tables in your queries are joined can have a dramatic effect on
how the query performs. If your query happens to join all the large tables first and
then joins to a smaller table later this can cause a lot of unnecessary processing by
the SQL engine.
Using functions as a part of any type of programming is usually a good practice as it
generally makes code more readable and allows you to use to use it over and over
again. This is also true for SQL queries except for the fact that there are cases when
running the same statement over and over again might not be the most efficient way
to get your result.
In almost all cases when we use the <> operator (or any other operator in conjunction
with the NOT operator, i.e.. NOT IN) index seeks will not be performed and instead a
table/index scan is required.
it's always best to use an equality operator, like = or IN, in you query if you want the
best performance possible.
.

When using the LIKE operator and having the first


character in your search string a wildcard character,
either % or _, the SQL Optimizer will be forced to do a
table/index scan when executing your query.
Using a derived table in place of the IN predicate when
we are aggregating data allows us to only have to
process certain table records once therefore reducing
the amount of resources required to execute a query.

SELECT MIN(IntDataColumn) FROM [dbo].[Parent]


WHERE ParentID IN (SELECT TOP 2 ParentID FROM
[dbo].[Parent] ORDER BY IntDataColumn DESC)
Rewite this as :

SELECT MIN(IntDataColumn) FROM (SELECT TOP 2


IntDataColumn FROM [dbo].[Parent] ORDER BY
IntDataColumn DESC) AS A

Das könnte Ihnen auch gefallen