Tag Archives: SQL Server 2008 R2

Get Top Queries By Average CPU Time SQL Server 2008 R2

Database performance plays significant effect in large user application. We need to examine and identify causes that makes system slow.

One step we can do is to identify query which takes high CPU time. Use transact SQL sys.dm_exec_sql_text in order to get top CPU expensive queries.

Taken from Microsoft site, https://msdn.microsoft.com/en-us/library/ms181929(v=sql.105).aspx, the code using sys.dm_exec_sql_text is below:

SELECT TOP 5 total_worker_time/execution_count AS [Avg CPU Time],
    SUBSTRING(st.text, (qs.statement_start_offset/2)+1, 
        ((CASE qs.statement_end_offset
          WHEN -1 THEN DATALENGTH(st.text)
         ELSE qs.statement_end_offset
         END - qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY total_worker_time/execution_count DESC;

Above T-SQL gets Top five query by Average CPU Time. Open your SQL Server Management Studio, choose your database and run above query.

After getting those Top queries, you can open SQL Server Database Engine Tuning Advisor to analyze them and apply the Tuning Advisor recommendation to get better performance.
You can take a look at my post about How to use Database Engine Tuning Advisor for your reference.

Agung Gugiaji

Basic How To – Using SQL Server Database Engine Tuning Advisor

The function of SQL Server Database Engine Tuning Advisor in MS Web is “examines how queries are processed in the databases you specify, and then recommends how you can improve query processing performance by modifying database structures such as indexes, indexed views, and partitioning”
Refer To: https://msdn.microsoft.com/en-us/library/ms166575.aspx
Query needs to be tuned in order to increase performance.

This post will showing how to basic use Database Engine Tuning Advisor. I use SQL Server 2008 R2 AdventureWorks sample Database.

  1. First create your prefered query. For example
    select row_number() over(order by Sales.Store.Name) as rowno, Sales.Store.Name as StoreName, Sales.SalesTerritory.Name as TerritoryName, sum(Sales.SalesOrderHeader.TotalDue) as SalesValue 
    from Sales.Customer
    inner join Sales.Store on Sales.Customer.StoreID = Sales.Store.BusinessEntityID
    inner join Sales.SalesTerritory on Sales.SalesTerritory.TerritoryID = Sales.Customer.TerritoryID
    inner join Sales.SalesOrderHeader on Sales.SalesOrderHeader.CustomerID = Sales.Customer.CustomerID
    where Sales.Customer.StoreID is not null
    group by Sales.Store.Name,Sales.SalesTerritory.Name order by Sales.Store.Name

    Save To your folder and name it StoreSales.sql

  2. Continue reading

How To Do SQL Server 2008 R2 Replication To Distribute Your Data Or Make Mirror

Replication means making exact copy or synchronize from source database to target with interval of time. So if source data get inserted then for short time target data will get inserted with same data from source.
Same as delete operation, if some records in source data deleted then same data in target table will deleted.

From technical point of view, Replication and Mirroring are different. Replication is synchronization process between databases and intended to be used by day to day application. Whereas mirroring is intended for high availability and data protection of database. So if live database corrupted then mirror database will be used automatically.
However from non technical point of view, they as if similar. Both source & target (secondary) server has same data but replication can have sql query in order to replicate for just specific data and/or combined data.

For some reasons, we want to have a replica for our database. In example, you have two servers with almost or same specification, one server is serving OLTP and other intended to reporting server.
The OLTP server will supply or export its data to Reporting Server, so that overall application load will be devided.

This export data from OLTP to Reporting can be done automatically using SQL Server 2008 R2 Replication. Users do transactional data from their application, data saved to OLTP database.
After that replication do insert or update or delete according source data in short time interval to target which is Reporting database. This makes exact state of data between source & target.

Another example is distribute central data to branches or vice versa. Let’s say you have central data that inputted by head office staffs.
You want some of the data to be distribute to branch servers so that branch users can view central data from their own server.
This also can be done using replication.

SQL Server Replication explanation

Copying some definitions from MS technet site => http://technet.microsoft.com/en-us/library/ms152567.aspx
Replication uses a publishing industry i.e magazine and/or newspaper metaphor to represent the components in a replication topology, which include Publisher, Distributor, Subscribers, publications, articles, and subscriptions.

The Publisher is a database instance that makes data available to other locations through replication. The Publisher can have one or more publications, each defining a logically related set of objects and data to replicate.

The Distributor is a database instance that acts as a store for replication specific data associated with one or more Publishers. Each Publisher is associated with a single database (known as a distribution database) at the Distributor. The distribution database stores replication status data, metadata about the publication, and, in some cases, acts as a queue for data moving from the Publisher to the Subscribers. In many cases, a single database server instance acts as both the Publisher and the Distributor. This is known as a local Distributor. When the Publisher and the Distributor are configured on separate database server instances, the Distributor is known as a remote Distributor.

A Subscriber is a database instance that receives replicated data. A Subscriber can receive data from multiple Publishers and publications. Depending on the type of replication chosen, the Subscriber can also pass data changes back to the Publisher or republish the data to other Subscribers.

An article identifies a database object that is included in a publication. A publication can contain different types of articles, including tables, views, stored procedures, and other objects. When tables are published as articles, filters can be used to restrict the columns and rows of the data sent to Subscribers.

A publication is a collection of one or more articles from one database. The grouping of multiple articles into a publication makes it easier to specify a logically related set of database objects and data that are replicated as a unit.

A subscription is a request for a copy of a publication to be delivered to a Subscriber. The subscription defines what publication will be received, where, and when. There are two types of subscriptions: push and pull.
Continue reading