Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Should I De-normalize?

Author  Topic 

mpaul31
Starting Member

3 Posts

Posted - 2010-08-06 : 16:37:18
my site has a daily deal and I show the user the total number of sales for that deal for the day. I fetch this with a simple query using a count() aggregate.

i also have a deal history page, where i also show the count using the same aggregate. now to my question, is it an expensive call to keep callling the aggregate in my queries or should i de-normalize my table to store the calculate count?

it just seems i need an additional "moving part" that calculates and updates the denormalized table and keeps it in sync. I also read something about materialized views??

Thoughts?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-06 : 16:45:18
How expensive it is depends on the query, database design, hardware, data. How many rows are in the table? How fast/slow is your current query?

Materialized views are an Oracle concept, but in SQL Server they are called indexed views.

We used to store the aggregate data like you are proposing when we were on SQL 2000, but since we've been on 2005/2008 we haven't had the need (performance-wise).

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mpaul31
Starting Member

3 Posts

Posted - 2010-08-06 : 16:53:39
Right now the transaction table has only a few thousand records, but the site has only been up for a few months, so it should be growing much larger in the near future (expanding to multiple cities). It just seems a little wasteful to calculate the total sales for deals in the past where those totals will not change.

With the following command, is there ever a chance the transaction row count and total sales count would not be in sync (not including deleting rows)? Does the default isolation level work for this?
Isolation levels always trip me up...

INSERT INTO [transaction] ... 

UPDATE deal
SET TotalSales = (select count(*) from [transaction] where dealid = @dealId)
WHERE dealId = @dealId
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-06 : 16:58:08
Yes the default isolation will work for it, assuming you put your queries into a transaction.

If totals will not change, then you might as well store that data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mpaul31
Starting Member

3 Posts

Posted - 2010-08-06 : 17:05:17
Great, thank you! and just for my clarification, the reason this will work with the default isolation level is because the initial insert into the transaction table will add a lock and no other transaction (say another purchase of the deal) can insert a row, so the count(*) query will always return the correct #?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-06 : 17:20:06
You'll need to lock the deal on top of the isolation level.

I am not great on the locking concept though, so hopefully someone else can assist you with that.


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -