Search This Blog

Friday, February 25, 2011

Find Longest Running Query

DBCC FREEPROCCACHE

Run following query to find longest running query using T-SQL.


SELECT DISTINCT TOP 10 t.TEXT
                       queryname
                       ,
                       s.execution_count
                       AS executioncount,
                       s.max_elapsed_time
                       AS maxelapsedtime,
                       Isnull(s.total_elapsed_time / s.execution_count, 0)
                       AS avgelapsedtime,
                       s.creation_time
                       AS logcreatedon,
                       Isnull(s.execution_count / Datediff(s, s.creation_time,
                                                  Getdate()), 0) AS
                       frequencypersec
FROM   sys.dm_exec_query_stats s
       CROSS APPLY sys.Dm_exec_sql_text(s.sql_handle) t
ORDER  BY s.max_elapsed_time DESC 

Find Most Expensive Queries Using DMV

SELECT TOP 10 Substring(qt.TEXT, ( qs.statement_start_offset / 2 ) + 1, (
                            ( CASE qs.statement_end_offset
                            WHEN -1 THEN Datalength(qt.TEXT)
                            ELSE qs.statement_end_offset
                                                                            END
                              -
              qs.statement_start_offset ) / 2 ) + 1),
              qs.execution_count,
              qs.total_logical_reads,
              qs.last_logical_reads,
              qs.total_logical_writes,
              qs.last_logical_writes,
              qs.total_worker_time,
              qs.last_worker_time,
              qs.total_elapsed_time / 1000000 total_elapsed_time_in_s,
              qs.last_elapsed_time / 1000000  last_elapsed_time_in_s,
              qs.last_execution_time,
              qp.query_plan
FROM   sys.dm_exec_query_stats qs
       CROSS APPLY sys.Dm_exec_sql_text(qs.sql_handle) qt
       CROSS APPLY sys.Dm_exec_query_plan(qs.plan_handle) qp
ORDER  BY qs.total_logical_reads DESC 

Wednesday, February 16, 2011

Convert exponential Number to non-exponential numbers in SQL Database

SELECT


or

SELECT
convert(varchar(10),5.0000000000000003E-2) 
convert(varchar(10),cast(5.0000000000000003E-2 AS float))

SQL Wildcards

ORDER BY ColumnName vs ORDER BY ColumnNumber

Case 1: When Not Using * and Columns are Re-ordered

-- ColumnName (Recommended)
SELECT name, DeptFROM testORDER BY name, DeptGO-- ColumnNumber (Strongly Not Recommended)SELECT name, DeptFROM name, DeptORDER BY 2,1GO

Case 2: When someone changes the schema of the table affecting column order

I will let you recreate the example for the same. If your development server where your schema is different than the production server, if you use ColumnNumber, you will get different results on the production server.
Summary: When you develop the query it may not be issue but as time passes by and new columns are added to the SELECT statement or original table is re-ordered if you have used ColumnNumber it may possible that your query will start giving you unexpected results and incorrect ORDER BY.
One should note that the usage of ORDER BY ColumnName vs ORDER BY ColumnNumber should not be done based on performance but usability and scalability.
It is always recommended to use proper ORDER BY clause with ColumnName to avoid any confusion

What are the difference between DDL, DML and DCL commands

DDL - Data Definition Language: statements used to define the database structure or schema. Some examples:
  • CREATE - to create objects in the database
  • ALTER - alters the structure of the database
  • DROP - delete objects from the database
  • TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
  • COMMENT - add comments to the data dictionary
  • RENAME - rename an object
DML - Data Manipulation Language: statements used for managing data within schema objects. Some examples:
  • SELECT - retrieve data from the a database
  • INSERT - insert data into a table
  • UPDATE - updates existing data within a table
  • DELETE - deletes all records from a table, the space for the records remain
  • MERGE - UPSERT operation (insert or update)
  • CALL - call a PL/SQL or Java subprogram
  • EXPLAIN PLAN - explain access path to the data
  • LOCK TABLE - controls concurrency
