My apologies for the unintuitive topic title.
I have a Jobs
table where each row represents a maintenance task performed by a computer program. It has the following construction:
CREATE TABLE Jobs ( JobId bigint PRIMARY KEY, ... Status int NOT NULL, OriginalJobId bigint NULL )
When the task is created / launched, its row is added to the table, and its status is 0
. When the task is completed, its status is updated to 1
, and when the task is not completed, its status is updated to 2
. When a job fails, the job manager will repeat the job, inserting a new row into the Jobs table, duplicating the details of the failed job and reset Status
to 0
and using the original (failed) JobId in OriginalJobId
for tracking purposes. If this retry failed, it should be repeated up to 3 times, each subsequent JobId
will contain the original JobId
in the OriginalJobId
column.
My problem is trying to formulate a query in order to get the current set of jobs that failed and get their number of attempts.
Here is an example of the data in the table:
JobId | Status | OriginalJobId 1, 1, NULL -- Successful initial job 2, 0, NULL -- Pending initial job 3, 2, NULL -- Failed initial job 4, 1, 3 -- Successful retry of Job 3 5, 2, NULL -- Failed initial job 6, 2, 5 -- Failed retry 1 of Job 5 7, 2, 5 -- Failed retry 2 of Job 5 -- should be tried again for 1 more time 8, 2, NULL -- Failed initial job 9, 2, 8 -- Failed retry 1 of Job 8 10, 2, 8 -- Failed retry 2 of Job 8 11, 2, 8 -- Failed retry 3 of Job 8 -- don't try again 12, 2, NULL -- Failed initial job
My request should return this:
JobId | RetryCount 5, 2 12, 0
Please note that Job 3
not enabled because its most recent retry was successful (status 1
). Similarly, Job 8
excluded because the number of attempts exceeds the limit of 3. Task 5
turned on because it is still not completed and has only 2 retries, and Job 12
on and has not yet tried.
I think the solution would be something like this:
SELECT J1.JobId FROM Jobs AS J1 LEFT OUTER JOIN Jobs AS J2 ON J1.JobId = J2.OriginalJobId WHERE J1.Status = 2
... but I canβt figure out how to get RetryCount data.
Here is the SQLFiddle I created for this problem, with one of the following solutions:
http://sqlfiddle.com/#!6/8765f
Update
Here is an updated SQLFiddle that compares 5 solutions provided so far (I added an additional HAVING
to remove jobs that had more than 3 attempts)
http://sqlfiddle.com/#!6/8765f/23
In terms of performance, I think GarethD's answer is the best, as it has the simplest execution plan and tends to end with the fastest time in SqlFiddle.
My production table has about 14,000,000 rows, so obviously the results will be different. I will try everyone in production and see which one is the fastest, and select the answer accordingly.
Thank you all for your help!