Search code examples
sql-serverjobs

I need to truncate and replace data in a table that is frequently being queried


Primary Question:

I want to truncate and refresh a table in SQL Server, but want to wait until any queries currently accessing the table to finish. Is this a simple setting in SQL Server, or do I need to create some logic to accomplish it?

Detailed Description:

I have a VB application that sits on about 300 terminals. The application calls a SqlServer(2008 R2) stored procedure ([spGetScreenData]) every 2 minutes to get the latest sales data.
[spGetScreenData] creates a series of temp tables and returns a select query of about 200 rows and 100 columns. It takes about 8 seconds to execute.

My goal is to create a new stored procedure ([spRefreshScreenData]) that executes every two minutes which will refresh the data in a table ([SCREEN_DATA]). I will then change [spGetScreenData] to simply query [SCREEN_DATA].

The job that refreshes [SCREEN_DATA] first sets a flag in a status table to 'RUNNING' while it executes. Once complete, it sets that status to 'COMPLETED'.

[spGetScreenData] checks the status of the flag before querying and waits (for a period of time) until it's ready. Something like...

DECLARE @Condition AS BIT=0
,       @Count AS INT=0
,       @CycleCount AS INT=10 --10 cycles (20 Seconds)

WHILE @Condition = 0 AND @Count < @CycleCount
BEGIN
    SET @Count = @Count + 1

    IF EXISTS(  SELECT  Status 
                FROM    tbl_Process_Status
                WHERE   Process = 'POS_Table_Refresh' 
                AND     Status='Running')
            WAITFOR DELAY '000:00:02' --Wait 2 seconds
        ELSE
            SET @Condition=1
END

SELECT *
FROM SCREEN_DATA
WHERE (Store=@Store OR @Store IS NULL)

My concern has to do with [spRefreshScreenData]. When [spRefeshScreenData] begins its truncation, there could be dozens of requests for the data currently running.

Will SqlServer simply wait until the request are done before truncating? Is there a setting I have to set to not mess these queries up?

Or do I have to build some mechanism to wait until all requests are completed before starting the truncation?


Solution

  • The job that refreshes [SCREEN_DATA] first sets a flag in a status table to 'RUNNING' while it executes. Once complete, it sets that status to 'COMPLETED'.

    [spGetScreenData] checks the status of the flag before querying and waits (for a period of time) until it's ready

    Don't. Use app locks. The readers (spGetScreenData) are the app lock in shared mode, the writers (refresh job) requests it X mode. See sp_getapplock.

    But even this is no necessary. You can build the new data online, while the queries continue, w/o affecting them using a staging table, ye a different table than the one queried by the apps. When the rebuild is complete simply swap the original tabel with the staging one, using either fast SWITCH operations (see Transferring Data Efficiently by Using Partition Switching) or using the good 'ole sp_rename trick.