DCL - Data Control Language. Some examples:
  • GRANT - gives user's access privileges to database
  • REVOKE - withdraw access privileges given with the GRANT command
TCL - Transaction Control: statements used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.
  • COMMIT - save work done
  • SAVEPOINT - identify a point in a transaction to which you can later roll back
  • ROLLBACK - restore database to original since the last COMMIT
  • SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use
DML are not auto-commit. i.e. you can roll-back the operations, but DDL are auto-commit

Index Seek Vs Index Scan

Index Scan:
Since a scan touches every row in the table whether or not it qualifies, the cost is proportional to the total number of rows in the table. Thus, a scan is an efficient strategy if the table is small or if most of the rows qualify for the predicate.
Index Seek:
Since a seek only touches rows that qualify and pages that contain these qualifying rows, the cost is proportional to the number of qualifying rows and pages rather than to the total number of rows in the table.
Index Scan is nothing but scanning on the data pages from the first page to the last page. If there is an index on a table, and if the query is touching a larger amount of data, which means the query is retrieving more than 50 percent or 90 percent of the data, and then optimizer would just scan all the data pages to retrieve the data rows. If there is no index, then you might see a Table Scan (Index Scan) in the execution plan.
Index seeks are generally preferred for the highly selective queries. What that means is that the query is just requesting a fewer number of rows or just retrieving the other 10 (some documents says 15 percent) of the rows of the table.
In general query optimizer tries to use an Index Seek which means that optimizer has found a useful index to retrieve recordset. But if it is not able to do so either because there is no index or no useful indexes on the table then SQL Server has to scan all the records that satisfy query condition.

Operator

Input --------> Operater-------------->Output
Blocking Operators (aggregate ,short) -- this will degrade our query performance

Non Blocking Operators

Reading Query Plans


Query Plan Option

1. Graphical Plans 
    
  Displayed estimated execution Plan   (ctrl+L)
  Include Actual execution Plan   (ctrl+M)
 
2. Text Plans

  SET SHOWPLAN_TEXT ON
  go
  go  SET SHOWPLAN_TEXT OFF


3. XML Plans

 SET SHOWPLAN_XML ON
  go
   Select * from tablename
  go
  SET SHOWPLAN_XML OFF

In order to view complete statistics


SET STATISTICS PROFILE ON

 In order to Execution Times

SET STATISTICS TIME ON




Query Optimizer
Query Execution Engin
Select * from tablenameSET STATISTICS PROFILE OFF
   Select * from tablename

Factors that impact performance

1. Appilication Architecture
2. Appilication Design
3. Transaction and Isolation Level
4. Transact-SQL Code
5. Hardware Resource
6. SQL Server Configuration
       6.1 Affinity Mask
       6.2 Lightweight Pooling
       6.3 Max work Threads
       6.4 Max Degree of Parallelism
       6.5 Min and Max Server Memory
7. Database Schema
8. Mixing log and data onto the samephysical disk
9. Sharing physical disk(s) amoung tempdb and user databases
10. Mapping heavily acessed tables onto the same physical disk

Rollup and Cube in sql

ROLLUP Extension to GROUP BY

ROLLUP enables a SELECT statement to calculate multiple levels of subtotals across a specified group of dimensions. It also calculates a grand total. ROLLUP is a simple extension to the GROUP BY clause, so its syntax is extremely easy to use. The ROLLUP extension is highly efficient, adding minimal overhead to a query.
The action of ROLLUP is straightforward: it creates subtotals that roll up from the most detailed level to a grand total, following a grouping list specified in the ROLLUP clause. ROLLUP takes as its argument an ordered list of grouping columns. First, it calculates the standard aggregate values specified in the GROUP BY clause. Then, it creates progressively higher-level subtotals, moving from right to left through the list of grouping columns. Finally, it creates a grand total.
ROLLUP creates subtotals at n+1 levels, where n is the number of grouping columns. For instance, if a query specifies ROLLUP on grouping columns of time, region, and department(n=3), the result set will include rows at four aggregation levels.
You might want to compress your data when using ROLLUP. This is particularly useful when there are few updates to older partitions.

