Friday, May 6, 2011

Minimally logged/Non-logged operation and truncate table

Nonlogged operations offer much better performance than logged operations.

The following DML operations are either nonlogged or minimally logged:

TRUNCATE TABLE.

SELECT INTO.


The TRUNCATE TABLE statement cannot be used on tables that are referenced by a FOREIGN KEY constraint, unless the FOREIGN KEY constraint is self-referencing. The TRUNCATE TABLE statement cannot be used on a table that is a part of an indexed view. You cannot use the TRUNCATE TABLE statement on tables that are published by transactional or merge replication. The TRUNCATE TABLE statement will not activate triggers, as triggers rely on transaction logs

The TRUNCATE TABLE statement uses much less transaction log space. The DELETE statement logs information about every row that is affected in the transaction log. When deleting from a table with millions of rows, this is both time- and disk-space-consuming.

The DELETE statement holds a lock on each individual row it is deleting. The TRUNCATE TABLE statement locks only the table and each data page. This offers better performance, as locking is one of the most time-consuming SQL Server activities.

The DELETE statement will leave empty data pages for the table and its indexes. If you wish to shrink the database by deleting data in a large table, the DELETE statement may prove counterproductive. The TRUNCATE TABLE statement, on the other hand, is guaranteed to leave zero data pages behind.

Tuesday, April 5, 2011

Void sales

--check/delete void sales
;with s1
as
(
--reverse transactions
select *,
row_number() over(partition by recordtype, TxnDateTime, TransactionIdentifier, ExternalStoreIdentifier, TillNumber, ProductItemIdentifier, ItemQuantity, SaleAmount order by recordtype, TxnDateTime, TransactionIdentifier, ExternalStoreIdentifier, TillNumber, ProductItemIdentifier, ItemQuantity, SaleAmount) as rn
from @sales
where (recordtype = 'S' and SaleAmount < 0 and ItemQuantity < 0) or (recordtype = 'R' and SaleAmount > 0 and ItemQuantity > 0)
),
s2 as
(
--regular transactions
select *,
row_number() over(partition by recordtype, TxnDateTime, TransactionIdentifier, ExternalStoreIdentifier, TillNumber, ProductItemIdentifier, ItemQuantity, SaleAmount order by recordtype, TxnDateTime, TransactionIdentifier, ExternalStoreIdentifier, TillNumber, ProductItemIdentifier, ItemQuantity, SaleAmount) as rn
from @sales
where (recordtype = 'S' and SaleAmount > 0 and ItemQuantity > 0) or (recordtype = 'R' and SaleAmount < 0 and ItemQuantity < 0)
),
s3 as
(
select s1.srn from s1 inner join s2 on s1.recordtype = s2.recordtype and s1.TxnDateTime = s2.TxnDateTime and s1.TransactionIdentifier = s2.TransactionIdentifier and s1.ExternalStoreIdentifier = s2.ExternalStoreIdentifier and s1.TillNumber = s2.TillNumber and s1.ProductItemIdentifier = s2.ProductItemIdentifier
where s1.rn = s2.rn and s1.SaleAmount = -s2.SaleAmount and s1.ItemQuantity = -s2.ItemQuantity
union
select s2.srn from s1 inner join s2 on s1.recordtype = s2.recordtype and s1.TxnDateTime = s2.TxnDateTime and s1.TransactionIdentifier = s2.TransactionIdentifier and s1.ExternalStoreIdentifier = s2.ExternalStoreIdentifier and s1.TillNumber = s2.TillNumber and s1.ProductItemIdentifier = s2.ProductItemIdentifier
where s1.rn = s2.rn and s1.SaleAmount = -s2.SaleAmount and s1.ItemQuantity = -s2.ItemQuantity
)
delete s from @sales s inner join s3 on s.srn = s3.srn

Thursday, December 2, 2010

Inserting into a table with single column and identity

How can you insert a row in a table with the following definition:

CREATE TABLE T (A INT IDENTITY (1,1))

Hmmmm..it is ridiculously simple as:

INSERT T DEFAULT VALUES

Sunday, September 19, 2010

Qs on locking/isolation

http://www.mssqltips.com/tip.asp?tip=1253

Table variable indexes

Two types of indexes may be specified on a table variable - primary key (clustered) and unique. However, they need to be included in the table variable definition.

DECLARE @test TABLE
(
a INT PRIMARY KEY,
b varchar(50),
UNIQUE (b)
)


Generally, temp tables perform better in situations where an index is needed.

RAID for SQL Server

http://msdn.microsoft.com/en-us/library/ms190764.aspx

RAID 0 (disk striping)
This RAID level offers the highest throughput from a write perspective, but there is no redundancy built in. Since your backup files are your last line of defense and there is not a redundant set of data, I would not recommend this option.

RAID 1 (disk mirroring)
This level also offers a high level of I/O throughput as well as a redundant copy. The downside is that the size of the data partition using RAID 1 will be limited based on your physical disk size. One option would be to write your backup file to multiple RAID 1 arrays that are independent of each other and, possibly, to use different controllers or channels to further increase the I/O throughput.

RAID 5 (striping with parity)
With RAID 5 there is a write penalty because of the need to keep the parity in check. If you are really looking for faster backup processing, this RAID level does not make sense.

RAID 10 (mirroring with striping)
With RAID 10, you get the advantage of both RAID 0 and RAID 1. The downside is that it becomes very expensive to implement. But, the upside is your I/O throughput should be very fast. Since RAID 10 uses all of the drives in the array, to gain higher I/O rates, the more drives in the array will increase performance.


Recommended: Consider using independent pair of RAID1 volumes over RAID10

In addition to RAID levels, here are other things to take into consideration:

Controllers
In setting up your RAID arrays, try to isolate the different types of I/O traffic to different controller cards or channels on the controller cards. Also, the better the controller card, the better the performance.

Disk speeds
Disk drives come in multiple speeds. SCSI drives currently run as fast as 15 K rpm and are faster than the IDE drives. In most cases, enterprise-wide servers will probably be using SCSI technology which is the better choice.

Disk size
You can now get drives that are several GBs in size. This is great if you want to store a lot of data that is not accessed very often. But for writing out a large amount of data as fast as possible, large disk size doesn't help. What will be more advantageous are more drives in the array versus a couple of very large drives.


Refer: http://blogs.zdnet.com/Ou/?p=484



Tempdb - Separate RAID0
Data/Log- RAID 10 but pricey, or go for independent RAID1 arrays

Friday, September 10, 2010

Performance counters

Performance Object

Counter

Instance

Remarks

Memory

Pages/Sec

Rate of hard page faults;

Average value <>

Physical Disk

Average Disk Queue Length

Average number of queued disk requests;

Average value <>

Processor

% Processor Time

_Total

Average value <>

SQL Server: Access Methods

Freespace scans/sec

The areas where the count increases need to be monitored and proper indexes introduced to avoid such scans.

SQL Server: Access Methods

Full scans/sec

The areas where the count increases need to be monitored and indexes revisited.

SQL Server: Buffer Manager

Buffer Cache Hit Ratio

% of requests served out of the buffer cache;

Average value >= 90%

SQL Server: Locks

Lock timeouts/sec

_Total

Should be 0

SQL Server: Locks

Lock wait time (ms)

_Total

Should be low

SQL Server: Memory Manager

Memory Grants Pending

Number of processes waiting for workspace memory grant;

Average value = 0

SQL Server: SQL Statistics

SQL Re-compilations/sec

Should be 0 or near it

System

Processor Queue Length

Number of requests outstanding on the processor:

Average value <>

System

Context Switches/sec

Rate of switching processors from one thread to another:

Average value <>