SQL Server Job with precise timing - sql-server-2008

I have a DB with game data (map, players, etc...) and I have a game core mechanics written in T-SQL stored procedure.
I need process game loop (via the stored procedure) every "X" seconds.
I tried used the SQL Job, but when I set the interval to seconds, the SQL server stops responding. If I set the interval greater than one minute, all was ok.
I need game loop precise in time, e.g. the game loop will run only once and will be executed every "X" precisely (tolerance should be less than one second).
Can I do it with SQL Server capabilities? Or should I create a windows service which will repeatly execute game loop procedure? Or should I go another way?
Thanks!
EDIT:
The game loop stored procedure takes less than the interval.

I would use a windows service for this. Have a loop with a thread sleep in it to get it to wait every x seconds.
The problem with timer jobs of type used in SQL server, is that there is a timer service checking if there is a job that need to be run. This timer job may check for example every 2 minutes, so precision down to a second is not possible.

To run smth with interval of 1 sec you can use code:
CREATE PROCEDURE SP_Execute_job
AS
WHILE 1=1
BEGIN
WAITFOR DELAY '00:00:01'
EXECUTE 'somewhat job'
END
END
This sp should be executed after each SQL Server start:
exec sp_procoption N'SP_Execute_job', 'startup', 'on'

Related

How to use/code stored procedure to stop MYSQL currently running task?

Here is my node, which sometimes freezes by the last INSERT command:
This process sometimes will freeze because the functionality for this node is to truncate the table. But sometimes the previous inserting command is not finished and the truncate is triggered, which will cause this node freeze for 2 hours to complete (originally should be 2 seconds to finish).
So my solution is to set up a timer condition if this node take too long then run the Stored procedure stop all MySQL current database tasks (the inserting Action which insert data to this table).
What stored procedure can do this work?
Is the previous node that is doing the insert, configured to run multiple instances? If it is then it is probably configured to "Move on when: Each time an instance finishes". If that is the case then you can configure the previous node to "Move on when: All instances are done".

What is the best way to run a query continually on SQL Server

I have a table on an AS400 server (DB2) that I need monitor from SQL Server. If a record is written, I will take the data, do some stuff and then delete the record.
Currently, I wrote a C# windows services that runs the stored procedure every 500 ms and that works fine. But I want to run this store procedure completely from SQL if possible
I can't run from SQL Server Agent because 5 minutes is too long.
What is the best way to do this and furthermore, is there any issues with running int from a continual loop within the server?
This is my solution:
create a stored procedure that will loop using wait time and a while loop which will exit at a midnight and will run the stored procedure, wait 500 msec and then run it again
Run the looping stored procedure from SQL Server Agent daily at midnight
I will tweak the interval by maybe running the looping sp several times a day.
Thank you for your attention

Find procedure respond time on database last 30 day

Is there any way to find out the duration of store procedure respond in last 30 day or more ?
i have a program that running store procedure on server and some days this sp stops responding or takes too long to respond(need Instance to restart).
now i need to find out why and when this happen.
my guess is there is a some Concurrent sp that locks the table and stop this sp from running.

Is there a way for SLEEP() in a stored procedure?

I have a stored procedure I'd like to run forever, but sleep for one second in a loop. When it wakes up it would poll a table to see if it should do some work. Work only needs to be done every minute, so there is no worry about the poll table getting hit with updates from two writers at the same time.
What is the best way to SLEEP() for an interval in a stored procedure? It would be nice, actually, if it could sleep for 200 milliseconds, but one second would work too.
I've encountered the same problem. After googling a lot, I found out that we can use
SELECT SLEEP(<seconds>);
to delay our procedures for this many seconds. In your case, using
SELECT SLEEP(0.2);
would be just fine.
You can use:
DO SLEEP(0.2);
Reference: http://dev.mysql.com/doc/refman/5.7/en/do.html
or
SELECT SLEEP(0.2);
Reference: http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_sleep
MySQL has an event scheduler baked in. https://dev.mysql.com/doc/refman/5.6/en/events-overview.html
Sample:
CREATE EVENT performance_schema_snapshots.fill_events_statements_summary_by_digest_history1
ON SCHEDULE -- every day at 6 am
EVERY 1 DAY
STARTS TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 14 HOUR
DO
-- take snapshot
CALL performance_schema_snapshots.events_statements_summary_by_digest_snapshot_reset ();
You dont specify which database you are using, but generally the way to achive what you want is not to have an infinetly running sproc but to have some external component - like a scheduler, or SQL Server Agent in MSSQL - execute the sproc every so often.

How to issue a rollback to a cron jobbed MySql query that runs too long

Due to performance issues arising from row locking and a long running query in a trigger, I've opted to instead run the query as a stored procedure from a cron job every five minutes.
My problem is that I need to prevent the situation where the query takes longer than 5 minutes and collides with the next scheduled run of the stored procedure. Since I do run this query in transaction, ideally I'd just execute a rollback somewhere in the stored procedure once the five minutes were up. Is this possible?
Thanks.
The 'brute force' method would be to have a table with 'jobs'. Each row would have a start time and end time. Look for the most recent start time that doesn't have an end time. If your next job wants to start and there isn't an end time listed then kill the previous job.
You could even put the process id in there.
I ended up just noting the epoch time at the beginning of the procedure, then after each record I process I do a commit and calculate the time elapsed. If it's above a preset timeout value I BREAK and end the procedure.
The only possible failure would be if the logic (an insert) in the stored procedure's while loop takes too long, but this is unlikely. I declared some exit handlers to mitigate this possibility (they do ROLLBACKs).