Professional Documents
Culture Documents
Agenda
Select Statement
Sql Where Clause
Insert Into Statement
Update Statement
Sql Join
Analyzing Sql Profiler Output
Missing Index Analysis
Worst Query Analysis
Execution Plan Analysis
Select Query
The WHERE clause is used to extract only those records that fulfill a
specified criterion.
Example:
SELECT * FROM Customers
WHERE Country='Mexico';
The first form does not specify the column names where the data will be inserted,
only their values:
The second form specifies both the column names and the values to be inserted:
Example:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country) VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
Example:
UPDATE Customers SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
SQL JOIN
An SQL JOIN clause is used to combine rows from two or more tables,
based on a common field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL
INNER JOIN return all rows from multiple tables where the join condition
is met.
The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns in both tables.
INNER JOIN is the same as JOIN.
Syntax:
The LEFT JOIN keyword returns all rows from the left table (table1), with the
matching rows in the right table (table2). The result is NULL in the right side
when there is no match.
SQL LEFT JOIN Syntax:
The RIGHT JOIN keyword returns all rows from the right table (table2),
with the matching rows in the left table (table1). The result is NULL in the
left side when there is no match.
The FULL OUTER JOIN keyword returns all rows from the left table (table1)
and from the right table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT
joins.
Microsoft SQL Server Profiler is a graphical user interface to SQL Trace for
monitoring T-SQL Statements of Database Engine. We can save and reuse
the state at a later point of time.
Cont...
Step 1
The following screen will come:
This is basically a login screen of sql server profiler where we have to
enter credentials to login
Step 2:
After Login this window will apper,this is basically trace properties window.
Here we will select various event aacording to our need by simply checking the
checkboxes.
Generally we check only following events:
RPC Completed
Sql Batch Completed
Sql Batch Starting
Step 3
After clicking on column filter new window will apperThis Section is something like
customization. Just click on the "Column Filter Button". In this section, you can
specify some condition (like or Not like).Here genrally we enter database id for
better tracking ,which is taken from sys.databases table .
Step 4:
Now Click on the "Run" Button, then Trace window will come:
Using these windows, you will get the detailed time duration of a query and all other
events information that you have selected.
You can save this result and use it in future. Or you can extract a particular query
from the trace, just right click and click on "Extract Event Data". And save this as a
SQL Script.
Important Points:
Profiler is basically used to analysis performance
issue or production issue.
In case of performance issue we can identify query
by identify query having higher duration & by start
time or end time of individual query.
In case of production issue where there is improper
function,we can identify query by searching query by
particular column name or its value.
After this we have to paste query in query editor &
check its execution plan & analyze query & try to find
out ,is there any scope of improvement.
Index Syntax
SQL CREATE INDEX Syntax:
CREATE INDEX index_name ON table_name (column_name)
Best Practices:
1.This query will provide top 20 worst queries along with their execution,max
worker time,total worker time an execution plan.
2.While selecting victim query we have to consider various factor like
execution,max worker time,total worker time.
3.After analyzing the query we have to copy the query in query editor,format
it,execute it and check its execution plan.
4.Check weather is there any missing index,any implicit casting and take action
accordingly.
5.If not then we verify is there is any scope of query re-factoring.
First, you click the 'New Query' button on the top left ofSQL Server
Management Studioto have a new query window to execute a query.
Second, you click the 'Execute' icon on the toolbar or press the F5 key to
process a query.
Third, SQL Server does some processing behind the scenes.
Fourth, results are returned. These are the four steps to retrieving
information from Microsoft SQL Server.
Three of which are pretty straightforward and happen in the light of day.
You see the commands, parameters, object names, keywords and the like
being laid down on the display in front of you when entering T-SQL
commands. You are the one clicking the Execute or F5 key.
You see the results as they are returned. Is there not a way to see how SQL
Server goes about taking the gibberish of T-SQL and converting it to
understandable rows of data? Of course there is. It's the graphical
execution plan!
Solution:
There are actually two other flavors of execution plans that we will not be
discussing at this time: text execution plan and an xml execution plan.
There are two types of graphical execution plans: the estimated execution
plan and the actual execution plan. They are precisely what the names
imply (yes, a rarity in technology).