DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group...

40
DAT332 Database Administration For Developers Stephen Wynkoop Stephen Wynkoop SQL Server Worldwide User’s SQL Server Worldwide User’s Group Group www.SSWUG.org www.SSWUG.org [email protected] [email protected]

Transcript of DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group...

Page 1: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

DAT332Database Administration For Developers

DAT332Database Administration For Developers

Stephen WynkoopStephen WynkoopSQL Server Worldwide User’s GroupSQL Server Worldwide User’s [email protected]@sswug.org

Page 2: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

AgendaAgenda

Learn about key features that you Learn about key features that you should be aware ofshould be aware of

Backup, RestoreBackup, Restore

Performance TipsPerformance Tips

Indexes, Tuning, CursorsIndexes, Tuning, Cursors

Available ToolsAvailable Tools

Q&AQ&A

Page 3: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Overall GoalOverall Goal

Get inGet in

Get what you needGet what you need

Get outGet out

NEVER: NEVER: SELECT * FROM mytableSELECT * FROM mytable

Be fastBe fast

Be recoverableBe recoverableData-wiseData-wise

Backup/recover-wiseBackup/recover-wise

Page 4: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Multiple Instance SupportMultiple Instance Support

Why is this interesting?Why is this interesting?

There is typically a default instance There is typically a default instance (but not required)(but not required)

Instances can “talk” to one-anotherInstances can “talk” to one-another

Instances can be administered Instances can be administered separatelyseparately

Good for splitting environments, Good for splitting environments, collation issues, testing, learningcollation issues, testing, learning

Page 5: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Recovery ModelsRecovery Models

Yes, it’s your job to work with the DBAYes, it’s your job to work with the DBA

Different ModelsDifferent ModelsFullFull

SimpleSimple

Bulk-loggedBulk-logged

Point in time, or “marker” recovery (drop Point in time, or “marker” recovery (drop anchor…)anchor…)

DO NOT wait until disaster strikes DO NOT wait until disaster strikes to find out the wrong measuresto find out the wrong measuresare in placeare in place

““Backups are not important Restoration/recovery is”Backups are not important Restoration/recovery is”

Page 6: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Recovery ModelsRecovery Models

Database, Properties, Options….Database, Properties, Options….

Page 7: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Recovery Model: FullRecovery Model: Full

Full transaction log restoreFull transaction log restoreRestore the baseline dbRestore the baseline db

Restore the transaction logs in orderRestore the transaction logs in order

Restored up to last tran. log backupRestored up to last tran. log backup

Watch the transaction log physical Watch the transaction log physical devicesdevices

Beware total reliance on tran. log Beware total reliance on tran. log

Best production environment optionBest production environment option

Page 8: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Recovery Model: SimpleRecovery Model: Simple

Backup/restore at the database levelBackup/restore at the database level

Good for development environmentsGood for development environments

Disaster recovery, but Disaster recovery, but not transaction not transaction recovery since the last backuprecovery since the last backup

““All or nothing” restoreAll or nothing” restore

Page 9: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Recovery Model: Bulk_loggedRecovery Model: Bulk_logged

Better for bulk operationsBetter for bulk operationsSelect into, etc.Select into, etc.

Good for a migrating databaseGood for a migrating databaseIf the data exists elsewhereIf the data exists elsewhereCan be rebuilt from other sourceCan be rebuilt from other sourceGood for DTS move operations – control Good for DTS move operations – control log size build-uplog size build-up

Restore to last transaction log backupRestore to last transaction log backupRe-bulk-operation to get current againRe-bulk-operation to get current againSwitch to better model for productionSwitch to better model for production

Page 10: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Other Recovery OptionsOther Recovery Options

Lumigent Log ExplorerLumigent Log Explorer

BMC SQL Back-TrackBMC SQL Back-Track

Allow you to potentially recover a Allow you to potentially recover a dropped table, a specific transaction, etc.dropped table, a specific transaction, etc.

Page 11: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

SQL Server BackupSQL Server Backup

Backup types/models – know them!Backup types/models – know them!Full (Complete)Full (Complete)

DifferentialDifferential

Transaction logTransaction log

File or file groupFile or file group

File differentialFile differential

Different impacts on the systemDifferent impacts on the system

Change based on production versus Change based on production versus test environmenttest environment

Page 12: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Copy/Move Database WizardCopy/Move Database Wizard

Uses sp_detach_db/sp_attach_dbUses sp_detach_db/sp_attach_db

Beware: objects, scripting, Beware: objects, scripting, dependenciesdependencies

Test, test, testTest, test, test

Check for tasks that copy, system-level Check for tasks that copy, system-level operationsoperations

Check for jobs, logins, etc. Check for jobs, logins, etc.

Good for moving to productionGood for moving to production

Page 13: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Moving A Database To ProductionMoving A Database To Production

DTS: If you use it, be cautious and DTS: If you use it, be cautious and check your workcheck your work

JobsJobs

ViewsViews

Don’t forget “system-level objects” – Don’t forget “system-level objects” – anything outside the db. Won’t transferanything outside the db. Won’t transfer

If you don’t tell it to replace data – it If you don’t tell it to replace data – it will append, duplicating your datawill append, duplicating your data

Page 14: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Creating SQL Agent TasksCreating SQL Agent Tasks

Use these to automate database Use these to automate database processesprocesses

Examples:Examples:Grouping of dataGrouping of data

Creating stat summary tablesCreating stat summary tables

Resetting countersResetting counters

SQL Server Agent: select Jobs, NewSQL Server Agent: select Jobs, New

Document your agent jobsDocument your agent jobs

Page 15: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Creating A Task…Creating A Task…

Page 16: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Maintenance PlansMaintenance Plans

Sets of instructions carried out Sets of instructions carried out regularlyregularly

Database backupDatabase backup

Integrity check, etc.Integrity check, etc.

Page 17: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Database Maintenance PlanDatabase Maintenance Plan

Creates a job for the backupCreates a job for the backup

Creates a job for the transaction log Creates a job for the transaction log backupsbackups

Creates a job for other database maint. Creates a job for other database maint. TasksTasks

Re-orgRe-org

ShrinkShrink

Etc.Etc.

Management >> SQL Server Agent >> Management >> SQL Server Agent >> Jobs…Jobs…

Page 18: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Most Important Tabs…Most Important Tabs…

Backup and Backup and Tran. Log Tran. Log BackupBackup

Warning:Warning:disk files candisk files canget reallyget reallylarge…large…

Page 19: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Shrinking The DatabaseShrinking The Database

Right click on the dbRight click on the db

All tasksAll tasks

Shrink Shrink database…database…

Can be a big Can be a big performanceperformanceboostboost

Can be a bigCan be a bigbudget saver!budget saver!

Page 20: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Traditional ASP TuningTraditional ASP Tuning

Put your connection STRINGS in the Put your connection STRINGS in the global.asaglobal.asa

Application variables (not session)Application variables (not session)

NOT other variables unless you absolutely NOT other variables unless you absolutely need toneed to

NOT connections themselves – very badNOT connections themselves – very bad

Remove non-essential info from the Remove non-essential info from the global.asaglobal.asa

Beware SESSION versus ApplicationBeware SESSION versus Application

Page 21: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Global.asa(x) – Hacker’ s DreamGlobal.asa(x) – Hacker’ s Dream

Access = access to SQL serverAccess = access to SQL server

Consider how your application accesses Consider how your application accesses the db –the db –

Read-only UIDsRead-only UIDs

Move admin functions – don’t use global for their Move admin functions – don’t use global for their security info?security info?

Never multi-database use for an IDNever multi-database use for an ID

Live behind a physical firewall…Live behind a physical firewall…

Encrypt passwords/infoEncrypt passwords/info

Page 22: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Stored ProceduresStored Procedures

If you’re using a SELECT over and If you’re using a SELECT over and over – consider stored proceduresover – consider stored proceduresCompiled on the serverCompiled on the serverBased on data modelingBased on data modelingOptimizedOptimizedCan be encryptedCan be encryptedCentral adminCentral adminFights injectionFights injectionAll DB interaction should go through All DB interaction should go through an SP if possiblean SP if possible

Page 23: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

IndexingIndexing

EXTREMELY IMPORTANTEXTREMELY IMPORTANT

If you’re not indexing, you’re not If you’re not indexing, you’re not closeclose to full performanceto full performance

““Covered” queriesCovered” queries

Clustered versus not…Clustered versus not…

Examine your Examine your SELECTSELECT statements statements

Order in the Order in the SELECTSELECT

Page 24: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Using The Tuning ToolsUsing The Tuning Tools

Captures the SQL statements Captures the SQL statements by connectionby connection

RunsRunsremotelyremotely

Tune toTune tospecificspecificdatabasedatabase

Page 25: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Tuning…Tuning…

Let profiler work with your workloadLet profiler work with your workloadSave the workloadSave the workload

Play it back through the Play it back through the performance wizardperformance wizard

Watch the cursors (resources)Watch the cursors (resources)

Watch the open connections (memory)Watch the open connections (memory)

Watch the DB growthWatch the DB growthAuto-grow is nice, but…Auto-grow is nice, but…

……Out of disk space!Out of disk space!

Page 26: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Index Tuning WizardIndex Tuning Wizard

Use the Profiler toolUse the Profiler tool

Capture activity to a fileCapture activity to a file

Re-run that Re-run that activity through activity through the profiler index the profiler index tuning wizardtuning wizard

Implement, Implement, review or review or document the document the suggestionssuggestions

Page 27: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

More Performance IdeasMore Performance Ideas

Select count(*) – Table ScanSelect count(*) – Table Scan

Execution plansExecution plansHow to get themHow to get them

How to read themHow to read them

Query Analyzer >> ctrl-K or “results in Query Analyzer >> ctrl-K or “results in grid” drop down – Show Execution Plangrid” drop down – Show Execution Plan

Page 28: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Example PlanExample Plan

Page 29: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Additional Performance TipsAdditional Performance Tips

SET NOCOUNT ON in stored SET NOCOUNT ON in stored proceduresprocedures

Stops “10 rows affected…”Stops “10 rows affected…”

Query Analyzer – What operation is Query Analyzer – What operation is faster?faster?

Set up statements to compareSet up statements to compare

Run with showplanRun with showplan

Shows relative execution timesShows relative execution times

Page 30: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

More Performance TipsMore Performance Tips

Check for indexes on…Check for indexes on…JOINs JOINs

ViewsViews

Keep statistics updated (esp. after Keep statistics updated (esp. after mass updates) sp_updatestatsmass updates) sp_updatestats

Also “auto update statistics” at database Also “auto update statistics” at database levellevel

Page 31: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Script And Schema ControlScript And Schema Control

Save your scripts offlineSave your scripts offline

Don’t rely on the DBA for your Don’t rely on the DBA for your backupsbackups

Version control applied to scriptsVersion control applied to scripts

Script out the entire db after major Script out the entire db after major changeschanges

Page 32: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Scripting ObjectsScripting Objects

Page 33: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

SecuritySecurity

Don’t have all developers login with Don’t have all developers login with the same UIDthe same UID

Don’t use the DBA UIDs for Don’t use the DBA UIDs for developersdevelopers

Do keep UIDs confidentialDo keep UIDs confidential

Don’t let an application use a Don’t let an application use a developer/user/dba logindeveloper/user/dba login

Page 34: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

SQL InjectionSQL Injection

Can provide alarming accessCan provide alarming accessSelect * from myTable where lname=‘” Select * from myTable where lname=‘” request.form(“lastname”) & “’”request.form(“lastname”) & “’”Form input ’ or ‘a’=‘aForm input ’ or ‘a’=‘aResult: Result: select * from mytable where lname=‘’ or ‘a’=‘a’select * from mytable where lname=‘’ or ‘a’=‘a’

How to protect against itHow to protect against itStored proceduresStored proceduresWatching quotesWatching quotesControl statement generationControl statement generationParameter queriesParameter queries

Best Description: Best Description: Don’t let someone else write your code…Don’t let someone else write your code…- Steve Kass- Steve Kass

Page 35: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Other Points To ConsiderOther Points To Consider

Web site: session timeoutWeb site: session timeoutDefaults to 20 minutesDefaults to 20 minutes

Web site: HTTP keep-alivesWeb site: HTTP keep-alivesForces IIS to keep around information on Forces IIS to keep around information on the connection (memory)the connection (memory)

Cursors versus more simple SELECTsCursors versus more simple SELECTs

Storing summary information versus Storing summary information versus detail for later calculationdetail for later calculation

Move reporting support to a different Move reporting support to a different serverserver

Page 36: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Use Available ToolsUse Available Tools

Query AnalyzerQuery Analyzer

Enterprise Manager Enterprise Manager SQL AgentSQL Agent

Scripting EngineScripting Engine

Wizards!Wizards!

Profiler – use it as first line of Profiler – use it as first line of investigationinvestigation

Import/Export/Move Tools – but with Import/Export/Move Tools – but with cautioncaution

Page 37: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Online ResourcesOnline Resources

Sswug.org Sswug.org

SqlServerCentral.comSqlServerCentral.com

MSSQLserver.com MSSQLserver.com Sql-Server-Performance.comSql-Server-Performance.com

SQLSecurity.comSQLSecurity.com

SQLTeam.comSQLTeam.com

SQLJunkies.comSQLJunkies.com

Page 38: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Third-Party ToolsThird-Party Tools

Imceda – compressed/faster backupsImceda – compressed/faster backups

Idera – monitoring, management toolsIdera – monitoring, management tools

BMC – recovery toolsBMC – recovery tools

Sonasoft – automated disaster Sonasoft – automated disaster recoveryrecovery

Sql Power Tools – low-impact Sql Power Tools – low-impact monitoringmonitoring

Red-Gate – schema comparison, Red-Gate – schema comparison, synchronizationsynchronization

Page 39: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Q1:Q1: Overall satisfaction with the sessionOverall satisfaction with the session

Q2:Q2: Usefulness of the informationUsefulness of the information

Q3:Q3: Presenter’s knowledge of the subjectPresenter’s knowledge of the subject

Q4:Q4: Presenter’s presentation skillsPresenter’s presentation skills

Q5:Q5: Effectiveness of the presentationEffectiveness of the presentation

Please fill out a session evaluation on CommNetPlease fill out a session evaluation on CommNet

Page 40: DAT332 Database Administration For Developers Stephen Wynkoop SQL Server Worldwide User’s Group @sswug.org.

Questions…Questions…

?? ????

??????

????

???? ??

??

??

??

??

????

?? ??????

????

Stephen WynkoopStephen [email protected]@sswug.org

(be sure to grab a performance tips poster from(be sure to grab a performance tips poster fromThe SQL Server booth in the exhibit hall)The SQL Server booth in the exhibit hall)