When to Use ROLLUP

Use the ROLLUP extension in tasks involving subtotals.
  • It is very helpful for subtotaling along a hierarchical dimension such as time or geography. For instance, a query could specify a ROLLUP(y, m, day) or ROLLUP(country, state, city).
  • For data warehouse administrators using summary tables, ROLLUP can simplify and speed up the maintenance of summary tables.

ROLLUP Syntax

ROLLUP appears in the GROUP BY clause in a SELECT statement. Its form is:
SELECT … GROUP BY ROLLUP(grouping_column_reference_list)


CUBE Extension to GROUP BY

CUBE takes a specified set of grouping columns and creates subtotals for all of their possible combinations. In terms of multidimensional analysis, CUBE generates all the subtotals that could be calculated for a data cube with the specified dimensions. If you have specified CUBE(time, region, department), the result set will include all the values that would be included in an equivalent ROLLUP statement plus additional combinations. For instance, in Figure 20-1, the departmental totals across regions (279,000 and 319,000) would not be calculated by a ROLLUP(time, region, department) clause, but they would be calculated by a CUBE(time, region, department) clause. If n columns are specified for a CUBE, there will be 2 to the n combinations of subtotals returned. Example 20-4 gives an example of a three-dimension cube. See Oracle Database SQL Reference for syntax and restrictions.

When to Use CUBE

Consider Using CUBE in any situation requiring cross-tabular reports. The data needed for cross-tabular reports can be generated with a single SELECT using CUBE. Like ROLLUP, CUBE can be helpful in generating summary tables. Note that population of summary tables is even faster if the CUBE query executes in parallel.
CUBE is typically most suitable in queries that use columns from multiple dimensions rather than columns representing different levels of a single dimension. For instance, a commonly requested cross-tabulation might need subtotals for all the combinations of month, state, and product. These are three independent dimensions, and analysis of all possible subtotal combinations is commonplace. In contrast, a cross-tabulation showing all possible combinations of year, month, and day would have several values of limited interest, because there is a natural hierarchy in the time dimension. Subtotals such as profit by day of month summed across year would be unnecessary in most analyses. Relatively few users need to ask "What were the total sales for the 16th of each month across the year?" See "Hierarchy Handling in ROLLUP and CUBE" for an example of handling rollup calculations efficiently.

CUBE Syntax

CUBE appears in the GROUP BY clause in a SELECT statement. Its form is:
SELECT …  GROUP BY CUBE (grouping_column_reference_list)

Row Number On Each Row of a Query Result Set with out using ranking function

Select Rowno=(Select Count(*) From test Where Col1<=t1.Col1 And Col2 <=t1.Col2)Col1, Col2, Col3, Col4  From test t1 Order By Col1, Col2
Ranking functions are not avalable in SQL 2000 that time we can use this code

Performance characteristics of Nested Loop, Merge Join and Hash Join

Nested loop, merge join and hash join is common operators which you will find out in execution plan as long as your query contains any logical joins (inner join, outer join, cross join, semi join). These 3 brothers are classified into physical joins. For performance perspective, they fit in some conditions. I would tell you characteristics for each of them.

Nested loop join is best to handle small input sets with index on inner input set specified by join key. It is suitable for OLTP where there are a large number of concurrent users. It supports equijoin though not required, and either left outer join and left (anti) semi join. If you have dynamic cursor, make sure nested loop is the only brother that is in. you can rely ordering issue if you want it gets ordered based on outer input.

Merge join is best to handle medium to large input sets. A better query plan will come up with index one- to-many merge join so that it would be able to support large number of concurrent users. Notice that at least one equijoin required in join predicate, so that optimizer will prefer merge join. Planning carefully to your tempdb database capacity is recommended if you found many-to-many merge join. Compared to join spectrum at nested loop, merge join supports all logical join. Because no inner and outer hierarchy like nested loop, merge join can preserve order of either of input sets.

