Displaying heavy SQL query intermediate results - sql

I am using SQL Server 2008 and SQL Server Management Studio.
I need to run a heavy SQL query looking through a large number of rows and doing XQuery selection. I have had to stop the execution because it ran for over 30 minutes and it apparently causes timeouts in the software system using the database.
Is there any way of showing the results as they are found instead of in the end, or can I even get some live statistics on the load?

Yes, the WITH FAST query hint allows this kind of behaviour.... it gives you a start, but the overall run time may be longer.
Andy Warren's videos are always worth watching: http://www.sqlshare.com/UsingtheOPTIONFASTQueryHint_819.aspx
Some old guidance from 2006: http://blogs.msdn.com/b/queryoptteam/archive/2006/05/03/589529.aspx

Related

SQL Server query fast after clearing cache

I have a problem with a query running on Microsoft SQL Server 2012.
This query selects articles out of different stores and ran fine over months, within under one second.
Several weeks ago, this query suddenly started taking a very long time to finish - 50 seconds or more - but only for one of our stores.
If I clear the query plan cache for this one SELECT statement, then the query takes less than one second to finish again.
Unfortunately, this problem occurs only very sporadically and only on our productive server, so I have no chance to analyze the problem.
I restarted the server few weeks ago and the problem did not occur until yesterday (before every three days).
Do you have any idea or tip for me to solve this problem?
It is definitely looks like a parameter sniffing issue.
Read more about Parameter Sniffing
You can try following options.
1- Use Dummy variables on SQL Server stored procedure or function used.
2- Alter SP WITH RECOMPILE Option.
3- Use hints like OPTION(RECOMPILE) or OPTION (OPTIMIZE FOR (#VARIABLE=VALUE))
4- Disable Parameter sniffing on SQL instance.
You can read more about this on MSDN for recommendation and workarounds here

Simple SQL Server query always time out

We have a stored procedure that has been working perfectly for over 10 years. A few days ago it started timing out. I run it from the Query console in SQL Management Studio and it timesout. I restart the server and then it runs in 1 second...
You should consider the changes happened to your system over these 10 years, maybe you have added some background services that is doing some heavy sql updates/insertions while you are running your query.
I think you can have a clear vision on the performance of your query if you run it on a staging server or a local machine( which normally have a different load of sql operations ), observe the time it takes to execute your query, do your tries from time to time.
If the execution time of your query on staging server is constant, then your query is not the cause of the issue.

SQL Performance Reports - but for a date range

SQL Server comes with very handy "performance" reports: in SSMS - right-click the server - "reporting" - "Standard" - you see many useful reports, like "Top queries by Total CPU time", "Top queries by average I/O" etc.
These reports aggregate the data since last restart or since last query stats reset.
Is there any way to see these reports for a date range? Like "Top queries by total CPU time from 10pm to 11pm"? Maybe there's an SQL Server addon on or service that should be installed or something, that would allow this kind or research?
Real life use case: a DBA wakes up and sees there were some "timeout" errors during the night. He wants to see which queries were running at the moment and eating up performance.
You can download the SSMS Performance Dashboard Reports from here and use as Custom reports with SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014. Having access to the rdl file you can customize it however you want.
Also worth reading this.
Found this series of blog posts at SQLShack that advises installing "Data Collectors" that continuously record performance data, so you can analyse it later: SQL Server performance monitoring with Data Collector

Limit resources available to query in SQL Server

I want to limit the system resources available to queries run by specific users in SQL Server 2008.
Some queries are seemingly running away with all the resources of the machine causing the server to become unresponsive.
My thought process is that if there is a way to cap the amount of resources (mem/cpu cycles/bw) a query can grab, this problem would go away.
Is this possible? Are there better techniques to accomplish this?
This is MS Sql Server 2008,
If you're running the Enterprise Edition, you could take a look at the Resource Governor.

Logging ALL Queries on a SQL Server 2008 Express Database?

Is there a way to tell SQL Server 2008 Express to log every query (including each and every SELECT Query!) into a file?
It's a Development machine, so the negative side effects of logging Select-Queries are not an issue.
Before someone suggests using the SQL Profiler: This is not available in Express (does anyone know if it's available in the Web Edition?) and i'm looking for a way to log queries even when I am away.
SQL Server Profiler:
File → New Trace
The "General" Tab is displayed.
Here you can choose "Save to file:" so its logged to a file.
View the "Event Selection" Tab
Select the items you want to log.
TSQL → SQL:BatchStarting will get you sql selects
Stored Procedures → RPC:Completed will get you Stored Procedures.
More information from Microsoft: SQL Server 2008 Books Online - Using SQL Server Profiler
Update - SQL Express Edition:
A comment was made that MS SQL Server Profiler is not available for the express edition.
There does appear to be a free alternative: Profiler for Microsoft SQL Server 2005 Express Edition
There is one more way to get information about queries that has been executed on MS SQL Server Express described here.
Briefly, it runs smart query to system tables and gets info(text, time executed) about queries(or cached query plans if needed). Thus you can get info about executed queries without profiler in MSSQL 2008 Express edition.
SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC
…Late answer but I hope it would be useful to other readers here…
Using SQL Server Express with advanced auditing requirements such as this is not really optimal unless it’s only in development environment.
You can use traces (www.broes.nl/2011/10/profiling-on-sql-server-express/) to get the data you need but you’d have to parse these yourself.
There are third party tools that can do this but their cost will be quite high. Log explorer from ApexSQL can log everything but select and Idera’s compliance manager will log select statements as well but it’s cost is a lot higher.
You can log changes. SQL Server 2008 will make this especially easy with Change Data Capture. But SQL Server isn't very good at logging SELECTs.
It is theoretically possible with the profiler, but it will kill your performance. You might "get away with it" on your desktop, but I think you'll notice your machine acting slow enough to cause problems. And it definitely won't work after any kind of deployment.
One important point a couple others have missed already: unless they changed something for 2008 I didn't hear about, you can't trigger a SELECT.
Just for the record, I'm including the hints to use DataWizard's SQL Performance Profiler as a separate answer since it's really the opposite to the answer pointing at SQL Server Profiler.
There is a free trial for 14 days, but even if you need to buy it, it's only $20 for 3 servers (at the moment of writing, 2012-06-28). This seems more than fair to me considering the thousands everybody using SQL Server Express edition has saved.
I've only used the trial so far and it offers exactly what the OP was looking for: a way to trace all queries coming in to a specific database. It also offers to export a trace to an XML file. The paid version offers some more features but I haven't tried them yet.
Disclaimer: I'm just another developer messing with DBs from time to time and I'm in no way affiliated with DataWizard. I just so happened to like their tool and wanted to let people know it existed as it's helped me out with profiling my SQL Server Express installation.
I would either use triggers or use a third party software such as Red Gate to check out your SQL log files.
Seems that you can create traces using T-SQL
http://support.microsoft.com/kb/283790/
That might help.
The SQL query below can show simple query logs:
SELECT last_execution_time, text
FROM sys.dm_exec_query_stats stats
CROSS APPLY sys.dm_exec_sql_text(stats.sql_handle)
ORDER BY last_execution_time
This is how it looks like below: