SQL Server Agent Job Running Slow - sql-server

I am executing a stored procedure using SQL Server Agent Job in SQL Server 2005.
This job was running fast until yesterday. Since yesterday this job is taking more than 1 hour instead of 2 mins.
I executed the stored procedure in SSMS, it just took less than 1 minute to execute.
I could not figure out why it is taking more than 1 hour when executed as a SQL Server Agent job?

After some time commenting and assuming that the SP performs with the same input parameters and data well when executed in SSMS, I finnaly think I can give a last tip:
Depending on what actions are performed within the SP (e.g. inserting/updating/deleting a lot of data within a loop or cursor), you should set nocount on at the beginning of your code.
set nocount on
If this is not the case or does not help, please add more information, already mentioned in the comments (e.g. all settings of the Job and each Jobstep, what has been logged, what is in the Jobhistory, check SQLerrorlogs, eventlogs,....).
Also take a look at the "SQL Server Logs" maybe you can gather some info here. Also a look into the Application/System eventlo of the Databaseserver is always a good idea.
To get a basic overview you can use the Activitymonitor in SSMS, by selecting the Databaseserver and selecting "Activity monitor" from contextmenu and search for the sql agent.
My last try would be to try to run a sql trace for the agent. In this case you would start a trace and filter e.g. by the user that the SQLAgent Service runs. There are so many options you can set for traces, so I would recommend to google for it, search on MSDN or ask another question here on stackoverflow.

We have a large proc that runs in 88 seconds in SSMS and 30-45 minutes in SQL Server Agent. I added the dbo. prefix on all the table names and now it runs just as fast as SSMS.

I've noticed that SQL Agent jobs ignore the server's MAXDOP setting and run everything with a MAXDOP of 1. If I run a stored procedure in a query windows, it obeys the server settings and uses 4 processes. If I use SQL Agent, any stored procedure I run uses only one process.

I have a similar issue with a script that calls a number of UDFs that I created. The UDF's themselves normally run subsecond under SSMS. Likewise, running the reports I generate with them is bearable under SSMS (30d data in 8s, 365d data in 22s). I've always done NOCOUNT ON with my SQL Agent jobs as they normally generate text files out for pickin up by other processes or Excel and I do not want the extra data at the end, so it was not a solution for me.
In this case, when we run the exact same script under SQL Agent as a job, my times grow exponentially. My 8s script takes 2m30s and my 22s script takes 2h20m. This is the same whether I run it midday with other user activity and jobs or after hours with no user activity, nor jobs or backups running. Our server is idle and at best I get one of the 8 cores being utilized when run. DB is only about 10GB running on SSD with a cached RAID card and 16 of 32GB RAM is free. Since my SQL runs efficiently in SSMS, I am pretty well of the belief that I am hitting a threading limit of some sort. I have researched and tried adjusting MAXDOP just prior to the scripts in the SQL Agent with no luck.
Since this is an activity I want to schedule, it needs to be automated one way or another. I could let these scripts take the hours they need to run as SQL steps in SQL Agent jobs, but I decided to run from command line instead and I get the same performance I see in SSMS.
sqlcmd -S SQLSRVRHost -i "C:\My Script Loc With Spaces.sql" -v MyVar="VarValue" >"C:\MyOutputFile.txt"
So I created a batch script with the SQL jobs run from sqlcmd. Then I run the batch script from a SQL Agent job, so I still have the same management and control in place. My 4 SQL jobs that collectively took over 3 hours to run complete in 1 min and a few seconds from a single batch script executed by SQL Agent.
I hope this helps...

Related

Schedule SQL Job X minutes after SQL Server Agent starts

I have a Job in SQL server which is scheduled to 'Start automatically when SQL Server Agent starts', however, this is creating some issues in our environment. The issue is not SQL specific but because of it we need to delay the execution of this particular job. I cannot schedule this as a recurring job and assign a particular time of a day, it needs to run every time when the SQL Server starts. Is there a way in which I can schedule the job that runs after 15 mins after SQL Server Agent starts?
Sure, just make the first step:
WAITFOR DELAY '00:15:00';
(Or, probably better, you could try to resolve whatever "some issues" are.)
However, note that someone can restart the Agent service without restarting SQL Server; or, they could set SQL Server Agent to not automatically start at startup, so that the next time the SQL Server service is restarted, your procedure will not run.
If you want to tie some startup activity to SQL Server starting, you could probably look into startup procedures, but if you need it to wait 15 minutes, the first thing in the procedure would be the above WAITFOR. Also startup procedures can't have input parameters (or output parameters, but that is less likely to be an issue for a stored procedure you're calling from a job).
Finally, SQL Server 2008 R2? That was barely still in support at the beginning of the last decade.

