Archive for the ‘DBMS’ Category

MS SQL Backup and Restore Strategies

Friday, May 21st, 2010

Introduction

One of the most significant benefits of running MS SQL is the database recovery model. Your company’s information is its life-blood, and there are myriad methods to backup and preserve it. We will be discussing some basic techniques to preserve your data in a simple and efficient manner. We will not delve into Mirroring, Transaction Log Shipping or Replication; merely the actual backup of individual databases. The backup operations are dependent on the type of backup recovery model used.

 The MS SQL default is Full recovery model, which is the model we will be discussing in this article as this is the most commonly used model. We will be using the MS AdventureWorks2008 database as our test database in this article. It is available for download from a Microsoft partner website AdventureWorks2008. Other backup strategies such as backing up full-text search catalogs and very large databases are not discussed in this article.

Types of Recovery Models

                Simple Recovery Model

                Full Recovery Model

                Bulk-Logged Recovery Model

Types of backups

                Full

                Differential

                Transaction Log

Creating a Full Backup

Creating a Full backup using T-SQL scripts

                 Using sqlcmd or SQL Query Analyzer in the SQL Query Management Studio tool, enter the following code to create a Full backup device and backup file:

USE MASTER
EXEC sp_addumpdevice ‘disk’, ‘AdventureWorks2008 Full Test’, ‘C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\Backup\AdventureWorks2008Full.dat’
USE MASTER
BACKUP DATABASE AdventureWorks2008 TO ‘AdventureWorks2008 Full Test’
WITH DESCRIPTION=‘Full Adventureworks 2008 Backup’ 

                Creating a Full Backup using SQL management Studio

  1. Highlight the database you wish to backup
  2. Right-click the database and select “Tasks”, “Back Up…”
  3. If you would like the backup to expire, enter an integer value.
    1. For general usage enter an integer between 7 and 30 days inclusive
  4. The default device is a disk file with a “bak” file extension.
    1. If you have an off-line or on-line backup repository specify this now.
  5. Click the <OK> button to create the backup.

Restoring a Full Recovery Model Backup Step-by-Step

Below we will give an example of a specific type of restore operation involving restoring the AdventureWorks2008 database using the Full recovery model from a Full backup, Differential backup and a transaction log backup.

Step one – Perform a tail-log backup

As mentioned above, immediately prior to our Restore operation we need to backup any uncommitted transactions that may still be in the log. From the SQL Query window in the Management Studio logged into the correct SQL Server instance or from a SQLCMD command prompt logged into the correct SQL Server instance type (Note the filename and pathname are user defined; just make sure the directory path exists):

BACKUP LOG AdventureWorks2008 TO
DISK=’c:\temp\AdventureWorks2008EmergencyLog.bak’ WITH NORECOVERY

Step two – Perform Full Database Restore

Next we need to restore the last good full backup. Make sure you use the NORECOVERY option. We cannot fully recover our database and allow users back in until we have applied all the files necessary to complete the Restore operation. Note also that since we have two files included in our Full backup we need to restore the second file (unless we are just initiated our backup job less than two weeks prior). In our example enter the following (referring to the files we created in our Backup Strategies session):

RESTORE DATABASE AdventureWorks2008 FROM “AdventureWorks2008 Full Test” WITH File = 1, NORECOVERY

Step three – Perform Differential Backup Restore

The next step is to restore the last good differential backup. For our example we can assume we have a single Differential backup file to restore. Our command should be as follows:

RESTORE DATABASE AdventureWorks2008 FROM “AdventureWorks2008 Diff Test” WITH NORECOVERY

Step four – Perform Log File Restore

 To restore the log file, run the following command:

RESTORE LOG AdventureWorks2008 FROM “AdventureWorks2008 Log1 Test” WITH NORECOVERY

Step five – Perform the Final Database Restore

The last SQL command verifies and activates the database for use:

RESTORE DATABASE AdventureWorks2008 WITH RECOVERY

Creating and scheduling automatic Backups

Using sqlcmd and the Task Scheduler for SQL Backups

We will need to schedule each of our backup jobs using the task scheduler. We will be running a single command, sqlcmd with the appropriate parameters. A typical command would look like the following:

Sqlcmd /S <server name\instance>/E /Q “EXEC sp_BackupAdventureWorksTest @backupType=’F'”

Caveat running sqlcmd: sqlcmd is particular about the syntax and text case. Make sure you use single-quotes around T-SQL string parameters and double-quotes around the T-SQL query command we are running. The stored procedure parameter text case must match the variable declaration exactly. This is a pain in the neck, but much worse if you make an error and have to track it down! Although you can use a hyphen (“-“) in front of the sqlcmd parameters, I have had better success using a “/” character. Watch out, because this may change in the future.

Scheduling our Task

Use the “Task Scheduler” Windows utility to create a backup schedule for your database. If you do not know how to configure, please refer to this MSDN knowledgebase articles Windows XP Tasks and Windows Vista Tasks.

Mertech Flex2SQL Vs. DAW CK: What are the differences?

Wednesday, March 24th, 2010

I still recall the time in 1997 when I presented the first SQL drivers; most DataFlex developers were skeptical. Their first question was “Does this even work?” Followed by “Why should we switch from Pervasive or DataFlex? It works for us and is the “right” fit for DataFlex.”

Now, 12 years later, it’s clear that we have come a long way. No one questions any connectivity from DataFlex to SQL, but there is a caveat.  The driver does a lot of work and it must not be assumed that just because Mertech’s solution works, others will work the same way.  So what are the differences?  Are all drivers the same?  While we have answered these questions many times, we have never formally published it.

So here they are:

  1. OLEDB vs ODBC: Mertech uses OLEDB while DAW CK uses CLI
    Microsoft has three different APIs to connect to SQL Server. DBLIB, CLI and OLEDB. DBLIB was a layover from the old Sybase engine that MS acquired in the early ’90s that turned into MS SQL Server. CLI was created to provide ODBC access. And then OLEDB was created to replace CLI and provide a universal API to access both SQL as well transaction data.  Our first driver, v2.x was developed using DBLIB but then in 1999, we wrote the driver to use OLEDB API.   Our reason for choosing OLEDB over CLI was 1) performance 2) better record access for ISAM style databases 3) Connection pooling and better connection handling, and 4) because MS told us 10 years ago that they were internally shifting everything to OLEDB and ADO (built on top of OLEDB) and away from CLI. The DAW CK uses CLI, the ODBC interface which provides access to SQL server through an ODBC driver.  Our testing had showed that for a mix of set and record based fetches, we achieved a 15% built-in performance advantage for just choosing the right API.
  2. Performance
    Mertech’s drivers overwhelmingly outperform DAW’s drivers in a real world environment. Our v9 drivers were two times faster than DAW’s. The new v10 drivers are 3-5 times faster depending on Indexes being used, etc. This was again highlighted yesterday by a customer in the U.S. who was looking to adopt SQL. They ran the same process on two different machines connecting to the SQL with everything exactly the same except the driver.  I asked him what performance difference he saw and he said, “When the process running with Mertech’s driver had finished, the process running DAW CK was at 14%.” This is consistent with the findings of other developers as well as with our own internal testing. The comparison was done with our latest v10 driver and the latest DAW v5 CK.  Needless to say, we have a new ISV partner! This same story was told to me by a Swedish ISV at EDUC in Seefeld. They had deployed five sites with DAW CK, after moving from Pervasive, faced serious performance problems and are now looking to deploy with Mertech.
  3. Intelligent Data Fetching
    With our v10 drivers we introduced an intelligent data fetching mechanism that determines the most optimal number of rows and columns to fetch. This highly complex process significantly boosts performance while reducing network traffic. For example, if you have a selection list based on an index with two segments in a table with 40 columns and say one million rows, we will fetch only the two columns and only the rows you see on the screen. Not only does it result in reduced network traffic but also makes it work with good speed over slow WAN connections.
  4. Recovering from Connection Problems
    Over the years, we have never received any complaints about dropped connections or drivers misbehaving due to a connection hiccup (very short interruption).  That is largely due to how OLEDB handles connections and connection pooling. It is more robust at correcting itself from connection glitches than CLI.  However, if there was a network outage (somebody pulls the network cable), DataFlex programs would just crash if the driver is unable to handle the effects of losing connection to the server. Our goal for  v10 drivers was “high availability, fastest performance.” In keeping with that goal, we added robust handling of recovering from connection issues. If a connection drops, the driver will retry and if it fails to reconnect to the server (for instance, if the server is down), it will generate an informative  message and ask the user to retry instead of blowing up. Once the connection with the server is established, all transactions and all operations will continue the way they were intended without any loss of data or application crash. The DAW CK doesn’t provide the ability to recover from connection hiccups.
  5. Support for SQL Server Failover and Mirroring
    Database Mirroring is an easy way in MS SQL Server to create a high-availability database environment where if the main server goes down, another server picks up the load without the user seeing a difference. A very good demo was given by Gandalf at DISD and then again at EDUC where in the middle of a find operation, he shuts down the server and then when he presses find again the next record is found. In today’s always-on 24X7 business operations this is a MUST HAVE feature. With version 10 drivers, this feature is now available. No such feature is offered in the DAW CK.
  6. Ability to Generate SQL Scripts to Create/Drop Tables and Indexes
    One thing we have learned is that some DBAs are pretty strict about who makes changes to their database. They want to see how tables are created and dropped, and for good reason. These operations can lead to serious performance issues.  In Flex2SQL, we have always provided a way to create empty tables and indexes (or drop them) so that users could give those scripts to the DBA to work with. This also creates a big advantage for software vendors who can use the scripts to create empty tables during the installation process. No such feature is available with DAW CK.
  7. No More Structure Caching!
    We started doing structure caching (.TD files) back in 1999 after we saw that opening the .INT file is way too slow in DataFlex. With the .TD files, we could store the table structure in a local binary file rather than read it every time from the server. While this led to faster opens, it also resulted in “stale cache” where a customer would change the table structure on the server but it didn’t delete the local .TD files, which would lead to errors when the application was run.  This was a big support headache for us and to some of our customers.  We worked on optimizing how we query tables and now have completely eliminated .TD files.  The DAW CK still uses structure caching via a .CCH file.
  8. Faster Switching Between Application Views
    While working on improving opens without caching, we also decided to keep all opened files in memory so that when you switch between programs or views and open and close multiple files, the  structures  would be available and open will be instant. The end-users can see perceptible improvement in performance while navigating between program components. This function will outperform DAW CK and even the native DataFlex database.
  9. Support for LOB Columns
    With our v10 drivers, we now provide the ability to store large amount of data in LOB columns and fetch them through new commands. Now you can store a large chunk of data and retrieve it in pieces to bypass the 16K field limit in DataFlex.
  10. Support for SQL Server Partitioning
    Flex2SQL allows you to  partition your index and data tables into separate filegroups which in turn can reside on separate physical drives. This is an important feature that speeds up SQL Server performance when there is a large number of concurrent users.


    Multiple embedded SQL statements

    Tuesday, June 30th, 2009

    While complex embedded SQL statements will let you do a lot of work in one statement, it is sometimes necessary to nest SQL statements to get the job done.

    Using cursor handling lets you easily nest multiple embedded SQL statements.

    Consider the scenario where we want to iterate through all orders with a specific status, and for each order we want to calculate the total of that order.

    Of course, in this case it would be pretty easy to write one SQL statement that returns everything, but for the example we will use two nesting calls:


    // Example
    Use Mertech.inc
    Integer iCursor1 iCursor2
    Integer iCustomer
    String sName
    Number nOrder nTotal

    login 'Server' 'User' 'Passwd' 'Driver'

    set_database_name to 'DataBase'
    sql_use_database 'DataBase'

    Sql_Open_Cursor_Stmt to iCursor1
    Sql_Set_Stmt to "select customer_number,name from customer order where status = 'Y' by customer_number"
    Sql_Prepare_Stmt
    Sql_Execute_Stmt

    Repeat
    // Back to the main cursor
    Sql_Set_Current_Cursor_Stmt to iCursor1
    Sql_Fetch_Next_Row into iCustomer sName
    If (Found) Begin
    // Open a new cursor to the new stmt
    Sql_Open_Cursor_Stmt to iCursor2

    Sql_Set_Stmt To ("Select sum(order_total) from Orderhea where customer_number = " + trim(iCustomer))
    Sql_Prepare_Stmt
    Sql_Execute_Stmt
    Sql_Fetch_Next_Row into nOrder
    If (Not(Found)) Move 0 to nTotal

    Sql_Close_Cursor_Stmt iCursor2

    // nTotal has the Order total for this current customer
    // Your code here
    Indicate Found True
    End
    Until (Not(Found))
    // End Example

    The keys in the example are the calls to Sql_Open_Cursor_Stmt and Sql_Set_Current_Cursor_Stmt which allows you to switch between two open recordsets.

    Using Native Client with v10 Flex2SQL MS SQL Server

    Tuesday, April 21st, 2009

    The v10 SQL Server drivers exclusively use the SQL Server native client libraries.  This is the approach MS is taking and is evident by the fact that almost all new features in SQL 2008 can only be accessed through the native client.

    However, as with all things Microsoft, things are not always that simple! In our testing, we have seen anamolies that shouldn’t be there but are. For example, in previous version, we could be sure that older versions of the OLEDB providers would work with newer versions of SQL Server. Simple right?  Not so with the native clients.

    When  it is necessary to match up the various versions of  native client versions with the versions of SQL Server. Here is what we recommend:

    If you are connecting to  SQL Server 2005,  you can use Native Client 2005 or Native Client 2008 installed.
    If you are connecting to  SQL Server 2008,  you MUST use Native Client 2008.  You cannot use the Native Client 2005.

    So what does this mean? If your customer has upgraded to SQL Server 2008 without informing you and now are experiencing “weird” behavior  with your application, tell them to make sure they install the Native Client for SQL Server 2008.  And yes, Microsoft wants you to intsall the native client because it is too much for them to just provide a single client connection library like MySQL or PostgreSQL!

    The Flex2SQL v10 installation comes with a native client installation, for your convenience.

    Resources on Oracle’s Best Practices

    Tuesday, March 31st, 2009

    Oracle has created a site showing best practices for high availability.

    (more…)