Monday, January 24, 2011

Logshipping Error: "Skipping log backup file" and "Could not apply log backup file"



This was a bad day for log shipping. I was on a leave for last two days and got my junior DBA screeming over logshipping latency as soon I stepped in.

DBA got many skip messages in logshipping as follows:

2011-01-24 21:50:11.17 Skipping log backup file 'j:\logshipping\Tuitionaffordable\Tuitionaffordable_20110123100410.trn' for secondary database 'Tuitionaffordable' because the file could not be verified.

Another interesting error was as follows:

2011-01-24 21:50:10.92 *** Error: Could not apply log backup file 'j:\logshipping\Tuitionaffordable\Tuitionaffordable_20110123100410.trn' to secondary database 'Tuitionaffordable'.(Microsoft.SqlServer.Management.LogShipping) ***
2011-01-24 21:50:10.92 *** Error: Cannot open backup device 'j:\logshipping\Tuitionaffordable\Tuitionaffordable_20110123100410.trn'. Operating system error 32(The process cannot access the file because it is being used by another process.).
RESTORE LOG is terminating abnormally.(.Net SqlClient Data Provider) ***

Let me discuss about these errors one by one.

Error 1 indicates that some transactional log file is missed in between. It just means that log chain is broken and you need to copy those logs manually to the secondary server.


Error 2 indicates that the log file is corrupt. Let's run following command to verify the log file.

RESTORE VERIFYONLY
FROM DISK = 'J:\logshipping\Tuitionaffordable\bak\Tuitionaffordable_20110123100410.trn'

Msg 3203, Level 16, State 1, Line 1
Read on "J:\logshipping\Tuitionaffordable\bak\Tuitionaffordable_20110123100410.trn" failed: 13(The data is invalid.)
Msg 3013, Level 16, State 1, Line 1
VERIFY DATABASE is terminating abnormally.

And this told that your log file is invalid. This happens when you use robocopy or xcopy to transfer file manually and your session is logged off. So get those files again from primary DB manually. I prefer to create a job and run it so that session logoff doesn't cause any problem.

Most of the readers of this blog also read following interview questions so linking them here:
Powershell Interview Questions and Answers
SQL Server DBA “Interview Questions And Answers”
SQL Azure Interview Questions and Answers Part - 1



Regards,
http://tuitionaffordable.webstarts.com

Sunday, January 16, 2011

Windows Azure Interview Questions and Answers


Custom Search
Want to prepare in ten minutes - read article Interview and Beyond in 10 Min

Qu1: What is Windows Azure?
Ans: Windows Azure is Microsoft Cloud Computing OS. Someone needs to worry about the h\w requirement, n\w requirement and the correct OS and much other stuff when he starts designing an application. Windows Azure will help the developer to get rid of these things and let him concentrate on main area which is nothing but automation and business needs.  
Qu2: What is fabric?
Ans: In the Windows Azure cloud fabric is nothing but a combination of many virtualized instances which run client application.
Qu3: What is the downtime for applications in case of any patching?
Ans: Windows Azure will have replicas for each and every application and provide zero downtime in case of any security patching.
Qu4: How many copies of data are maintained in Windows Azure?
Ans: Windows Azure provides you with three copies of data. This makes your application running on very reliable data.
Qu5: What is queue storage in the Windows Azure?
Ans: Queue storage gives you capability of sending the small data just as messages. Queue storage basically helps informing the task to the worker threads.


Most of the readers of this blog also read following interview questions so linking them here:
Powershell Interview Questions and Answers
SQL Server DBA “Interview Questions And Answers”
SQL Azure Interview Questions and Answers Part - 1


Regards,
http://tuitionaffordable.webstarts.com

Friday, January 14, 2011

MS SQL SERVER 2008 Fix Fragmented Index



Who Should Read This Blog: MS SQL Server DBA facing following issues:

1. MS SQL Server DBA - Index getting fragmented often.
2. MS SQL Server DBA - Index not being used by the queries properly.