Last brother of physical join is hash join. The join is best to large input sets. This typical characteristics will be found in datawarehouse environment with parallel query execution, so it supports limited number of concurrent users. Hash join will hold its output until all rows of first input set are completely build into hash table, and remember that hash join will be in-memory storage so it takes memory. If memory is not sufficient, it will spill out to tempdb as temporary repository of build table, in worst case it turns out to bailout. And like merge join, it supports all logical joins and at least one equijoin required.


Eamples:

select * from test1 a inner merge join test4 b on a.id=b.iselect * from test1 a inner hash join test4 b on a.id=b.iselect


see the execution planfor above query
* from test1 a inner loop join test4 b on a.id=b.i

Tuesday, February 15, 2011

CROSS APPLY and OUTER APPLY

Types of Joins

INNER JOIN

This join returns rows when there is at least one match in both the tables.

OUTER JOIN


There are three different Outer Join methods.
LEFT OUTER JOIN
This join returns all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.
RIGHT OUTER JOIN
This join returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.
FULL OUTER JOIN
This join combines left outer join and right after join. It returns row from either table when the conditions are met and returns null value when there is no match.

CROSS JOIN


This join is a Cartesian join that does not necessitate any condition to join. The resultset contains records that are multiplication of record number from both the tables.

Additional Notes related to JOIN:

The following are three classic examples to display where Outer Join is useful. You will notice several instances where developers write query as given below.
SELECT t1.*FROM Table1 t1WHERE t1.ID NOT IN (SELECT t2.ID FROM Table2 t2)GO
The query demonstrated above can be easily replaced by Outer Join. Indeed, replacing it by Outer Join is the best practice. The query that gives same result as above is displayed here using Outer Join and WHERE clause in join.
/* LEFT JOIN - WHERE NULL */SELECT t1.*,t2.*FROM Table1 t1LEFT JOIN Table2 t2 ON t1.ID = t2.IDWHERE t2.ID IS NULL
The above example can also be created using Right Outer Join.
NOT INNER JOIN
Remember, the term Not Inner Join does not exist in database terminology. However, when full Outer Join is used along with WHERE condition, as explained in the above two examples, it will give you exclusive result to Inner Join. This join will give all the results that were not present in Inner Join.
You can download the complete SQL Script here, but for the sake of complicity I am including the same script here.
USE AdventureWorks
GO
CREATE TABLE table1(ID INT, Value VARCHAR(10))INSERT INTO Table1 (ID, Value)SELECT 1,'First'UNION ALLSELECT 2,'Second'UNION ALLSELECT 3,'Third'UNION ALLSELECT 4,'Fourth'UNION ALLSELECT 5,'Fifth'GOCREATE TABLE table2(ID INT, Value VARCHAR(10))INSERT INTO Table2 (ID, Value)SELECT 1,'First'UNION ALLSELECT 2,'Second'UNION ALLSELECT 3,'Third'UNION ALLSELECT 6,'Sixth'UNION ALLSELECT 7,'Seventh'UNION ALLSELECT 8,'Eighth'GOSELECT *FROM Table1SELECT *FROM Table2
GO
USE AdventureWorks
GO
/* INNER JOIN */SELECT t1.*,t2.*FROM Table1 t1INNER JOIN Table2 t2 ON t1.ID = t2.ID
GO
/* LEFT JOIN */SELECT t1.*,t2.*FROM Table1 t1LEFT JOIN Table2 t2 ON t1.ID = t2.ID
GO
/* RIGHT JOIN */SELECT t1.*,t2.*FROM Table1 t1RIGHT JOIN Table2 t2 ON t1.ID = t2.ID
GO
/* OUTER JOIN */SELECT t1.*,t2.*FROM Table1 t1FULL OUTER JOIN Table2 t2 ON t1.ID = t2.ID
GO
/* LEFT JOIN - WHERE NULL */SELECT t1.*,t2.*FROM Table1 t1LEFT JOIN Table2 t2 ON t1.ID = t2.IDWHERE t2.ID IS NULLGO/* RIGHT JOIN - WHERE NULL */SELECT t1.*,t2.*FROM Table1 t1RIGHT JOIN Table2 t2 ON t1.ID = t2.IDWHERE t1.ID IS NULLGO/* OUTER JOIN - WHERE NULL */SELECT t1.*,t2.*FROM Table1 t1FULL OUTER JOIN Table2 t2 ON t1.ID = t2.IDWHERE t1.ID IS NULL OR t2.ID IS NULLGO/* CROSS JOIN */SELECT t1.*,t2.*FROM Table1 t1CROSS JOIN Table2 t2
GO
DROP TABLE table1DROP TABLE table2

