I've got a stored procedure which seems to be my bottle neck in my application. The issue is that the tables it is being applied on are updated very frequently (about once a second with tens of records) - so indexing is not trivial.
It seems that for every X runs of the SP - there is one that takes about 1.5 seconds to run (where as the others run for about 300-400ms or less). In my understaning, it's the indexing tree being updated.
THE RBDMS is SQL Server 2008 R2.
Here is the SP:
THE PK for the archive and live table is "pk1" (for example) - which is not being used here.
the FK is userid (which is a PK in Table_Users)
INSERT INTO Table_Archive
SELECT userid, longitude, latitude, direction, RcvDate
FROM Table_Live
WHERE userid = @userid
DELETE FROM Table_Live WHERE userid = @userid
-- write down the new location
INSERT INTO
Table_Live (userid, longitude, latitude, direction)
VALUES (@userid, @lon, @lat, @dir)
UPDATE Table_Users
SET location = 'true'
WHERE loginid = (SELECT MAX(loginid) as loginid
FROM Logins
WHERE userid = @userid)
Any idea what could be done to make it run optimally? Preferably it should run under 200ms.