SQL Server 2005: Schedule Start/Stop of TSQL Script

Currently running SQL 2005 on Server 2005. I have a TSQL script which updates 1000 rows at a time. It loops through a counter until there are no rows to update.
In my situation, I have a lot of rows to update and I need to run this "after hours". So I would like to see if there is a way that I can schedule a way to automatically start and stop a task based upon a set time. I am thinking that I could place this task in a SP and start it with SQL Agent. However, I can not think of how to stop the task automatically. I'm open to SSIS too.
You can use a SQL Job, and make this call a stored procedure or some sql. The job can be set on a timer, for specific days etc.
See Creating a Job for help
You can schedule following T-SQL to stop jab as well.
USE msdb ;
GO
EXEC dbo.sp_stop_job N'Your job name' ;
http://technet.microsoft.com/en-us/library/ms182793.aspx

schedule sql job in another server via stored procedure

Here is a picture of how things are supposed to work.
I'll log into sql server and manually start jobA. My interaction ends here. This job doesn't have a schedule enabled. Hence it is manually started. It has 3 steps. The 3rd step will execute another job manually (which is on another server). Technically the 3 step should in turn schedule jobA, to run say after 5 minutes (only 1 time).
But how to schedule jobA which is on another server?
Maybe it is better for jobB to wait for 5 minutes and then start jobA (without scheduling).
Assuming it is SQL Server 2005 or newer, You can use WAITFOR:
WAITFOR DELAY '00:05';
Or if You insist on scheduling a job using stored procedure, check sp_add_schedule stored procedure.
If You already knew this and focus of your question is on 'another' server, check: How to run a Job from a Stored Procedure in another server?

Scheduled jobs in Sql Agent

I have created SSIS packages to move data from AS400 to SQL Server which are scheduled daily.some of the packages in sql agent are taking longer duration more than 9 hours to complete.IF I run same package in Business intelligence studio manually, it is completing in less than 4 hours.Due to this problem my schedule packages are not competing on time.please help me to sort out this issue. I am unable to understand why there is a difference in task completion duration between manual interaction and schedule jobs.
My environment is windows server 2003 with sql server 2005 with SP3.please help me to sort out this issue.
The best way to get around this problem is to watch the scheduled task by using some debug statements and messages. For example, have some insert statements in the stored procedures the SSIS package is invoking. This way u will get to know what control is taking more time than expected. First try to isolate the control that is making the difference.
Also, you can invoke the package from command prompt using:-
dtexec /f filename.dtsx
This will print out all the messages in the console at each step as well.
Use SSIS logging in the package to log to a database table. Set logging to record start and end of tasks. By running the package in BIDS and comparing it to the logging when it is run on the server you will see which tasks are taking too long. See http://msdn.microsoft.com/en-us/library/ms138020.aspx for more info on SSIS logging (in sql 2008)
Might it be that the SQL server is less powerful than your client or has more load when you execute the package?
Business intelligence Studio the package is executed on your local client with it's CPU and RAM (I think).
Check what version of DTSEXEC you are using. May be you are using 32-bit version at one place and 64-bit at the other one.

SQL Server job (stored proc) trace

I need your suggestion on tracing the issue.
We are running data load jobs at early morning and loading the data from Excel file into SQL Server 2005 db. When job runs on production server, many times it takes 2 to 3 hours to complete the tasks. We could drill down to one job step which is taking 99% of the total time to finish.
While running the job step (stored procs) on staging environment (with the same production database restored) takes 9 to 10 minutes, the same takes hours on production server when it run at early morning as part of job. The production server always stuck up at the very job step.
I would like to run trace on the very job step (around 10 stored procs run for each user in while loop within the job step) and collect the info to figure out the issue.
What are the ways available in SQL Server 2005 to achieve the same? I want to run the trace only for these SPs and not for certain period time period on production server, as trace give lots of information and it becomes very difficult for me (as not being DBA) to analyze that much of trace information and figure out the issue. So I want to collect info about specific SPs only.
Let me know what you suggest.
Appreciate your time and help.
Thanks.
Use SQL Profiler. It allows you to trace plenty of events, including stored procedures, and even apply filters to the trace.
Create a new trace
Select just stored procedures (RPC:Completed)
Check "TextData" for columns to read
Click the "Column Filters" button
Select "TextData" from the left hand nav
Expand the "Like" tree view, and type in your procedure name
Check "Exclude Rows that Do Not Contain Values"
Click "OK", then "Run"
What else is happening on the server at that time is important when it is faster on other servers but not prod. Maybe you are running into the daily backup or maintenance of statistics or indexes jobs?

Resources