I can see many DBAs asking the same questions again and again regarding index fragmentation. Most of them take a wrong path to fix this problem. Let's understand how index fragmentation happens and how to fix this.

Index fragmentation is of two types: Internal and External. Internal is not very harmful. Let's talk External now.

Before you start fixing index fragmentation, you should be able to identify whether the system is OLTP or OLAP. Please never go for high fill factor if you have an OLTP system and low fill factor if you have an OLAP system.

Let's Understand: If you go for high fill factor then this means you are allowing more data
to absorbe into the index pages. And when new data will come in the order, page split will
occur. So wait before you look for high fill factor. The same rule applies on Low fill
factor also. If your system is OLAP then never ever go for low fill factor as this will
unnecessarily consume the space on each page. So the key is to understand your system
before you understand the problem.

Reason of Index Fragmentation:

1. Autoshrink. Verify that autoshrink is not on. The sql server will cost high fragmentation in case it needs to shrink the data. I believe to confirm index defragmentation as soon as you ever need to shrink the DB.

2. Having less Fill factor on OLTP env can cause the more page splits. Please understand your system before you go for the fill factor and index managment. You can fix the fill factor with following command.

ALTER INDEX PK_Powershelltute ON [Tuitionaffordable].dbo.Powershelltute REBUILD WITH
(ONLINE=ON, MAXDOP=1, SORT_IN_TEMPDB=ON, FILLFACTOR=95)

3. Following is the query where you can identify the fill factor of your tables:

SELECT  tbl.name AS table_name,
        indx.name AS index_name,
        indx.fill_factor
FROM    sys.tables tbl
        JOIN
        sys.indexes indx
        ON tbl.object_id = indx.object_id
WHERE
        indx.type in (1,2)
ORDER BY
        tbl.name,
        indx.name


Tuesday, January 11, 2011

SQL SERVER 2008 R2 - Trasaction and Isolation Levels - "Cannot roll back Transaction. No transaction or savepoint of that name was found."



Who Should Read This Blog: DBA who wants to understan how Transaction works.

Let's run following command first:

1. SELECT @@TRANCOUNT
--Result 0

2. DBCC OPENTRAN()
--Result as follows
Transaction information for database 'Tuition'.
Oldest active transaction:
    SPID (server process ID): 54
    UID (user ID) : -1
    Name          : Affordable
    LSN           : (3073:52373:1)
    Start time    : Jan 11 2011  6:23:47:993PM
    SID           : 0x0105000000000005150000001ac6c4f30376dea8123bc97be8030000
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

So the difference between these two commands are as follows:

1. @@TRANCOUNT will tell you how many open transactions are there whereas DBCC OPENTRAN() tells only about the oldest trasaction.

2. @@TRANCOUNT will tell you the open transaction as soon as you run BEGIN TRAN but DBCC OPENTRAN() will tell some information only when any other command is run under BEGIN TRAN.

There is more to understand about the transactions and two popular ISOLATION Levels ie READ COMMITTED AND READ UNCOMMITTED:

Let's create a simple test table TestTran with a column server. Populate the table with any data.

Execute the commands as follows:

SELECT @@TRANCOUNT --0
DBCC OPENTRAN()
No active open transactions.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Let's run following command now and verify the difference between @@TRANCOUNT and DBCC
OPENTRAN()
BEGIN TRAN Tuition

Let's verify

SELECT @@TRANCOUNT --1
DBCC OPENTRAN()
No active open transactions.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

I want to add one popular error in the blog which most of the DBAs would get when they try to rollback the transaction. Use follwoing code to see the error:

BEGIN TRAN Affordable

ROLLBACK tran affordable
Msg 6401, Level 16, State 1, Line 1
Cannot roll back Affordable. No transaction or savepoint of that name was found.


Try this now:

ROLLBACK tran Affordable
Command(s) completed successfully.

Now this is the time to go ahead with understanding how nested transactions behave:


Custom Search
Run following command:

BEGIN TRAN Affordable1
UPDATE TestTran
SET Server= 'Affordable1'


BEGIN TRAN Affordable2
UPDATE TestTran
SET Server= 'Affordable2'


BEGIN TRAN Affordable3
UPDATE TestTran
SET Server= 'Affordable3'


Now we shall try to rollback the last trans Affordable3. We get following error msg:

Msg 6401, Level 16, State 1, Line 1
Cannot roll back Afford. No transaction or savepoint of that name was found.


You can commit any internal transaction but cann't rollback. Now if you rollback the transaction the changes affected by the  internal transaction would go away.

Let's also understand READ COMMITTED and READ UNCOMMITTED Transaction Isolation Level.

Let's run following command on one connection.

BEGIN TRAN Affordable1
UPDATE TestTran
SET Server= 'Affordable1'


Run following command on another session

SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SELECT *
FROM TestTran

The session will hung as you asked the sql server to show you only committed data.

Now run following command on another session

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT *
FROM TestTran

This data is nothing but the "dirty read" because if you rollback the transaction in the previous session then the data would change.




Monday, January 10, 2011

Cost Analysis Among EXISTS (SELECT 1), EXISTS (SELECT COUNT(1)), EXISTS (SELECT *) AND EXISTS (SELECT TOP 1)



Let's understand how 'SELECT 1', 'SELECT COUNT(1)', 'SELECT *' and 'SELECT TOP 1' would behave when they are called in the WHERE EXISTS clause. Let's look at following queries and find out which one will cost least.

Query 1:
SELECT a.ProductId, a.ProductName
FROM Product.Catalog AS a
WHERE EXISTS
(SELECT *
FROM Product.Products AS b
WHERE a.ProductId = b.ProductId
AND a.ProductName = 'RAM');
GO

Query 2:
SELECT a.ProductId, a.ProductName
FROM Product.Catalog AS a
WHERE EXISTS
(SELECT 1
FROM Product.Products AS b
WHERE a.ProductId = b.ProductId
AND a.ProductName = 'RAM');
GO

Query 3:
SELECT a.ProductId, a.ProductName
FROM Product.Catalog AS a
WHERE EXISTS
(SELECT TOP 1
FROM Product.Products AS b
WHERE a.ProductId = b.ProductId
AND a.ProductName = 'RAM');
GO

Custom Search
Query 4:
SELECT a.ProductId, a.ProductName
FROM Product.Catalog AS a
WHERE EXISTS
(SELECT COUNT(1)
FROM Product.Products AS b
WHERE a.ProductId = b.ProductId
AND a.ProductName = 'RAM');
GO

There is a grave misconception among DBAs and I saw most of them choosing query 3 and 4. The clue is that work under 'WHERE EXISTS' completes as soon as the first record is returned. Query 1 and 2 are same here. Now Query 3 is more costlier than the first two and query 4 is most costly. Also query 4 is nowhere equivalent to others as this will always execute even this returns 0.

We can arrange the queries Cost wise: query 1 = query 2 < query3 < query 4


Thursday, January 6, 2011

Comparison between Litespeed Backup and Native Backup Compression



Following article would help you understand the benefit of taking backup using litespeed. The backup file size with litespeed is far lighter than the native backup with compression. This is a well known fact that the compression of any backup depends on the data in the DB. This is why the ratio of this analysis can vary in different scenarios. I tested this with 5 different kind of DBs and always got better results with Litespeed. Let’s walk through:
Database size- 3GB
Comparison between native backup and litespeed backup-

--Native Backup
BACKUP DATABASE Tower
TO DISK= 'D:\towerbknative.bak'
WITH COMPRESSION


Custom Search
--Litespeed Backup
EXEC master.dbo.xp_backup_database
@database='Tower'
   , @filename='D:\towerbknative.bak'
   , @init=1
   , @compressionlevel = 8
   , @encryptionkey='Password' 

--Native Restore
RESTORE DATABASE Tower
FROM DISK= 'D:\towerbknative.bak'

