Supporting SQLserver

Preview:

DESCRIPTION

 

Citation preview

Supporting SQL Server – When YouSupporting SQL Server – When You’’d d Really Rather NotReally Rather NotDon JonesDon JonesConcentratedTech.com

Pre-requisites for this presentation:

1) Strong understanding of basic Windows administration

Level: Advanced

This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it

within your own organization however you like.

For more information on our company, including information on private classes and upcoming conference appearances, please

visit our Web site, www.ConcentratedTech.com.

For links to newly-posted decks, follow us on Twitter:@concentrateddon or @concentratdgreg

This work is copyright ©Concentrated Technology, LLC

About the InstructorAbout the Instructor

Don Jones Contributing Editor,

technetmagazine.com IT author, consultant, and speaker Co-founder of Concentrated Technology Seven-time recipient of Microsoft’s Most

Valuable Professional (MVP) Award Author and Editor-in-Chief for Realtime

Publishers Trainer for www.CBTNuggets.com

About this SessionAbout this Session

Primarily demo and how-to… not so much with the slides

Most queries I run I will save (remind me) – I’ll make these available on my Web site (see slide at end of deck) for you to download, along with this deck

About YouAbout You

A Windows admin stuck managing one or more SQL Server installs because you’re “the Microsoft guy/gal”

Not particularly interested in SQL Server Just stuck running it

SUPPORTING SQL SERVERSUPPORTING SQL SERVERPart 1

How SQL Server WorksHow SQL Server Works

Database is a logical+physical structure – think of it as a container

Contains one or more tables, which represent entities that an application works with

Tables consist of columns and rows – kinda like an Excel spreadsheet

Physical StoragePhysical Storage

SQL stores data in database files (MDF, NDF)

Organized into 8KB pages, which hold 1+ rows of data each

An entire row must always fit on a page; large data chunks may be spread across multiple pages

FilestreamsFilestreams

New in SQL2008: Ability to store large data in normal files outside the database, and only keep a pointer in the database

Shares the files via a normal file share Impacts some features such as

database mirroring and certain forms of replication

Querying DataQuerying Data

SQL Server loads needed pages into memory to access their data

Uses a query optimizer to analyze incoming queries and decide the best way to get the needed data

Optimizer relies on internal statistics that tell it the physical condition of the data (fragmentation level, etc)

Changing DataChanging Data

SQL Server loads affected pages into RAM

Changes pages in RAM and logs the change to a transaction log (LDF file)

Doesn’t write the affected pages back to disk immediately

Log serves as a form of bakcup

Committing DataCommitting Data

When SQL writes affected pages back to disk, it “marks” them as “committed” in the log

It does not clear the from the log

Recovery ModeRecovery Mode

SQL Server automatically starts in recovery mode

It checks to see if any uncommitted transactions are in any logs

If it finds them, it re-runs the transactions and commits them immediately

This deals with “we had uncommitted data in RAM and the server crashed”

Backup ModelBackup Model

Full Backup: All of the data. Clears (‘truncates’) committed transactions from the log – because now they’re safe

Incremental: Everything since the last full or incremental

Log: Backs up just the log file

Backup ExampleBackup Example

1am: Full backup

9am: Incremental

10am: Log 11am: Log 11:05: CRASH

Restore the full, the incremental, both logs

Will lose everything from 11:00-11:05

Log backups are VERY fast

TipsTips

Keep logs on separate disks/volumes from the data

Backup to a file, and then grab the backup file to tape – or use an agent to backup directly to tape

SQL itself can also backup directly to tape for locally-attached tape drives

PerformancePerformance

When new objects (tables, etc) are created in the database, you can designate which file they go to

One MDF file per database, multiple NDF files

Spread files across disks to improve access times

Backup/restore the files as a unit by having all files in the same filegroup

IndexesIndexes

Help SQL Server find sorted data faster Each table can have one clustered index

– physically stores data in the desired order

Can have 1+ nonclustered indexes – pointers to the actual physical data

Indexes are also stored on 8KB pages

Index Fragmentation (Page Splits)Index Fragmentation (Page Splits)

Happens when a page is full, and SQL needs to insert data “in between” existing data

The page is split: Half the data is copied to a new page at the end of the data file, making room for the new data

Results in index fragmentation

Index MaintenanceIndex Maintenance

