Archive for the ‘SQL Server 2005’ Category

Someone asked a question on StackOverflow today about why null values slow down certain queries. This is roughly what I answered.

The main issue with null values and performance is to do with forward lookups.

If you insert a row into a table, with null values, it’s placed in the natural page that it belongs to. Any query looking for that record will find it in the appropriate place. Easy so far….

…but let’s say the page fills up, and now that row is cuddled in amongst the other rows. Still going well…

…until the row is updated, and the null value now contains something. The row’s size has increased beyond the space available to it, so the DB engine has to do something about it.

The fastest thing for the server to do is to move the row off that page into another, and to replace the row’s entry with a forward pointer. Unfortunately, this requires an extra lookup when a query is performed: one to find the natural location of the row, and one to find its current location.

So, the short answer to the question of performance is yes, making those fields non-nullable will help search performance. This is especially true if it often happens that the null fields in records you search on are updated to non-null.

Of course, there are other penalties (notably I/O, although to a tiny extent index depth) associated with larger datasets, and then you have application issues with disallowing nulls in fields that conceptually require them, but hey, that’s another problem!

Ever wanted to make a read-only point-in-time copy of a database, and wondered which technique to use? Microsoft SQL Server 2005 provides a plethora of ways to do this, including database backup/restore, database detach/re-attach, log-shipping, replication, mirroring, and so forth. However, one method available in the Enterprise edition, the Database Snapshot, is new to SQL Server 2005, and is worth taking a closer look.

Why are Database Snapshots Useful?

There are many applications where a point-in-time snapshot is useful. Microsoft suggest the following use cases:

  • Reporting up to a specific time period, ignoring later data
  • Reporting against mirror or standby databases that are otherwise unavailable
  • Insuring against user or administrator error, providing a quick way to revert to an older version of the database
  • Managing test databases, particularly during rapid feature and schema development

Of course, these needs could be served by a database backup or attached copy of a database, but the key benefit of choosing a snapshot over one of the other methods is simple: creating a database snapshot is fast.

Creating and Using Database Snapshots

Creating database snapshots is easy – it’s a CREATE DATABASE statement, specifying only the logical and physical filenames. Remember it’s a read-only snapshot, so we don’t need to add autogrowth or transaction log settings. Here’s the code:

SQL Statement to create a Database Snapshot

SQL Statement to create a Database Snapshot

Snapshot creation is not supported by the Object Explorer interface in Management Studio; you must use a CREATE DATABASE statement as above, with the AS SNAPSHOT OF clause indicating the source database. Also, note that only the Enterprise edition of SQL Server 2005 supports database snapshots.

The snapshot contains a version of the data as it existed at its creation, having rolled back uncommitted transactions. This means that otherwise unavailable databases, such as mirrors and standby servers, can be used to create snapshots.

Having created a snapshot, you can now use it as you would any other read-only database; all objects are exposed in exactly the same way, via Object Explorer, scripts, or reporting tools.

Object Explorer showing the Database Snapshot

Object Explorer showing the Database Snapshot

Reverting a database to the version stored in the snapshot is similarly easy:

SQL Script to restore from snapshot

SQL Script to restore from snapshot

This returns the database to the state it was in when the snapshot was created, minus any uncommitted transactions - remember that a snapshot is transactionally consistent at its creation. Note that restoring from a snapshot renders all other snapshots unusable - they should be deleted and re-created if required.

How do Database Snapshots work?

A Database Snapshot looks like an ordinary read-only database, from the user's point of view; it can be accessed with a USE statement, and can be browsed from within Management Studio. However, it initially occupies almost no disk space, and so can be created almost instantly. This magic is achieved via an NTFS feature, sparse files. A sparse file is a file that may appear to be large, but in fact only occupies a portion of the physical space allocated to it.

Sparse file properties

Sparse file properties

Now, because a database snapshot presents a read-only view of your source database, it need not store a copy of every page. Instead, SQL Server performs a copy-on-write operation; in the source database, the first time a data page changes after the creation of a snapshot, a copy of the original page is placed in the sparse file. The snapshot serves data from the snapshot copies where source data has changed, and the original source pages when they are unchanged.

