SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
SQL Azure
Developer
General DBA
ASP.NET / ADO.NET
SQL Azure

Training Videos

Check out our new SQL Server Training Videos section More...

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server
SQL Server 2008 R2 Multi-server Administration - A First Look ...
An overview of Master Data Services - MDS in SQL Server ...

More     
 
Latest FAQ's

Add Node to A SQL Server failover Cluster failed with invalid ...
SQL Server Destination remote server error
Setting Up Data And Log Files For SQL Server
Will Check Constraints Improve Database Performance?

More     
   
Latest Software Reviews

dbForge Review
Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008

More     

articles >> developer >> Handling Cursor-Friendly Problems in T-SQL: Running Totals ...

Handling Cursor-Friendly Problems in T-SQL: Running Totals Example

By : Mirko Marovic
Aug 31, 2006

By design, the SQL programming language works with data sets, so it is not surprising that for most problems, row by row processing is not nearly as efficient as data set processing. There is a class of problems, however, that is much easier to solve using cursors. A typical "cursor friendly" problem is one where the data set returned contains at least one column whose value depends on column values from one or more previous rows of the same row set. Even when a data set based solution exists, it is hard to build a query that is more efficient than a cursor based solution. Usually, advanced coding techniques have to be used, and sometimes a special index design is required for the tables involved. For that type of problem, it is usually better to send a simple row set to the calling application and let the client cycle through the rows and calculate the column value derived.



The Problem

Let's define the problem as displaying cumulative sales for a specific store and product. This is a simplified example since I haven't included the sales period, which would be more realistic. I don't want to include a discussion about handling time periods as a part of the criteria in Transact-SQL. That topic deserves a separate article.

Let's say there is a sales table with the structure below:

CREATE TABLE [dbo].[Sales] (
     [TransactionID] [int] IDENTITY (1, 1) NOT NULL,
     [StoreID] [int] NOT NULL,
     [productID] [int] NOT NULL,
     [transactionTime] [datetime] NOT NULL,
     [amount] [money] NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Sales] WITH NOCHECK ADD
     CONSTRAINT [PK_Sales] PRIMARY KEY CLUSTERED
     (
          [TransactionID]
     ) ON [PRIMARY]
GO

To populate the table you can use this script:

set noCount on
declare @i int
set @i = 0
while @i < 100000 begin

     insert into dbo.Sales(StoreID, productId, transactionTime, amount)
     select 1,
          p.productID,
          dateAdd(minute, - @i, getDate()),
          case (p.ProductID)
           when 1 then 10
           when 2 then 100
          end
     from (select 1 as productID union all select 2 as productID) as p

     if @i % 1000 = 0 print @i

     set @i = @i+1
end
set nocount off
go
set noCount on
declare @i smallint
set @i = 0
while @i < 1000 begin

     insert into dbo.Sales(StoreID, productId, transactionTime, amount)
     select 2,
          p.productID,
          dateAdd(minute, - @i, getDate()),
          case (p.ProductID)
           when 1 then 10
           when 2 then 100
          end
     from (select 1 as productID union all select 2 as productID) as p

     set @i = @i+1
end
set nocount off
go

Finally, we need an index on StoreID and ProductID:

CREATE INDEX [IX_Sales] ON [dbo].[Sales]([StoreID], [productID], [transactionID]) ON [PRIMARY]

GO

We want the script below:

execute dbo.Sales_sel_by_StoreID_ProductID @storeID = 2, @productID = 1

To output this row set:

transactionID

transactionTime

amount

total

200001

29.12.2005 17:18

10.0000

10.0000

200003

29.12.2005 17:19

10.0000

20.0000

200005

29.12.2005 17:20

10.0000

30.0000

200007

29.12.2005 17:21

10.0000

40.0000

200009

29.12.2005 17:22

10.0000

50.0000

200011

29.12.2005 17:23

10.0000

60.0000

200013

29.12.2005 17:24

10.0000

70.0000

200015

29.12.2005 17:25

10.0000

80.0000

200017

29.12.2005 17:26

10.0000

90.0000

200019

29.12.2005 17:27

10.0000

100.0000

…

…

…

…


    Next Page>>    








C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Cloud Hosting Magazine | ASP.NET Tutorials | Windows Server Help | Windows Phone Pro | Silverlight Ace | Visual Studio Tutorials | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 2010 Jude O'Kelly. All rights reserved