Periodically rebuild/reorganize indexes Can specify amount of free space to

leave – leaves room for growth w/out splits

Allow SQL to auto-update statistics – helps it understand how useful each index is on a continuing basis

Index TricksIndex Tricks

An index can contain more than one column

A covering index is one which contains all the data needed to satisfy a query – without having to go to the actual data

Non-Covering IndexNon-Covering Index

SELECT Name,ID FROM Users WHERE Name LIKE ‘D%’

ID Name

4 Erin

5 Don

6 Dave

Dave

Don

Erin

CICINCINCI

Covering IndexCovering Index

SELECT Name FROM Users WHERE Name LIKE ‘D%’

ID Name

4 Erin

5 Don

6 Dave

Dave

Don

Erin

CICINCINCI

Index DownsidesIndex Downsides

Indexes improve lookup speeds, but diminish write speeds

Indexes have to be updated Always a compromise between read and

write performance

SQL Index WizardsSQL Index Wizards

Help analyze database usage and suggest useful indexes that can be added

Also suggests indexes that can be removed

Ideal when used with production-quality traffic

Getting SQL TrafficGetting SQL Traffic

SQL Profiler lets you capture traffic to a file or database

Run Profiler, and capture to, a different machine than the one you are profiling

Great way to troubleshoot and to gather “real” traffic for tuning wizards

Understanding Query Execution Understanding Query Execution PlansPlans

A plan is generated by the Query Optimizer and is a game plan for how to conduct the query

Specifies what indexes will be used, etc Can be viewed graphically or as XML;

XML plans can be saved and re-used (bad idea)

Hints and Reused PlansHints and Reused Plans

SQL will cache execution plans for a short time and re-use them

But the condition of the database is always changing – a good plan today may be a bad plan tomorrow

Applying manual “hints” or forcing plan re-use often results in good performance short-term, worse long-term

High Availability OptionsHigh Availability Options

Windows Clustering Database Mirroring (w/Witness) Transaction Log Shipping

InstancesInstances

SQL is designed to run multiple copies of itself in parallel

Called instances Each instance is a complete install of

the product One default instance, multiple named

instances SQL Express installs a named instance

by default

SecuritySecurity

Security is configured first as the instance level: Windows or Mixed authentication (determines type of logins allowed)

Logins grant access to the server Logins map to in-database users, which

grant access to databases

PermissionsPermissions

Permissions assigned via membership in roles (in-SQL groups, basically) or via direct permissions

Each DB object is owned by a particular user

Permissions chaining says SQL only checks permissions on an owner change

More FunMore Fun

Let’s look at database options and discuss what they do

Discuss user databases in SQL Express Discuss attaching/detaching databases Discuss misc. SQL Server components:

Reporting, Integration, Analysis

More ResourcesMore Resources

Drop off a business card (or your e-mail address) and Greg Shields and I will send you our decks and other class materials (scripts, samples, etc)

Visit http://ConcentratedTech.com for free, daily technical articles, Q&A, step-by-step guides, and much more

Want my “USB Go Pack?” It’s a 4GB USB key stuffed with video clips, white papers, step-by-step guides, articles, and much more – available only here for $60.

THE SQL LANGUAGETHE SQL LANGUAGEPART 2

How WeHow We’’ll Proceedll Proceed

Most of this will be done as demos. I’ll make the queries available as

downloads for you I’ll also detach the sample database and

include that for you– Note: Most of the sample data will be created

on-the-fly – makes it easier to see what data is coming from where

Slide at the end will have the address

Major Language ElementsMajor Language Elements

Queries SELECT: Get Data INSERT: Add Data UPDATE: Change Data DELETE: Remove

Data

Clauses WHERE ORDER BY TOP Aggregate

expressions GROUP BY / HAVING UNION JOIN

Thank You!Thank You!

Please feel free to pick up a card if you’d like copies of my session materials

I’ll be happy to take any last questions while I pack up

Please complete and submit an evaluation form for this and every session you attend!

This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it

within your own organization however you like.

For more information on our company, including information on private classes and upcoming conference appearances, please

visit our Web site, www.ConcentratedTech.com.

For links to newly-posted decks, follow us on Twitter:@concentrateddon or @concentratdgreg

This work is copyright ©Concentrated Technology, LLC