Next, try clicking on the Edit Mappings… button, which takes you to the dialog box shown in Figure 18-8.This should bring up the question of “Why would I have even put the column in the
Trang 1We’re not going to go into real detail on this, because the options available will vary widely depending
on the particular OLE DB data source you’ve selected Indeed, the real point of the moment is just that:SSIS will alter the dialog to give you contextual choices that are relevant to the particular OLE DB datasource you’re using In the preceding example, I’ve simply clicked the “Column names in the first datarow” option, and it’s time to click Next again This takes us to choices for selecting what data we wantout of our source database We have two choices here:
❑ Copy:This has us do a straight copy out of a table or view
❑ Query:This allows us to select out of pretty much any operation that’s going to yield a result set.Let’s start by taking a look at the Query option If you choose this option and click Next, you get yetanother dialog; this one pretty boring in the sense that all it does is give you a box where you can enter
in the text of your query (See Figure 18-5.)
Figure 18-5
In my example here, I’ve shown where we could be setting up to export a list of employees that we hired
in our first month of business If you want to test this, you can type the query, and then try testing itwith the Parse button to make sure that the query you want to run is syntactically valid, or you can copy
it into a query window in the Management Studio and test it there Then click Next to get to the dialogbox shown in Figure 18-6
Trang 2Figure 18-6
For starters, this sets us up to decide what we want our flat file to look like I can easily change the rowdelimiter (For example, if you’re exporting for use in a Linux or UNIX based system, you may want tochoose just a line feed rather than the line feed/carriage return option.) You can click Preview to seewhether your query looks like it’s going to return the data that you expect (See Figure 18-7.)
Figure 18-7
Click OK, and you’re back to the Configure Flat File dialog
Trang 3Next, try clicking on the Edit Mappings… button, which takes you to the dialog box shown in Figure 18-8.
This should bring up the question of “Why would I have even put the column in the original query if I was just going to ignore it at output?” The reasons are multifold First, you may have wanted the column there primarily for preview purposes to verify that the data is what you expected — for example, including the names to verify that it seems like the right people when you only intend to output the EmployeeID.
Also, you may be using a query that is copied from some other source and want to avoid risking editing
it (for example, if it is particularly complex).
One other point: This dialog is shared with the direct table choice (Back when we chose Query, we had the option of a table, remember?) So the option is even more applicable in direct table copy scenarios, where there may be many columns in the table that you don’t want in the end output.
Next, try clicking on the Type column values, and you’ll see a number of choices
I highly recommend resizing the columns in this dialog so you can see what the Type column is really trying to show you Just hover your mouse over the right side of the column header much as you would
if you were using Excel, and then click and drag the column divider to the right to enlarge the column.
Trang 4Most of the time you’ll stick with whatever the default conversion was, based on the source data type,but sometimes you may want to change the output type in some way, for example, treating integer data
as a string to deal with some expectation of the end destination of your data (Not all systems treat what
is seemingly the same data the same way.)
Finally, you can mess with the nullability and scale/precision of your data It’s pretty rare that you wouldwant to do this, and it’s something of an advanced concept, but suffice to say that if you were to use moreadvanced transformations (instead of the default Import/Export Wizard), then this might be an interest-ing option to facilitate error trapping of data that isn’t going to successfully go into your destinationsystem In most cases, however, you would want to do that with WHEREclauses in your original query.Now, let’s click Cancel to go back to our Configure Flat File dialog, and then click Next to move onto thenext dialog
At this point, you’ll get a simple confirmation dialog It will synopsize all the things that you’ve askedthe wizard to do — in this case:
❑ Copy rows from [Query] to C:\Documents and Settings\xxx\My Documents\
TestExport.txt
❑ The new target table will be created
❑ The package will be saved to the package file C:\Documents and Settings\xxxx\
My Documents\Visual Studio 2005\Projects\Integration Services Project1\Integration Services Project1\Package1.dtsx
❑ The package will not be run immediately
Most of this is self-explanatory, but I want to stress a couple of things
First, it will create the file for us with the name specified; if, when we designed the package, the file hadbeen detected as already existing, then we would have been given some options on whether to overwritethe existing file or just append our data onto the end of it
Perhaps more importantly, notice that a package is being created for you This package is what amounts
to an SSIS program You can have SSIS execute that package over and over again (including schedulingit), and it will perform the defined export for you each time
Finally, note that it says that your package will not be run immediately; you need either to execute this
package manually or schedule it to run
Click Next, and your package is created
Let me stress again that this is just something of a preparation step; your package has not run, and your data is not yet exported You still need to execute or schedule the package to get your actual exported
file That side of things — the execution of the package — is the same for all SSIS packages though, and isn’t Import/Export Wizard-specific, so we’ll hold that side of things until the next section.
Trang 5Examining P ackage Basics
Fully examining all the parts of an SSIS package and the uses of each piece can fill up the entire book onits own (Indeed, there are many books dedicated to the subject.) But understanding at least the funda-mentals is important to be able to make even the most basic use of Integration Services, so let’s take alook at the package that the Import/Export Wizard just created for us
Assuming you still have the Development Studio open (including the package we just created — if not, openthat back up), you should have a fairly standard looking Visual Studio window up similar to Figure 18-9,with the exception of having some fairly Integration Services-oriented stuff in the main pane (as well as
in the Connection Managers pane just below the main pane)
Figure 18-9
Before we explore the Integration Services panes, let’s focus first on the standard Visual Studio SolutionsExplorer pane (by default, in the upper right It was what we right-clicked to start the Import/ExportWizard) You should see two packages there The first — simply titled Package.dtsx— was a default
Trang 6package created when we first chose the Integration Services template when we first created our newpackage It should be empty, and can be deleted Package1.dtsxwill be the one you want to select (youcan rename it if you choose) in order to follow the remaining discussion in this section.
With that out of the way, let’s look at a few of the key panes…
Connection Managers
I know… I know… You expected me to go top down! Well, besides my sadistic nature coming forward,
I have a more logical reason to get the Connection Managers pane out of the way First, it’s simple, and we’ll be a while on the main pane, so let’s get it out of the way.
By this point, you may well already have a solid idea as to what the Connection Managers pane tured in Figure 18-10) is all about It’s about allowing you quick access to all the connections that areused in our package
(fea-Figure 18-10
If you double-click on either of the two connection managers, you should see a dialog that will look prettysimilar to the dialogs that you worked with earlier in the previous section (Refer to Figures 18-3 and 18-4.)Notice, however, that they are not quite the same Indeed, the FlatFileConnection Manager has a lot of thesame information, but has a somewhat different editing format For example, Figure 18-11 shows the part
of the dialog oriented around editing the data types we want output
That said, there really isn’t anything new here The connection managers area is just a path to allow us toedit our connection information (both for input and output) after we’ve exited the wizard
The Package Editor Pane
This is something of the “meat and potatoes” section of the package While we obviously couldn’t movemuch data around without the connections we discussed in the previous section, you’ll find that the con-nection side of things won’t look all that different from package to package The actual flow and handlerside of things will, however, often vary a great deal
As you can see along the top of Figure 18-12, the package editor has four tables to it, so let’s explore each
of these in turn
Control Flow
This does pretty much what its title would indicate It controls flow between different logical constructs,
or “tasks,” in our package In our case, we have only one task defined, the Data Flow Task visible inFigure 18-12, but if you select that task (as I have in Figure 18-13), you’ll see that it will show an arrowcoming out of it
Trang 7Figure 18-11
Figure 18-12
Figure 18-13
Trang 8The arrow is representative of possible flow We could, for example, add an FTP or external process task
to our package to deal with elements of our Extract, Transform, and Load processes that can’t be handledwithin the one data flow task that we have This control flow aspect is critical to many packages in sev-eral ways:
❑ The flow outlined in this part of the package establishes precedence Think about this for amoment: If we need to FTP our data export to some external system after we’ve created it, then
we need to be sure that the FTP process does not fire until the export portion of the package iscomplete; control of flow does this for us
❑ If multiple tasks are available that do not have precedence on earlier tasks (or are dependent onthe same task), the package can allow the tasks to branch and run simultaneously
❑ Further branching can take place depending on specific return conditions from the previoustask (for example, retrieving a file if our FTP succeeds, but notifying an operator if it doesn’t).What, exactly, there is to potentially edit, varies widely depending on the specific type of task you have
It might range from things as complex as connection and login information (as would be needed insomething like the FTP task mentioned earlier) to something as simple as there being nothing to edit atall (Indeed, were we to edit our data flow task, it would actually take us to the Data Flow tab, withnothing specific to control flow.)
Data Flow
Well, since our example is based around a data flow task, it’s probably not surprising to find a little bitmore meat in this area We can edit the data flow by either double-clicking our data flow task in the Con-trol Flow tab, or selecting the data flow tab, and then selecting the data flow you’re interested in fromthe drop-down in the top of the pane (as shown in Figure 18-14)
Figure 18-14
Our wizard has populated these data flow items for us, but let’s take a quick look inside so we can relate
to the various pieces; either double-click the Source – Query flow item, or right-click it and choose Edit
Trang 9This brings up the OLE DB Source Editor shown in Figure 18-15 The default option for editing is calledthe Connection Manager, but notice that, while there is overlap, there are several differences versus thedialog that we looked at earlier If we were using a parameterized query, this would be where we couldedit the properties of those parameters.
Figure 18-15
Before we leave the Data Flow section, select the Error Output option for this dialog, as shown in Figure 18-16 The key thing to draw out of this dialog is that you have different options on how to handledata flow errors For this simple wizard-generated data flow item, all errors will be considered fatal forthe package, and the package will close with a fail return to whatever called the flow item (In this case,the data flow task in the control flow area, which will in turn pass the failure on and cause the package
to terminate.) Other options would include ignoring the error (and skipping the row) or redirecting therow (perhaps to a holding table where the row can be manually or programmatically repaired beforereattempting to import) Consider these options as you graduate into more advanced packages
Trang 10Acti-❑ OnError:Well, I would hope that this is the most obvious one As you would expect, this event
is fired in the event an error is detected It allows you to address the error in some fashion
❑ OnWarning:Basically the same as OnError, but it allows you to examine and possibly addresswarnings too
❑ OnProgress:For long running processes, this can be handy for updating some form of progressindicator (perhaps a status table somewhere)
❑ OnPreExecute:Allows for things like preparation code to run prior to starting the main part ofthe task
And this is, of course, just a taste What events to use will vary by need
Trang 11Figure 18-17
Package Explorer
This is, again, one of those things that is what it sounds like Much like Windows Explorer is a way toexplore your file system in a folder-based paradigm, the Package Explorer allows you to explore thepackage in a similar paradigm In Figure 18-18, I’ve expanded a few of the folders to give you a feel
Figure 18-18
Trang 12Executing P ackages
There are a few different ways to execute an SSIS package:
❑ From within the Development Studio:The Business Intelligence Development Studio, just likeany other instance of Visual Studio, allows for the idea of you running your packages fromwithin the development environment (you gotta be able to debug!)
❑ The Execute Package Utility:This is essentially an executable where you can specify the age you want to execute, set up any required parameters, and have the utility run it for you ondemand
pack-❑ As a scheduled task using the SQL Server Agent: We’ll talk more about the SQL Server Agent
in our next chapter, but for now, realize that executing an SSIS package is one of the many types
of jobs that the agent understands You can specify a package name and time and frequency torun it in, and the SQL Server agent will take care of it
❑ From within a program:There is an entire object model supporting the notion of instantiatingSSIS objects within your programs, setting properties for the packages, and executing them
This is fairly detailed stuff — so much so that Wrox has an entire book on the subject
(Profes-sional SQL Server 2005 Integrations Services by Knight et al, Wiley 2006) As such, we’re going to
consider it outside the scope of this book other than letting you know it’s there for advancedstudy when you’re ready
Executing a Package Inside the Development Studio
This works just like debugging most any Visual Studio project Simply press F5, click the green arrow onthe toolbar, or choose Start Debugging from the Debug menu As it builds (assuming it builds success-fully), you should see the Data Flow Task first turn yellow, and then green (indicating it ran success-fully) If there were multiple steps to our package, then you would see each task fire in turn until theoverall package was complete
Go ahead and run the package this way, and then check out the produced text file (Its exact location willdepend on whether you specified a full path in your file definition or how Visual Studio is configured.)
Using the Execute Package Utility
The Execute Package Utility comes in two forms: A UI-driven application by the name of DTExecUI.exe, and
a command-line version simply called DTExec.exe For our exploration here, we’ll fire up DTExecUI.exe.You can also navigate using Windows Explorer and find a package in the file system (they end in DTSX)and then double-click it to execute it Do that to our original export package, and you should get the exe-cute dialog shown in Figure 18-19
As you can see, there are a number of different dialogs that you can select by clicking on the variousoptions to the left Coverage of this could take up a book all to itself, but let’s look at a few of the impor-tant things on several key dialogs within this utility
Trang 13❑ SSIS Package Store:This storage model provides the idea of an organized set of “folders”where you can store your package along with other packages of the same general type or pur-pose The folders can be stored in either MSDB or the file system.
Configurations
SSIS allows you to define configurations for your packages These are essentially a collection of settings
to be used, and you can actually combine more than one of them into a suite of settings
Command Files
These are batch files that you wish to run as part of your package You can use these to do system-levelthings such as copying files around to places you need them (They will run under whatever account theIntegration Services Service is running under, so any required access on your network will need to begranted to that account.)
Trang 14Connection Managers
This is a bit of misnomer This isn’t so much a list of connection managers as it is a list of connections Bytaking a look at the Description column, you’ll see many of the key properties for each connection yourpackage uses Notice that in our example package, we have two connections, and if you look closely, you’llsee how one relates to file information (for our connection to the flat file we’re using) and there is anotherthat specifically relates to SQL Server (the export source connection)
Execution Options
Do not underestimate the importance of this one Not only does it allow you to specify how, at a highlevel, you want things to happen if something goes wrong (if there’s an error), but it also allows you toestablish checkpoint tracking — making it easy to see when and where your package is getting to differ-ent execution points This can be critical in performance tuning and debugging
Using this area, you can configure your package to write log information to a number of preconfigured
“providers” (essentially, well understood destinations for your log data) In addition to the preinstalledproviders such as text files and even a SQL Server table, you can even create your own custom providers(not for the faint of heart) You can log at the package level, or you can get very detailed levels of granu-larity and write to different locations for different tasks within your package
age/version you want to execute
Command Line
You can execute SSIS packages from the command line (handy when, for example, you’re trying to runDTS packages out of a batch file) This option within the SSIS Package Execution Utility is about specify-ing parameters you would have used if you had run the package from the command line
The utility will establish most of this for you; the option here is just to allow you to perform something
of an override on the options used when you tell the utility to execute
Trang 15Executing the Package
If you simply click Execute in the Package Execution Utility, your package will be off and running After
it runs, you should find a text file in whatever location you told your package to store it; open it up, take
a look, and verify that it was what you expected
Executing Using the SQL Server Agent
We haven’t really discussed the SQL Server Agent up to this point, but, from an SSIS point of view, think
of the SQL Server Agent as a job scheduler that allows you to do the same thing as running DTExec.exe,but doing so on a specific time and frequency basis with very robust job and task scheduling systems tointegrate your package into a larger set of system jobs
We will discuss the agent in a bit more detail in our next chapter
Executing a Package from Within a Program
SQL Server offers a very rich object model that is usable from within any NET language The bility object model is well beyond the scope of this book, but suffice to say that you can not only program-matically execute packages, but dynamically build packages to meet a certain need before executing them
programma-A F inal Word on P ackages
I want to take a moment and reiterate how much we’ve really only touched the surface of what’s possible
As I’ve said before, there are entire books (rather large ones actually) written around Integration Services
as the sole topic
The package we focused on here was generated by the Import/Export Wizard, but the end product was
a typical SSIS package Not only could we edit it, but we could well have built it from scratch ourselves
We could easily add other tasks to deal with things like “scrubbing” data after importing it to some form
of “staging” area and then perform additional tasks We can add complex branching to deal with errors
or perform several tasks in parallel; the possibilities go on and on
Summar y
SQL Server Integration Services is a robust Extract, Transform, and Load tool You can utilize IntegrationServices to provide one-off or repeated import and export of data to and from your databases — mixing
a variety of data sources while you’re at it
While becoming expert in all that Integration Services has to offer is a positively huge undertaking, ting basic imports and exports up and running is a relative piece of cake I encourage you to start outsimple, and then add to it as you go As you push yourself further and further with what SSIS can do,take a look at other books that are specific to what SSIS has to offer
Trang 17get-19 Playing Administrator
And so, here we are The relative “end of the road,” and yet, it’s really just the beginning It’s time
to talk a bit about maintenance and administration of the databases you develop
As a developer, I can just hear it now: “Isn’t that the Database Administrator’s job?” If you did indeed
say something like that, then step back, and smack yourself — hard (and no, I’m not kidding) If
there is anything I hope to instill in you in your database development efforts, it’s to avoid the
“Hey, I just build ’em, now it’s your problem” attitude that is all too common out there
A database-driven application is a wildly different animal than most stand-alone applications Moststand-alone applications are either self-maintaining, or deal with single files that are relatively easyfor a user to copy somewhere for backup purposes Likewise, they usually have no “maintenance”issues the way that a database does
In this chapter, we’re going to take a look at some of the tasks that are necessary to make sure thatyour end users can not only recover from problems and disasters, but also perform some basicmaintenance that will help things keep running smoothly
Among the things we’ll touch on are:
❑ Scheduling jobs
❑ Backing up and restoring
❑ Performing basic defragmentation and index rebuilding
❑ Using alerts
❑ Using Policy Based Management
❑ ArchivingWhile these are far from the only administration tasks available, these do represent something of
“the minimum” you should expect to address in the deployment plans for your application.For purposes of this book, we’re going to largely stay with the GUI tools that are included in SQLServer Management Studio (we will touch very briefly on the new Policy Based Management) Be
Trang 18aware, however, that there are more robust options available for the more advanced user that can allowyou to programmatically control many of the administration features via the SQL Server ManagementObjects (SMO) object model.
Scheduling Jobs
Many of the tasks that we’ll go over in the remainder of the chapter can be scheduled Scheduling jobs
allows you to run tasks that place a load on the system at off-peak hours It also ensures that you don’tforget to take care of things From index rebuilds to backups, you’ll hear of horror stories over and overabout shops that “forgot” to do that, or thought they had set up a scheduled job but never checked on it
If your background is in Windows Server, and you have scheduled other jobs using the Windows uler service, you could utilize that scheduling engine to support SQL Server Doing things all in the
Sched-Windows Scheduler allows you to have everything in one place, but SQL Server has some more robust branching options.
There are basically two terms to think about: Jobs and Tasks
❑ Tasks:These are single processes that are to be executed or a batch of commands that are to berun Tasks are not independent They exist only as members of jobs
❑ Jobs:These are a grouping of one or more tasks that should be run together You can, however,set up dependencies and branching depending on the success or failure of individual tasks (Forexample, Task A runs if the previous task succeeds, but Task B runs if the previous task fails.)Jobs can be scheduled based on the following criteria:
❑ Daily, weekly, or monthly basis
❑ A specific time of the day
❑ A specific frequency (say, every 10 minutes, or every hour)
❑ When the CPU becomes idle for a period of time
❑ When the SQL Server Agent starts
❑ In response to an alert
Tasks are run by virtue of being part of a job and based on the branching rules you define for your job.Just because a job runs doesn’t mean that all the tasks that are part of that job will run; some may be exe-
cuted and others not depending on the success or failure of previous tasks in the job and what branching
rules you have established SQL Server not only allows one task to fire automatically when another
fin-ishes, it also allows for doing something entirely different (such as running some sort of recovery task) ifthe current task fails
In addition to branching you can, depending on what happens, also tell SQL Server to perform the following:
❑ Provide notification of the success or failure of a job to an operator You’re allowed to send aseparate notification for a network message (which would pop up on a user’s screen as long asthey are logged in), a pager, and an e-mail address to one operator each
Trang 19❑ Write the information to the event log.
❑ Automatically delete the job (to prevent executing it later and generally “clean up”)
Let’s take a quick look at how to create operators in Management Studio
Creating an Operator
If you’re going to make use of the notification features of the SQL Agent, then you must have an tor set up to define the specifics for who is notified This side of things — the creation of operators —isn’t typically done through any kind of automated process or as part of the developed code; these areusually created manually by the DBA We’ll go ahead and take a rather brief look at it here just to under-stand how it works in relation to the scheduling of tasks
opera-Creating an Operator Using Management Studio
To create an operator using Management Studio, you need to navigate to the SQL Server Agent node ofthe server for which you’re creating the operator Expand the SQL Server Agent node, right-click theOperators member, and choose New Operator
Be aware that, depending on your particular installation, the SQL Server Agent Service may not start matically by default If you run into any issues or if you notice the SQL Server Agent icon in Management Studio has a little red square in it, the service is probably set to manual or even disabled; you will probably want to change the service to start automatically Regardless, make sure that it is running for the examples, Try It Out, and Exercises found in this chapter You can do this by right-clicking the Agent node and selecting Manage Service.
auto-You should be presented with the dialog box shown in Figure 19-1 (Mine is partially filled in.)
Figure 19-1
Trang 20You can then fill out a schedule indicating what times this operator is to receive e-mail notifications forcertain kinds of errors that you’ll see on the Notifications tab.
Speaking of that Notifications tab, go ahead and click over to that tab It should appear as in Figure 19-2
Figure 19-2
Until you have more alerts in your system (we’ll get to those later in this chapter), this page may notmake a lot of sense What it is about is setting up which notifications you want this operator to receivedepending on which defined alerts get triggered Again, hard to understand this concept before we’vegotten to alerts, but suffice to say that alerts are triggered when certain things happen in your database,and this page defines which alerts this particular operator receives
Creating Jobs and Tasks
As I mentioned earlier, jobs are a collection of one or more tasks A task is a logical unit of work, such as ing up one database or running a T-SQL script to meet a specific need such as rebuilding all your indexes.Even though a job can contain several tasks, this is no guarantee that every task in a job will run They willeither run or not run depending on the success or failure of other tasks in the job and what you’ve defined
back-to be the response for each case of success or failure For example, you might cancel the remainder of thejob if one of the tasks fails
Just like operators, jobs can be created in Management Studio as well as through programmatic structs For purposes of this book, we’ll stick to the Management Studio method (Even among highlyadvanced SQL Server developers, programmatic construction of jobs and tasks is very rare indeed.)
Trang 21con-Creating Jobs and Tasks Using Management Studio
SQL Server Management Studio makes it very easy to create scheduled jobs Just navigate to the SQLServer Agent node of your server Then right-click the Jobs member and select New Job You should get
a multimode dialog box, shown in Figure 19-3, that will help you build the job one step at a time:
We can then move on to Steps, as shown in Figure 19-4 This is the place where we tell SQL Server tostart creating our new tasks that will be part of this job
To add a new step to our job, we just click the New button and fill in the new dialog box, shown inFigure 19-5 We’ll use a T-SQL statement to raise a bogus error just so we can see that things are reallyhappening when we schedule this job Note, however, that there is an Open button to the left of the com-mand box; you can use this to import SQL scripts that you have saved in files
Trang 22Figure 19-4
Figure 19-5
Trang 23Let’s go ahead and move on to the Advanced tab for this dialog box, shown in Figure 19-6; it’s here that
we really start to see some of the cool functionality that our job scheduler offers
Figure 19-6
Notice several things in this dialog box:
❑ You can automatically set the job to retry at a specific interval if the task fails
❑ You can choose what to do if the job succeeds or fails For each result (success or failure), you can:
❑ Quit reporting success
❑ Quit reporting failure
❑ Move on to the next step
❑ You can output results to a file (This is very nice for auditing.)
❑ You can impersonate another user (for rights purposes) Note that you have to have the rightsfor that user Since we’re logged in as a sysadmin, we can run the job as the dbo or just aboutanyone The average user would probably only have the guest account available (unless theywere the dbo) but, hey, in most cases a general user shouldn’t be scheduling their own jobs thisway anyway (Let your client application provide that functionality.)
Trang 24Okay, so there’s little chance that our RAISERRORstatement is going to fail, so we’ll just take the default
of Quit the job reporting failure on this one (We’ll see other possibilities later in the chapter when wecome to backups.)
That moves us back to the main New Job dialog box, and we’re now ready to move on to the Schedulesnode, shown in Figure 19-7
Figure 19-7
In this dialog box, we can manage one or more scheduled times for this job to run To actually create anew scheduled time for the job to run, we need to click the New button That brings up yet another dia-log box, shown in Figure 19-8
I’ve largely filled this one out already (lest you get buried in a sea of screenshots), but it is from this log box that we create a new schedule for this job Recurrence and frequency are set here
dia-The frequency side of things can be a bit confusing because of the funny way that they’ve worded things
If you want something to run at multiple times every day, then you need to set the job to Occur Dailyevery 1 day This seems like it would run only once a day, but then you also have the option of settingwhether it runs once or at an interval In our case, we want to set our job to run every 5 minutes.Now we’re ready to move on to the next node of our job properties: alerts, shown in Figure 19-9
Trang 25Figure 19-8
Figure 19-9
Trang 26From here, we can select which alerts we want to make depending on what happens Choose Add and
we get yet another rich dialog box, shown in Figure 19-10
Figure 19-10
Our first node — General — is going to let us fill out some of the basics We can, for example, limit thisnotification to one particular database We also define just how severe the condition needs to be beforethe alert will fire (in terms of severity of the error)
From there, it is on to the Response node, shown in Figure 19-11
Notice that I am able to choose the operator that we created earlier in the chapter It is through the nitions of these operators that the SQL Server Agent knows what e-mail address or net send address tomake the notification to Also notice that we have control, on the right side, over how our operator isnotified
defi-Last, but not least, we have the Options node, shown in Figure 19-12
Trang 27Figure 19-11
Figure 19-12
Trang 28Finally, we can go back to the Notifications node of the main New Job dialog box, shown in Figure 19-13.
Figure 19-13
This window lets you bypass the older alerts model and define a response that is specific to this one job;we’ll just stick with what we already have for now, but you could define specific additional notifications
in this dialog box
The last node in the list is the Targets node This one generally falls in both the administrator and advancedrealms, as it is based on the idea of one SQL Server scheduling jobs onto other SQL Servers Be awarethat it’s there for special needs, but we will otherwise save that topic for an advanced-level text
At this point, you are ready to say OK and exit the dialog box You’ll need to wait a few minutes before
the task will fire, but you should start to see log entries appear every five minutes in the Windows event
log You can look at this by navigating to Start ➪ Programs ➪ Administrative Tools ➪ Event Viewer.
You’ll need to switch the view to use the Application log rather than the default System log
Don’t forget that, if you’re going to be running scheduled tasks like this one, you need to have the SQL Server Agent running for them to be executed You can check the status of the SQL Server Agent by
running the SQL Server Configuration Manager and selecting the SQL Server Agent service, or by gating to the SQL Server Agent node of the Object Explorer in Management Studio.
navi-Also, don’t forget to disable this job (Right-click the job in Management Studio after you’ve seen that it’s working the way you expect Otherwise, it will just continue to sit there and create entries in your Application log; eventually, the Application log will fill up and you can have problems with your system!)
Trang 29Backup and Reco ver y
No database-driven application should ever be deployed or sold to a customer without a mechanism fordealing with backup and recovery As I’ve probably told people at least 1,000 times: You would truly beamazed at the percentage of database operations that I’ve gone into that do not have any kind of reliablebackup In a word: EEEeeeeeek!
There is one simple rule to follow regarding backups; do it early and often The follow up to this to notjust back up to a file on the same disk and forget it; you need to make sure that a copy moves to a com-pletely separate place (ideally off-site) to be sure that it’s safe I’ve personally seen servers catch fire.(The stench was terrible, as were all the freaked out staff.) You don’t want to find out that your backupswent up in the same smoke that your original data did
For applications being done by the relative beginner, you’re probably going to stick with referring thecustomer or onsite administrator to SQL Server’s own backup and recovery tools, but, even if you do,you should be prepared to support them as they come up to speed in their use In addition, there is noexcuse for not understanding what it is the customer needs to do
Creating a Backup: a.k.a “A Dump”
Creating a backup file of a given database is actually pretty easy Simply navigate in Object Explorer tothe database you’re interested in, and right-click
Now choose Tasks and Back up, as shown in Figure 19-14
Figure 19-14
Trang 30And you’ll get a dialog box that lets you define pretty much all of the backup process as in Figure 19-15.
Now, those are the simple parts, but let’s break down some of the rest of the options that are available
Backup Type
First of the choices to be made is the backup type Depending on the recovery model for your database(again, be patient with me, we’ll get there on what this is), you’ll have either two or three types of back-ups available:
❑ Full:This is just what it sounds like: a full backup of your actual database file as it is as of thelast transaction that was committed prior to you issuing the Backup command
Trang 31❑ Differential:This might be referred to as a “backup since” backup When you take a differential
backup, it only writes out a copy of the extents (see Chapter 9 if you’ve forgotten) that havechanged since you did the last full backup These typically run much faster than a full backupdoes and will take up less space How much less? Well, that depends on how much your dataactually changes For very large databases where backups can take a very long time to run, it isvery common to have a strategy where you take a full backup only once a week or even onlyonce a month, and then take differential backups in between to save both space and time
❑ Transaction Log:This is again just what it sounds like: a copy of the transaction log This optionwill show up only if your database is set to Full or Bulk logging (This option is hidden if youare using simple logging.) Again, full discussion of what these are is coming up shortly
A subtopic of the backup type is the backup component, which applies only to full and differential backups.For purposes of this book, we should pretty much just be focused on backing up the whole database.That said, you’ll notice another option titled Files and Filegroups Back in our chapters on databaseobjects and creating databases, we touched briefly on the idea of filegroups and individual files for data
to be stored in This option lets you select just one file or filegroup to participate in for this backup; I highlyrecommend avoiding this option until you have graduated to the “expert” class of SQL Server user
Again, I want to stress that you should avoid this particular option until you’ve got yourself something just short of a doctorate in SQL Server Backups The option is for special use, designed to help with very large database installations (figure terabytes) that are in high-availability scenarios There are major consistency issues to be considered when taking and restoring from this style of backup, and they are not for the faint of heart.
Backup Set
A backup set is basically a single name used to refer to one or more destinations for your backup.SQL Server allows for the idea that your backup may be particularly large or that you may otherwisehave reason to back up across multiple devices — be it drives or tapes When you do this, however, youneed to have all the devices you used as a destination available to recover from any of them — that is,they are a “set.” The backup set essentially holds the definition of what destinations were involved inyour particular backup In addition, a backup set contains some property information for your backup.You can, for example, identify an expiration date for the backup
Destination
This is where your data is going to be backed up to Here is where you define one or more destinations
to be utilized for one backup set For most installations this will be a file location (which later will bemoved to tape), but you can also define a backup device that would let you go directly to tape or similarbackup device
Options
In addition to those items we just covered from the General node of the dialog box, you also have a nodethat lets you set other miscellaneous options Most of these are fairly self-describing Of particular note,however, is the Transaction Log area
Trang 32With all this setup, wouldn’t it be nice to set up a job to run this backup on a regular basis? Well, theScript button up at the top of the dialog box provides a means to do just that Click it, and you’ll get alist of options Choose “Script Action to Job…” and it will bring up the Job Schedule dialog box we sawearlier in the chapter, but with the job steps already completed You can then define a regular schedule torun the backup you just defined by selecting the Schedules tab and filling it out as you desire
addi-Imagine for a moment that you're running a bank Let's say you've been taking deposits and withdrawalsfor the last six hours — the time since your last full backup was done Now, if your system went down,I’m guessing you’re not going to like the idea of going to last night’s backup and losing track of all themoney that went out the door or came in during the interim See where I’m going here? You really needevery moment’s worth of data
Keeping the transaction log around gives us the ability to “roll forward” any transactions that happened
since the last full or differential backup was done Assuming both the data backup and the transaction
logs are available, you should be able to recover right up to the point of failure
The recovery model determines for how long and what type of log records are kept; there are threeoptions:
❑ Full: This is what it says; everything is logged Under this model, you should have no data loss
in the event of system failure assuming you had a backup of the data available and have alltransaction logs since that backup If you are missing a log or have one that is damaged, thenyou’ll be able to recover all data up through the last intact log you have available Keep in mind,however, that as keeping everything suggests, this can take up a fair amount of space in a sys-tem that receives a lot of changes or new data
❑ Bulk-Logged: This is like “full recovery light.” Under this option, regular transactions arelogged just as they are with the full recovery method, but bulk operations are not The result isthat, in the event of system failure, a restored backup will contain any changes to data pagesthat did not participate in bulk operations (bulk import of data or index creation, for example),but any bulk operations must be redone The good news on this one is that bulk operations per-
form much better This performance comes with risk attached, so your mileage may vary.
Note that there are several more options for backups that are only addressable via
issuing actual T-SQL commands or using the NET programmability object model
SMO These are fairly advanced use, and further coverage of them can be found in
Profession SQL Server 2008 Programming.
Trang 33❑ Simple: Under this model, the transaction log essentially exists merely to support transactions
as they happen The transaction log is regularly truncated, with any completed or rolled backtransactions essentially being removed from the log (not quite that simple, but that is the effect).This gives us a nice tight log that is smaller and often performs a bit better, but the log is of zerouse for recovery from system failure
For most installations, full recovery is going to be what you want to have for a production-level database.End of story
Recovery
This is something of the reverse of the backup side of things You’ve done your backups religiously, and nowyou want to restore one, either for recovery purposes or merely to make a copy of a database somewhere.Once you have a backup of your database, it’s fairly easy to restore it to the original location To getstarted, it works much as it did for backup: Navigate to the database you want to restore to and right-click; then select Tasks ➪ Restore and up comes your Restore dialog box, as shown in Figure 19-16
Figure 19-16
As long as what you’re after is to take your old backup and slam it over the top of the database youmade the backup of, this is pretty straightforward; simply say OK, and it should restore for you withoutissue
Trang 34Restoring to a Different Location
Things get tricky when you want to change something about where you’re restoring to As part of thebackup process, the backup knows what the name of the database that was backed up, and, perhapsmore importantly, it knows the path(s) to the physical files that it was supposed to be using
Changing the destination database name is right there — no biggie — the problem is that changing thedestination database name does nothing to change which physical files (the MDF and LDF files) it’sgoing to try and store to To deal with this, go to the Options node of the Restore dialog box
Again, most of the options here are self-describing, but, in particular, notice the Restore As column Inthis part of the dialog box, you can replace every original file’s destination location and name, providingyou with the way to deal with restoring multiple copies of a database to the same server (perhaps fortest purposes) or installing your database to a new volume or even a new system
Recovery Status
This one is merely about the state you want to have the database be in when you are done with thisrestore This has particular relevance when you are restoring a database and still have logs to apply tothe database later
If you go with the default option (which translates to using the WITH RECOVERYoption if you wereusing T-SQL), the database would immediately be in a full online status when the restore operation iscomplete If, for example, you wanted to restore logs after your initial restore was done, you would want
to select one of the two other options Both of these prevent updates from happening to the database,and leave it in a state where more recovery can be done; the difference is merely one of whether usersare allowed to access the database in a “read only” mode or whether the database should be appear asstill being offline
The issue of availability is a larger one than you probably think it is As big of a deal as I’m sure it
already seems, it’s really amazing how quickly users will find their way into your system when the
restore operation suddenly marks the database as available Quite often, even if you know that you will
be “done” after the current restore is completed, you’d like a chance to look over the database prior to
actual users being in there If this is the case, then be sure and use the NO RECOVERYmethod of ing You can later run a restore that is purely for a WITH RECOVERYoption and get the database fully back online once you’re certain you have things just as you want them.
restor-Index Maintenance
Back in Chapter 9, we talked about the issue of how indexes can become fragmented This can become amajor impediment to the performance of your database over time, and it’s something that you need tohave a strategy in place to deal with Fortunately, SQL Server has commands that will reorganize yourdata and indexes to clean things up Couple that with the job scheduling that we’ve already learned about,and you can automate routine defragmentation
You may still run into maintenance scripts that use the old DBCC DBREINDEXor DBCC
INDEXDE-FRAGcommands Both of these have been replaced by functionality within the ALTER INDEXcommand.
Trang 35The older DBCCcommands are still supported for backward compatibility, but Microsoft may remove them at any time, so you should be proactive about updating those scripts should you come across any.
ALTER INDEXis the workhorse of database maintenance We addressed this somewhat back in Chapter 9,but let’s revisit this one, and then look at how to get it scheduled
ALTER INDEX
The command ALTER INDEXis somewhat deceptive in what it does While most ALTERcommands havebeen about changing the definition of our object — we ALTERtables to add or disable constraints andcolumns, for example —ALTER INDEXis different; it is all about maintenance and zero about structure
If you need to change the makeup of your index, you still need to either DROPand CREATEit, or youneed to CREATEand use the with the DROP_EXISTING=ONoption
An abbreviated version of the ALTER INDEXsyntax looks like this:
ALTER INDEX { <name of index> | ALL }
ON <table or view name>
{ REBUILD
[ [ WITH ( <rebuild index option> [ , n ] ) ]
| [ PARTITION = <partition number>
[ WITH ( <partition rebuild index option>
[ , n ] ) ] ] ]
| DISABLE
| REORGANIZE
[ PARTITION = <partition number> ]
[ WITH ( LOB_COMPACTION = { ON | OFF } ) ]
| SET ( <set_index_option> [ , n ] )}
[ ; ]
A fair amount on this is fairly detailed “Realm of the advanced DBA” stuff — usually used on an ad hocbasis to deal with very specific problems, but there are some core elements here that should be part ofour regular maintenance planning We’ll start by looking at a couple of top parameters and then look atthe options that are part of our larger maintenance planning needs
Index Name
You can name a specific index if you want to maintain one specific index, or use ALLto indicate that youwant to perform this maintenance on every index associated with the named table
Table or View Name
Pretty much just what it sounds like: the name of the specific object (table or view) that you want to form the maintenance on Note that it needs to be one specific table (You can feed it a list and say “do all
per-of these please!”)
REBUILD
This is the “industrial strength” approach to fixing an index If you run ALTER INDEXwith this option,the old index is completely thrown away and reconstructed from scratch The result is a truly optimized
Trang 36index, where every page in both the leaf and non-leaf levels of the index have been reconstructed as youhave defined them, either the defaults, or using switches to change things like the fill factor.
Careful on this one By default, as soon as you kick off a REBUILD, the index you are working on is
essentially gone until the rebuild is complete Any queries that relied on that index may become tionally slow (potentially by orders of magnitude) This is the sort of thing you want to test on an
excep-offline system first to have an idea how long it’s going to take, and then schedule to run in off hours,
preferably with someone monitoring it to be sure it’s back online when peak hours come along.
The Enterprise version of SQL Server does include a special ONLINEversion of this option that will keepthe index alive and build the new one parallel to the old, but realize that non-Enterprise versions are notgoing to have that option available, and, even if it is, the creation of the index is going to be a significantload on the system; tread carefully when rebuilding In general, treat this one as though it can havemajor side effects while it runs, and leave its use squarely in the domain of the database administrator
DISABLE
This one does what it says, only in somewhat drastic fashion It would be nice if all this command didwas take your index offline until you decided what you want to do, but instead it essentially marks theindex as unusable Once an index has been disabled, it must be rebuilt (not reorganized, but rebuilt)before it will be active again
This is one you’re very, very rarely going to do yourself (You would more likely just drop the index.) It
is far more likely to happen during a SQL Server upgrade or some other oddball situation
Yet another BE CAREFUL!!! warning on this one If you disable the clustered index for your table, it has the effect of disabling the table The data will remain, but will be inaccessible by all indexes, since
they all depend on the clustered index — the entire table is effectively offline until you rebuild the tered index.
clus-REORGANIZE
BINGO!!! from the developer perspective With REORGANIZEwe hit a happy medium in life When youreorganize your index, you get a slightly less complete optimization than you get with a full rebuild, butone that occurs online (Users can still utilize the index.)
This should, if you’re paying attention, bring about the question “What exactly do you mean by ‘slightly
less complete’?” Well, REORGANIZEworks only on the leaf level of your index; non-leaf levels of theindex go untouched This means that we’re not quite getting a full optimization, but, for the lion’s share
of indexes, that is not where your real cost of fragmentation is, although it can happen and your mileagemay vary
Given its much lower impact on users, this is usually the tool you’ll want to use as part of your regularmaintenance plan; let’s take a look at running a index reorganization command
To run this through its paces, we’re going to do a reorg on a table in the AdventureWorks2008 database.The Production.TransactionHistorytable is an excellent example of a table that is likely to have