--Litespeed Restore
EXEC master.dbo.xp_restore_database
@database='Tower'  
   , @filename='D:\towerbknative.bak'
   , @encryptionkey='Password'
Considering the Newbies in litespeed, let me explain how to take the litespeed backup at Litespeed console.
Step 1- Go to Backup Manager wizard in LiteSpeed consol
Step 2- Select Backup type as Fast Compression-















Step 3- Next > Select the backup destination>Next Fast Compression type- Select Self-containded backup sets.













Step 4- Next>Next> Compression :Select Compression level as 8 which is highest level of compression.
Give your encryption password











Step 5- Finish. Your backup is created in the destination.
In my case the backup was of 3GB and my LiteSpeed backup size after fast Compression, is only 753KB.
You can see the status of your backup job in the job manager window by clicking Cntr+5:

Same way you can restore the DB using Litespeed Restore Backup Wizard in Backup Manager.-


Regards,
http://tuitionaffordable.webstarts.com

Sql Job Failure - Unable to Determine if the Owner (PROD\tuitionaffordable) of Job has Server Access "error code 0x2"



Unable to determine if the owner (PROD\tuitionaffordable) of job  has server access error code 0x2

Got following error for some job which runs every day without any issue-
Error 
Date                      1/6/2011 2:25:00 PM
Log                         Job History (Tuitionaffordable-REPLDB-Tuition-28)

Step ID                 0
Server                   Tuitionaffordable
Job Name                            Tuitionaffordable-REPLDB-Tuition-28
Step Name                         (Job outcome)
Duration                              00:00:09
Sql Severity                        0
Sql Message ID                 0
Operator Emailed                           
Operator Net sent                          
Operator Paged                               
Retries Attempted                          0

Message
The job failed.  Unable to determine if the owner (PROD\tuitionaffordable) of job Tuitionaffordable-REPLDB-Tuition-28 has server access (reason: Could not obtain information about Windows NT group/user 'PROD\tuitionaffordable', error code 0x2. [SQLSTATE 42000] (Error 15404)).

Let's Understand: The error is thrown because the account PROD\tuitionaffordable is either disabled or doesn't have access to the shared folders where the job tries to save/access files.

Solution: Change the owner of the job to some other account which has access to the shared folders where the job tries to save/access files.



Another Problem: What if there are 50 jobs with the same owner id and I want all of them to change.

Solution:
UPDATE sysjobs
SET    owner_sid = 0x01
FROM   sysjobs
INNER  JOIN  sysjobhistory hist
ON     hist.job_id = sysjobs.job_id
AND    hist.run_status = 0
AND    hist.message LIKE '%PROD\tuitionaffordable%'

Note: When the owner of the job is sa then the job runs under the account under which sql agent runs.



Custom Search






Wednesday, January 5, 2011

MSSQL Replication Error: "The process could not connect to Subscriber"



Error Message: "The process could not connect to Subscriber"

MSSQL Error: 45000 Severity: 16 State: 1 ALERT: REPLICATION LATENCY BETWEEN PUBLISHER AND DISTRIBUTOR IS MORE THAN THE THRESHOLD VALUE OF 3000 SECONDS FOR THE PUBLICATION:TuitionaffordablePublication SUBSCRIBER:Tuitionaffordable || SUBSCRIBER DB:TestDB

I connect to the replication agent and found that the distributor was struggling with following error:

Message
The replication agent encountered an error and is set to restart within the job step retry interval. See the previous job step history message or Replication Monitor for more information. The Agent 'Tuitionaffordable-Test_0_Rpt1-Tuitionaffordable1-33' is retrying after an error. 5 retries attempted. See agent job history in the Jobs folder for more details.

Let's Understand: The process could not connect to Subscriber 'TuitionaffordableREP1' indicates that there is some connection problem between the subscriber and the publisher.

Let's go to the job and find out what went wrong:

The job history looks like as follows:

Date                      1/5/2011 10:50:00 AM
Log                         Job History (Tuitionaffordable-Test_0_Rpt1-Tuitionaffordable1-33)

Step ID                 2
Server                   Tuitionaffordable
Job Name            Tuitionaffordable-Test_0_Rpt1-Tuitionaffordable1-33
Step Name         Run agent.
Duration              00:06:56
Sql Severity                        0
Sql Message ID                 0
Operator Emailed                           
Operator Net sent                          
Operator Paged                               
Retries Attempted                          0

Message
2011-01-05 10:50:00:112 User-specified agent parameter values:
                                                -Subscriber Tuitionaffordable1
                                                -SubscriberDB Test
                                                -Publisher Tuitionaffordable
                                                -Distributor TuitionaffordableDist
                                                -DistributorSecurityMode 1
                                                -PublisherDB Test
                                                -OutputVerboseLevel 0
                                                -Continuous
                                                -XJOBID 0x64BAEC84ECCFB2423D323296D689F
                                                -XJOBNAME Tuitionaffordable-Test_0_Rpt1-Tuitionaffordable1-33
                                                -XSTEPID 2
                                                -XSUBSYSTEM Distribution
                                                -XSERVER Tuitionaffordable
                                                -XCMDLINE 0
                                                -XCancelEventHandle 0000000222000991

Custom Search
                                                -XParentProcessHandle 0000003330013DC
2011-01-05 10:50:00:112 Startup Delay: 4935 (msecs)Parameter values obtained from agent profile:
                                                -bcpbatchsize 213232247
                                                -commitbatchsize 100
                                                -commitbatchthreshold 1000
                                                -historyverboselevel 1
                                                -keepalivemessageinterval 300
                                                -logintimeout 15
                                                -maxbcpthreads 1
                                                -maxdeliveredtransactions 0
                                                -pollinginterval 5000
                                                -querytimeout 1800
                                                -skiperrors
                                                -transactionsperhistory 100
2011-01-05 10:50:00:112 The process could not connect to Subscriber 'TuitionaffordableREP1'.
2011-01-05 10:50:00:112 The agent failed with a 'Retry' status. Try to run the agent at a later time.

Action: The next step is to go to the subscriber and look whether there is any connection from host 'Publisher'.

SELECT *
FROM   sys.sysprocesses
where   hostname = 'Tuitionaffordable'

Let's Understad: I can see the connection from publisher but the job and replication agent throw following error continuously: "The process could not connect to Subscriber".

Problem Found: The exact problem is that there is a lot of blocking because of which the resources are not allocated to the publisher connection and this throws a false error. I killed some resource intensive jobs and replication started working properly.



Tuesday, January 4, 2011

SQL Azure Interview Questions and Answers Part - 2



Here you go with the second set of Interview question of SQL Azure:

Qu 5. What is the difference in accessing DB between SQL Server Vs SQL Azure?
Ans: YOu connect to directly DB in SQL Azure instead of connecting
 to SQL Server as we do in SQL Server. From application point of view, if you need to deal with many DBs, you have to write complete connection string again and again.


Custom Search
Qu 6. What encryption security is available in SQL Azure?
Ans: Only SSL connections are supported. SET Encryption = TRUE

Qu 7. What is the Data Tier Application?
Ans: This is basically used for data deployment and started in 2008 R2. This is like a .rar file which is used to deploy the data.

Want to prepare in ten minutes - read article Interview and Beyond in 10 Min

Qu 8. What is the max size of the DB in SQL Azure Web Edition?
Ans: Min 1GB and Max 5GB.

Qu 9. What is the index requirement in SQL Azure?
Ans: All tables must have clustered index. You can't have a table without clustered index.

Qu 10. How do you migrate data from MSSQL server to Azure?
Ans: bcp data out to one text file then bcp data in to Azure. Also read migrate data in eleven steps @ Brute Force Migration of Existing SQL Server Databases to SQL Azure

SQL Azure Interview Questions and Answers Part - 1

Most of the readers of this blog also read following interview questions so linking them here:
Powershell Interview Questions and Answers
SQL Server DBA “Interview Questions And Answers”


Regards,
http://tuitionaffordable.webstarts.com

SQL Azure Interview Questions and Answers Part - 1



Here you go with first set of Interview questions on SQL Azure:

Qu 1. What is Cloud?
Ans: Cloud indicates that user don't need to worry about the s\w installation and management. User neither needs to buy the costly license nor they need to worry about the maintenance.

Qu 2. What is the code far Application topology?
Ans: To connect to the SQL Azure from outside of the data center. Other are code near and code hybrid scenarios. Code near means application running in Windows Azure inside microsoft data center.

Qu 3. What is SQL Azure Data sync?
Ans: This is to synchronize the data between local and cloud.

Qu 4. How many replicas are maintained for each SQL Azure DB?
Ans: 3 replicas are maintained for each logical DB. Single primary is observed as the replica where actual read/write take place. Once this goes down, another replica is upgraded automatically as a single primary.

Want to prepare in ten minutes - read article Interview and Beyond in 10 Min
Custom Search
Qu 5. What is the difference in accessing DB between SQL Server Vs SQL Azure?
Ans: YOu connect to directly DB in SQL Azure instead of connecting
 to SQL Server as we do in SQL Server. From application point of view, if you need to deal with many DBs, you have to write complete connection string again and again.

SQL Azure Interview Questions and Answers Part - 2

Most of the readers of this blog also read following interview questions so linking them here:
Powershell Interview Questions and Answers
SQL Server DBA “Interview Questions And Answers”


Regards,
http://tuitionaffordable.webstarts.com

Monday, January 3, 2011

Powershell Interview Questions and Answers



I have been interviewing for last six years. Recently I added some "powershell questions" in my interview hurdles. I would like SE and DBAs to prepare similar questions before you go for interviews.

my new blog - How to Handle Sexual Harassment at Workplace
Want to prepare in ten minutes - read article Interview and Beyond in 10 Min

Go through following list of questions and blog entries and BEAT IT.

My new blog - How to Handle Sexual Harassment at Workplace

Question 1. What is the best way to find all the sql services on one server?

Ans. There are two ways to do this.

1. get-wmiobject win32_service | where-object {$_.name -like "*sql*"}
2. get-service sql*

Question 2. Hοw tο find out which server and services are running under a specific account?

Ans. Go through my blog for this: Find out which server and services are running under a specific account

Question 3. How do you manage not to take any action for errors while executing powershell?

Ans: -ErrorAction Silentlycontinue

Question 4. Why do you get the error "Cannot bind argument to parameter 'Name' because it is null" in powershell?

Ans: Read my blog Starting Service Using Powershell Commands . I have also described to start the services in this blog.

Custom Search
Question 5. Which class can help us identify whether the m\c is 32 bit or 64?

Ans: win32_computersystem. This can be used as follows:

PS C:\> $server = gwmi -cl win32_computersystem
PS C:\> $server.SystemType
X86-based PC

Question 6. When do you get "getwmicomexception"?

Ans: Read Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) “getwmicomexception,microsoft.powershell” and “getwmicomexception”

Question 7. How to find using powersell if the system is 32 bit or 64 bit?

Ans: Read "How can I tell if my computer is running a 32-bit or a 64-bit version of Windows?"

Question 8. When do you get following error: "getwmicomexception,microsoft.powershell.commands.getwmiobjectcommand"

Ans: Read Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) “getwmicomexception,microsoft.powershell” and “getwmicomexception”

Question 9. How to determine the health of SCOM agent in an environment having hundreds of servers?

Ans: Read blog SCOM Health using Powershell Commands

Most of the readers of this blog also read following interview questions so linking them here:
SQL Azure Interview Questions and Answers Part - 1
SQL Azure Interview Questions and Answers Part - 2
SQL Server DBA “Interview Questions And Answers”
What after Final Year of Education?


Regards,
http://tuitionaffordable.webstarts.com