August 2007 - Posts

Use Trace to capture Production Server Activity

One of the great things about SQL Server is that there are many ways to accomplish the same task.

When problems arise it can be difficult to determine what went wrong, so I keep a trace going on all my production servers. I capture details on all RPC Completed and Batch Completed events, so I know what was submitted to the server for processing and when, so when an application problem arises I can see what the application sent to SQL Server, and can work through the issue with the developers or vendor (for third-party apps).

Now, SQL Server Profiler has a great graphical interface that allows you to select what information to trace, but it can seriously impact performance and on a production server that can be a very bad thing. Profiler has a great option that allows you to script the trace commands to a script file. You can then run the script, and you get the same trace you would get without incurring the performance overhead you'd get by running Profiler.

First, set up the trace you want to run in Profiler. You'll want to select the Save To File option, and specify a file name on the server hard drive. (I use a subdirectory under my Backup directory, so the nightly backup to tape grabs the trace files as well.) The default file size is 5MB, but I set it at 20MB so I don't get too many physical files, but I do set it to roll over so I don't lose any trace data.

Once you've set up the options you want you can go to the File menu, select Export, then Script Trace Definition, then For SQL Server 2005, and specify a file name for the script. It'll produce a file something like this:

 

/****************************************************/
/* Created by: SQL Server Profiler 2005 */
/* Date: 08/28/2007 08:41:47 AM */
/****************************************************/


-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
set @maxfilesize = 20

-- Please replace the text InsertFileNameHere, with an appropriate
-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension
-- will be appended to the filename automatically. If you are writing from
-- remote server to local drive, please use UNC path and make sure server has
-- write access to your network share

exec @rc = sp_trace_create @TraceID output, 0, N'InsertFileNameHere', @maxfilesize, NULL
if (@rc != 0) goto error

-- Client side File and Table cannot be scripted

-- Set the events
declare @on bit
set @on = 1
exec sp_trace_setevent @TraceID, 10, 15, @on
exec sp_trace_setevent @TraceID, 10, 16, @on
exec sp_trace_setevent @TraceID, 10, 1, @on
exec sp_trace_setevent @TraceID, 10, 9, @on
exec sp_trace_setevent @TraceID, 10, 17, @on
exec sp_trace_setevent @TraceID, 10, 2, @on
exec sp_trace_setevent @TraceID, 10, 10, @on
exec sp_trace_setevent @TraceID, 10, 18, @on
exec sp_trace_setevent @TraceID, 10, 11, @on
exec sp_trace_setevent @TraceID, 10, 12, @on
exec sp_trace_setevent @TraceID, 10, 13, @on
exec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 14, @on
exec sp_trace_setevent @TraceID, 12, 15, @on
exec sp_trace_setevent @TraceID, 12, 16, @on
exec sp_trace_setevent @TraceID, 12, 1, @on
exec sp_trace_setevent @TraceID, 12, 9, @on
exec sp_trace_setevent @TraceID, 12, 17, @on
exec sp_trace_setevent @TraceID, 12, 6, @on
exec sp_trace_setevent @TraceID, 12, 10, @on
exec sp_trace_setevent @TraceID, 12, 14, @on
exec sp_trace_setevent @TraceID, 12, 18, @on
exec sp_trace_setevent @TraceID, 12, 11, @on
exec sp_trace_setevent @TraceID, 12, 12, @on
exec sp_trace_setevent @TraceID, 12, 13, @on


-- Set the Filters
declare @intfilter int
declare @bigintfilter bigint

exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - fab17603-e5cc-4107-80a1-65330cd2eece'
-- Set the trace status to start
exec sp_trace_setstatus @TraceID, 1

-- display trace id for future references
select TraceID=@TraceID
goto finish

error:
select ErrorCode=@rc

finish:
go

 

Read the comments, and set up the file name for the trace file appropriately. You can close Profiler because you no longer need it. You can run the script in a new query in SQL Server Management Studio, or you can set up a job to start it up. Once it's running it'll save the server activity you selected earlier in a trace file, which you can then review after something unusual has occurred to see what was sent to the server.

Allen




Cross-posted from SQLBlog! - http://www.sqlblog.com


Time for a Change

After more than a year of blogging at sqljunkies.com it's time for a change.  As I mentioned yesterday, Adam and Peter have put together a great resource site and I'm really happy to be part of it now.  My blog posts are no longer posted at http://sqljunkies.com/Weblog/MarathonSQLGuy/default.aspx so please change your bookmarks to my new site at http://sqlblog.com/blogs/allen_white/default.aspx.

Thanks.

Allen

 




Cross-posted from SQLBlog! - http://www.sqlblog.com


PASS - One Month and Counting

In one month from today I'll be presenting my session Using SMO to Manage SQL Server at the 2007 PASS Community Summit conference in Denver.

I hope to see you there.

Allen

Naming Conventions for Tables and Views

Last month I taught the Microsoft 2779B Implementing a Microsoft SQL Server2005 Database class, and in Module 7 (focusing on Views) I came across the following "tip":

Tip: You should develop a consistent naming convention to distinguish views from
tables. For example, you could add the letter v or the word view as a suffix to the
name of each view that you create. This approach allows tables and views to be
easily distinguished.

Now, I agree that you should develop a consistent naming convention, but I advised the students that (in my opinion) views are, from the user's perspective, no different than tables. The relational model makes little distinction between tables (relational variables, or "base" relvars) and views ("derived" or "virtual" relvars).

In fact, in C.J. Date's book An Introduction to Database Systems(8th Edition, Addison-Wesley/Pearson Education, 2004, pg. 75), Chris states "Indeed, the fact that a view is a relvar is precisely one of the strengths of relational systems; it is important in just the same way as the fact that a subset is a set is important in mathematics. Note: SQL Products and the SQL Standard ... often seem to miss this point, however, inasmuch as they refer repeatedly to 'tables and views' with the tacit implication that a view is not a table."

In other words, from the perpective of an application or from the user, there is no difference between a view and a table. Now, we know that's not completely true, because it's easy to create a view that can't be updated because it joins multiple tables, and the query processor can't parse out what's being updated amongst the various tables in the view. That's a relatively easy problem to solve, however, with the INSTEAD OF trigger.

In my company we have an application that manages web sites. It uses a master database for all sites, and then a database for each site. In each database is a table that the application uses to display exhibitors at our trade shows. Some of our trade shows have multiple shows around the country, and for these the site database is actually a set of databases with a parent database for the main site and child databases for the individual shows in the group. In the child databases the table is replaced by a view, presenting data in the parent database. The application had a problem with an update to this object, so I wrote the INSTEAD OF trigger, and the application works as it was supposed to. I didn't have to create views in every database used by the application for this exception situation.

So, my point is that tables and views should follow the same naming standard - the difference between the two should be transparent to the user.

Allen

SQL-DMO Removed from SQL Server 2008 Express

I've installed the new July CTP of SQL Server 2008 (Katmai) and found this in the Readme:

SQL Server Database Management Objects (SQL-DMO) has been removed from SQL Server 2008 Express.
We recommend that you modify applications that currently use this feature as soon as possible.
If you need to support SQL-DMO for SQL Server Express, install the Backward Compatibility
Components from the SQL Server 2005 feature pack from the Microsoft Download Site. Do not
use SQL-DMO in new development work; use SQL Server Management Objects (SMO) instead. You can
refer to the SMO documentation by installing SQL Server 2005 Books Online.

I figure that some people would rather know this sooner than later.

Allen