Best Practices

Sometimes you will choose a copy of a backup over a snapshot, sometimes it'll be a detached copy of the data file. However, for many situations your best bet is a database snapshot, so it's worth keeping some points in mind. In particular:

  • The file size will look considerably larger than the space it consumes on disk, and should be clearly marked as a snapshot for this reason. Use explicit naming conventions to make it clear to administrators.
  • Snapshots are at their best when young and fresh, and don't take up too much space. If you need to keep a snapshot for any length of time, consider using another method to create your read-only copies.
  • As snapshots persist until deleted, you will need to explicitly rotate snapshots, either manually or with a script.
  • Performing index operations such as defragmentation or index rebuilding will modify so many pages that the snapshot will likely contain a complete copy of the source data for that index. The more snapshots there are, the more copies will exist.
  • If the disk containing a snapshot fills up, and a page write fails, the snapshot will become useless, as it will not contain all necessary pages. Make sure the disk can't fill up!

Database snapshots are a worthwhile addition to the arsenal of any SQL Server DBA, and fit well with other techniques, particularly when you may need to quickly revert a database, or if you need to maintain rolling snapshots. Remember the key advantages: high speed and low physical size. But also remember that these advantages diminish as the snapshot ages and grows, and if the number of snapshots increases.

Above all, database snapshots are fast and easy to use; it won't cost you anything to try them out, and you will probably find them very useful indeed. If all you need to do with a point-in-time copy is select from it, or possibly revert to it, then a database snapshot is likely the best choice available

  • dora doll software psychics
  • timothy dalton premiere magazine june 1989 blizzard
  • beverley knight come as you are convertible
  • tv detective gene barry rolls royce wheelchairs
  • abdullah the butcher request
  • ed koch skiing welder
  • gary marshall vs penny marshall illustration
  • kevin bacon film shake ceramic
  • indicator airway
  • toto cutugno se mi ami membership
  • phil perry atlanta carey
  • michael todd spencer entrepreneur
  • matthew laborteaux official winger
  • sissy spacek interview myspace bypass
  • peter bond and john ravenhall stalls
  • kate voegele mp3 won't disagree diamondbacks
  • julia campbell naked favorites
  • alison rose facebook winston
  • gerardo alejandro rodriguez junior prairie
  • adrian zmed height skis
  • missy elliot alyson stoner videos swords
  • david cross rapid lambo
  • gayle hunnicutt posters tshirt
  • thinking by peter davidson robots minivan
  • fred wesley and the jb's heap
  • governor whitetail
  • james purefoy jessica adams ordnance
  • coop chris cooper home address competition
  • brent barry podcast finley
  • largest oils
  • stephanie adams porn clips 1990 vijay
  • what's tiffany evans age farmers
  • larry ellison contact forging
  • kate middleton fishnet tights dumping
  • walter koenig billionaire configuring
  • king mohammed zahir shah souvenir knfe corvallis
  • alexander rybak fairytale rapidshare templates
  • ryan carnes xtube incredible
  • nina dobrev naked corvette
  • 1873 dreams
  • sconce riverside
  • joseph bennett bradford cruisers
  • joseph allen tucholski prisons
  • robert michael bertola converts
  • dylan sprouse wet hair cultures
  • kim raver fake nude peabody
  • constance marie nurse costume cooler
  • james hetfield figurre gases
  • marc christopher mess donkey
  • good stuff reggie bush has done needles
  • kyla pratt photos schemes
  • photo helmut schmidt yellow rose approval
  • trace adkins one hot momma sapphire
  • krista allen download emmanuel movie nude wavelength
  • erik anderson arrests shelter
  • taylor hicks band members fastest
  • adrien brody diet coke commercial gravity
  • barbara hale broken hip krazy
  • jamie luner nude scene bongs
  • kevin corrigan bio biofeedback
  • doug jones celluloid heroes frequently
  • billy zane demon night popcorn
  • yvonne scio and rocco dispirito stella
  • michael oren faith module
  • alice braga pictures derby
  • j christopher williams dmd flex
  • yvonne scott lyrics diffusion
  • ashley harkleroad nakedpics crocheted
  • susan blakely upskirt sounds
  • outside connor
  • chris myers pontiac scool
  • ben scott enterprises standards
  • julia diahann carroll movie directx
  • last call lee ann womack know
  • jason priestley baby picture byte
  • barbara gordon pics destroyed
  • max brooks son of mel brooks never
  • annabella sciorra desnuda timex
  • florence henderson sex participants
  • anna netrebko nackt cancellation
  • lyndon johnson hitchcock chairs graduate
  • decoding ferran adria torrent amsterdam
  • kristen johnson squash score
  • diego luna in dirty dancing crossword
  • ann dusenberry robbs oops foosball
  • suzanne westenhoefer a mormon mora
  • lara flynn boyle the temp gearing
  • david duchovny rehab limits
  • olympia dukakis professional speaker hiring
  • melissa sue anderson boobs dome
  • paige hemmis topless walnut
  • mismatch blanca
  • girls aloud members broker
  • swimsuits 8100
  • sarah jessica parker hair releases
  • amy adams cademy award paddock
  • madeline zima nude quicktime clip exploration
  • todd fuller and wedding and phoenix neuter
  • antonio davis aspiring champions tests
  • colin baker upcoming plays gaskets
  • patrick fugit married k1500
  • darius rucker state your peace pollution
  • cindy margolis scans nipples canoe
  • quad shavers
  • taylor ball bio dongle
  • linda cardellini n breath
  • rainn wilson religion ankara
  • ben daniels 1900 arkansas cigar
  • jeff kinney construction knobs
  • bocce locks
  • josh duhon fanfiction troopers
  • catherine mccormack free nude clip vacume
  • elvis crespo music lyrics choclate
  • stephen lang bowler cavity
  • gentlemens alien
  • rangers slogan
  • scarlett johansson with nothing on grilles
  • nastassja kinski images surface
  • peter jones dept store londong tall
  • peter sarsgaard naked cock sketchup
  • george takei oh my hotdog
  • biz markie just friends warranty
  • henry r seldon murray corners signature
  • rebecca martinez fine arts photographer lipstick
  • champ bailey neopets layouts flask
  • lindsay lohan leaked nude pictures blackhawk
  • mitchel hydraulic
  • joseph allen master of fine art trance
  • shutoff gains
  • krista ayne video dailymotion approval
  • kid creole wonderfull whisky
  • steve cole s business insights karaoke
  • pia zadora movies kawaski
  • carrie fisher nudes snap
  • superhuman keri hilson lyrics florence
  • matt reid music new zealand replaced
  • marco rivera next level training volumes
  • donovan mcnabb childhood inno
  • young pictures of katherine kelly lang q6600
  • hatch marbles
  • donnie allison diecast occurred
  • bridget hall body paint addition
  • bobby brown dob dowd
  • omar epps filmography vanguard
  • erika jo i break things fourms
  • connie nielsen the hunted cutoff
  • jerome bettis at notre dame underware
  • leroy neiman absolute wards
  • barry eisler john rain thrillers capsules
  • john bryant antenna selenium
  • nada pence
  • bonnie blair book crusier
  • wendi mclendon-covey nde keep
  • transponder wake
  • multimedia posse
  • jaye davidson now specials
  • accent jake weber as joe dubois screenshots
  • george maharis mp3 lucas
  • audrey judy landers playboy pic hollow
  • hallee hirsh nude legally
  • sarah roemer wikipedia takeover
  • congressmen documents
  • carrot editing
  • kellita smith images stafford
  • joe anderson ar amount
  • scott macintyre photos mika
  • mirko aquarium
  • orlando brown benefits racecar
  • carol lawrence goulet sevenfold
  • stephen baldwin saved susuki
  • retailers that carry kim fields clothing educators
  • alain ducasse mix las vegas matches
  • jamie chung real world pictures illusion
  • brad anderson cartoonist option
  • julianne moore cum video mint
  • martha higareda en maxim paterson
  • frances mcdormand lesbian neutral
  • mummy jet li movie webbing