Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Keeping at table lean and highly available Expand / Collapse
Author
Message
Posted Thursday, June 21, 2012 10:01 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 25, 2014 12:30 PM
Points: 20, Visits: 105
Hi folks,

I have a question in regards to a design I've been pondering. Perhaps i'm merely over analyzing the problem but I wouldn't mind some input.

I have a raw table that is used to collect large amounts of statistical data. Right now a web application hits this table directly and of course it is inefficiant and slow. I'm trying to redesign to process by having a job that parses through and summarizes the data and outputs it to a staging table before then adding it to a small table that will be exposed to the web application. I have this working great and seeing huge performance gains in a test environment.

My concern (and it might not be warranted) is with the updating of the final table. It needs to be refreshed every hour with the new statistics. Keep in mind as well that the web application will sometimes query the table thousands of times in a span of a few minutes. My thoughts was to have a bit field let's say "isNew" in the destination table that is defaulted to true as new data comes in. (The calling stored procedure always grabbing data from rows where "isNew" is false.) Then I delete "old" rows then change all rows makred as new to old. Making them available once again and ready for the next data load.

The resulting table should never grow beyond 10,000 rows thus the point of trying to keep it small, accessible and fast. Does anybody see this solution causing issues with a query returning no data in that small timeframe when I do the DELETE/ALTER or causing any kind of significant delay or wait to the application. Is there a beter way? Sorry about the verbage folks and any comments are appreciated.

Thanks,
Post #1319432
Posted Thursday, June 21, 2012 10:14 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:38 PM
Points: 22,511, Visits: 30,234
Assuming that you are doing this update in a stored procedure, I would do something more along the lines of this psudo code:

... prep work

set transaction isolation level serializable;
begin transaction
begin try
truncate destination_table;
insert into destination_table;

commit transaction
end try
begin catch
rollback transaction
... other error code as needed
end catch

end -- end of update procedure.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1319439
Posted Thursday, June 21, 2012 10:27 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:30 PM
Points: 41,531, Visits: 34,448
This actually sounds like a case for partitioning. Partition the main table, make sure the staging table is identical (indexes, constraints, columns, etc) then to move the data from staging into main you could just add a new partition to the main table and switch that new partiton with the staging table. Metadata operation, near instant.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1319447
Posted Thursday, June 21, 2012 10:29 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:38 PM
Points: 22,511, Visits: 30,234
GilaMonster (6/21/2012)
This actually sounds like a case for partitioning. Partition the main table, make sure the staging table is identical (indexes, constraints, columns, etc) then to move the data from staging into main you could just add a new partition to the main table and switch that new partiton with the staging table. Metadata operation, near instant.


This works really fast. Only thing is, you need to be using the Enterprise Edition of SQL Server. What edition are you using?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1319449
Posted Thursday, June 21, 2012 11:04 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 25, 2014 12:30 PM
Points: 20, Visits: 105
Yes, I am using Enterprise edition. Partitioning is not something I have yet experimented with but that sounds like a great solution. Thanks for the idea.
Post #1319477
Posted Thursday, June 21, 2012 11:06 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:38 PM
Points: 22,511, Visits: 30,234
Since you are using Enterprise Editon, I'd go with Gail's suggestion.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1319479
Posted Thursday, June 21, 2012 11:12 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 25, 2014 12:30 PM
Points: 20, Visits: 105
GilaMonster (6/21/2012)
This actually sounds like a case for partitioning. Partition the main table, make sure the staging table is identical (indexes, constraints, columns, etc) then to move the data from staging into main you could just add a new partition to the main table and switch that new partiton with the staging table. Metadata operation, near instant.


Just a quick question regarding this method. This loads the 'new' data into the main table quickly but you still end up with the old data in that table. As such could result in unwated data...or perhaps I read it incorrectly.
Post #1319486
Posted Thursday, June 21, 2012 11:14 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:38 PM
Points: 22,511, Visits: 30,234
yb751 (6/21/2012)
GilaMonster (6/21/2012)
This actually sounds like a case for partitioning. Partition the main table, make sure the staging table is identical (indexes, constraints, columns, etc) then to move the data from staging into main you could just add a new partition to the main table and switch that new partiton with the staging table. Metadata operation, near instant.


Just a quick question regarding this method. This loads the 'new' data into the main table quickly but you still end up with the old data in that table. As such could result in unwated data...or perhaps I read it incorrectly.


You would also swap out the old data. This means each set of data will have to be in its own partition.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1319489
Posted Thursday, June 21, 2012 11:22 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, February 25, 2014 12:30 PM
Points: 20, Visits: 105
Hmmmm...just quickly reading over msdn articles on switching it seems you can only switch a partition with an empty one. So then I would see it as a three partition cycle.

1. Staging -> Holding
2. Main -> Staging
3. Holding -> Main
4. Delete Staging

or perhaps simply
1. Main -> Holding
2. Staging -> Main
3. Delete Holding

Thanks folks, I'll have to do some more reading and testing but it sounds promising.
Post #1319501
Posted Thursday, June 21, 2012 11:35 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:30 PM
Points: 41,531, Visits: 34,448
Typically it's done like this:

Load new data into staging
Add a new partition to main
Switch new partition in main and staging (now staging is empty)
Switch partition containing old data with staging
Merge empty partition in main table
Truncate staging.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1319509
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse