views:

254

answers:

7

A similar question has been asked, but since it always depends, I'm asking for my specific situation separately.

I have a web-site page that shows some data that comes from a database, and to generate the data from that database I have to do some fairly complex multiple joins queries.

The data is being updated once a day (nightly).

I would like to pre-generate the data for the said view to speed up the page access.

For that I am creating a table that contains exact data I need.

Question: for my situation, is it reasonable to do complete table wipe followed by insert? or should I do update,insert?

SQL wise seems like DELETE + INSERT will be easier (INSERT part is a single SQL expression).

EDIT: RDBMS: MS SQL Server 2008 Ent

+13  A: 

TRUNCATE will be faster than delete, so if you need to empty a table do that instead

You didn't specify your RDBMS vendor but some of them also have MERGE/UPSERT commands This enables you do update the table if the data exists and insert if it doesn't

SQLMenace
RDBMS: MS SQL Server 2008 Ent
2007 doesn't exists, do you mean 2005 or 2008, if 2008 then look at MERGE http://msdn.microsoft.com/en-us/library/bb510625.aspx
SQLMenace
yes 2008 (IIS is 7) - merge appears to be the perfect solution
A: 

It partly depends on how the data is accessed. If you have a period of time with no (or very few) users accessing it, then there won't be much impact on the data disappearing (between the DELETE and the completion of the INSERT) for a short while.

kevinw
A: 

It depends on the size of the table and the recovery model on the database. If you are deleting many hundreds of thousands of records and reinstating them vs updating a small batch of a few hundred and inserting tens of rows, it will add an unnecessary size to your transaction logs. However you could use TRUNCATE to get around this as it won't affect the transaction log.

Do you have the option of a MERGE/UPSERT? If you're using MS-SQL you can use CROSS APPLY to do something similar if you don't.

Joe Swan
A: 

One approach to handling this type of problem is to insert into new table, then do a table Rename. This will insure that all new data is present at the same time.

Gary
Assuming that it takes multiple inserts, as long as all of the inserts are in a single transaction this is not needed.
Donnie
A: 

What if some data that was present yesterdays is not anymore? Delete may be safer or you could end up deleting some records anyway.

And in the end it doesnt really matter which way you go. Unless on the case @kevinw mentioned

Imre L
+1  A: 

Have you considered using a materialized view (MSSQL calls them indexed views) instead of doing it manually? This could also have other performance benefits as an indexed view gives the query optimizer more choices when its constructing execution plans for other queries that reference the table(s) in the view.

Donnie
materialized view in SQL Server do not allow outer and self joins, which is something any query (worth materializing) will contain. If not for that limitation - materialized view would be perfect solution.
A: 

Although I fully agree with SQLMenace's answer I do would like to point out that MERGE does NOT remove unneeded records ! If you're sure that your new data will be a super-set of the existing data, then MERGE is great, otherwise you'll either need to make sure that you delete any superfluous records later on, or use the TRUNCATE + INSERT method ... (Personally I'm still a fan of the latter as it usually is quite fast, just make sure to drop all indexes/unique constraints upfront and rebuild them one by one. This has the benefit of the INSERT transaction being smaller and the index-adding being done in (smaller) transactions again later on). (**)

(**: yes, this might be tricky on live system, but then again he already mentioned this was done during some kind of overnight anyway, I'm extrapolating there is no user-access at that time)

deroby
(and for reasons beyond me, I can't comment on SQLMenace's anwer, hence my own answer) (I guess I'm too 'young' on StackOverflow atm)
deroby
MERGE on SQL Server DOES handle INSERT, UPDATE _and_ DELETE.
OMG, it does !? I'm so used calling it UPSERT that I didn't expect it to support DELETE... (being stuck on SQL2k5, Merge is one of the things I can't use in my day to day stuff... ) Sorry for the bad advice, reading up on documentation before posting wouldn't hurt me it seems =( http://technet.microsoft.com/en-us/library/bb510625.aspx
deroby