Tuesday, February 8, 2011

what are the ypes of locking available in SQL



1)Shared locks are used for operations that does not allow to change or update data, such as a SELECT statement.
(2)Update locks are used when SQL Server intends to modify a page, and later promotes the update page lock to an exclusive page lock before actually making the changes.
(3)Exclusive locks are used for the data modification operations, such as UPDATE, INSERT, or DELETE


SHARED - This lock is applied for read operation where the data is not updated. A good example would be the select statement.
UPDATE – This locked on those resources that can be updated. This lock prevents the common form of dead lock that occurs when multiple sessions are locking the data so that they can update it later.

EXCLUSIVE - Used for data-modification operations, such as INSERT, UPDATE, or DELETE. Ensures that multiple updates cannot be made to the same resource at the same time.
INTENT - Used to establish a lock hierarchy. The different types of intent locks are: intent shared, intent exclusive, and shared with intent exclusive.

SCHEMA - Used when an operation dependent on the schema of a table is executing. The different types of schema locks are: schema modification and schema stability.
BULK UPDATE – This lock is applied when there is a bulk copying of data and the TABLOCK is applied
KEY RANGE - Protects the range of rows read by a query when using the serializable transaction isolation level. Ensures that other transactions cannot insert rows that would qualify for the queries of the serializable transaction if the queries were run again

How to find Out Table Properties using OBJECTPROPERTY

DECLARE
SELECT
 select OBJECTPROPERTY(@objectID,'TableDeleteTrigger') AS 'Table Has A Delete Trigger',OBJECTPROPERTY(@objectID,'TableHasClustIndex') AS 'Table Has A Clustered Index',OBJECTPROPERTY(@objectID,'TableHasIdentity') AS 'Table Has An Identity Column',OBJECTPROPERTY(@objectID,'TableHasPrimaryKey') AS 'Table Has A Primary Key',OBJECTPROPERTY(@objectID,'TableHasTimestamp') AS 'Table Has A Timestamp',OBJECTPROPERTY(@objectID,'TableHasUniqueCnst') AS 'Table Has A Unique Constraint',OBJECTPROPERTY(@objectID,'IsAnsiNullsOn') AS 'ANSI NULLS Is Set To ON'
@objectID INT @objectID =OBJECT_ID('tablename')

How to return random results from one table

SELECT  top 10  * FROM test ORDER BY NEWID()

Sunday, February 6, 2011

Spilt comma separated values into separeted rows



Actutal table ::
   


Step_Configuration_Idstep_modes
1,7202,
2,7202,7204,7201,7203,7200,
3,7202,
5,7201,7203,7200,
6,7202,7201,7203,7200,



Excepted output::


Step_Configuration_Idstep_nodes
17202
27204
27200
27202
27201
27203
37202
57201
57200
57203
67202
67200
67201
67203


Solution:

select
distinct Step_Configuration_Id,i.value('.', 'VARCHAR(20)') AS step_modes
from
(select Step_Configuration_Id,step_modes ,CAST('<i>'+ replace(step_modes,',','</i><i>')
+
'</i>' AS XML) AS step_modesXml from Step_Configurations
)
a CROSS APPLY step_modesXml.nodes('i') x(i) where i.value('.', 'VARCHAR(20)') <> ''
order by 1