Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

SQL Server Interview questions and answers on DBCC

$
0
0
SQL Server Interview questions 1: What are DBCC commands in SQL Server?

Microsoft provides the following Database console commands to provide information on SQL Server operations:

Status Validation Maintenance Miscellaneous SQL Server Interview questions 2: Can you list our one or two example under each category of DBCC commands? Status Commands:

The status commands are the ones you normally run first. With these commands, you can gain an insight into what your server is doing.

DBCC Showcontig DBCC Show_statistics DBCC Opentran DBCC Inputbuffer DBCC Outputbuffer DBCC Proccache DBCC Sqlperf DBCC Tracestatus DBCC Useroptions Validation Commands:

These are useful when you see problems with the database due to page corruption, fragmentation etc.

DBCC Checkdb DBCC CheckTable DBCC CheckCATALOG DBCC CheckConstraints DBCC CheckFILEGROUP DBCC CheckIdent Maintenance Commands:

The maintenance commands are the final steps you normally run on a database when you’re optimizing the database or fixing a problem.

DBCC DBreindex DBCC Indexdefrag DBCC Shrinkdatabase DBCC Shrinkfile DBCC Updateusage Miscellaneous Commands: DBCC Dllname DBCC Help DBCC Pintable DBCC Traceon DBCC ERRORLOG DBCC DROPCLEANBUFFERS DBCC FLUSHPROCINDB DBCC MEMORYSTATUS SQL Server Interview questions 3: Can you explain how to use DBCC CHECKDB?

Let’s look at a couple of the more common options to know how to use DBCC CHECKDB in SQL Server. Steps to execute the DBCC CHECKDB:

Step 1: USE MASTER; GO DBCC CHECKDB ('TestDB', NOINDEX) WITH NO_INFOMSGS;

The command above checks the “TestDB” database but not its indexes. This won’t take long at all. The output returned will tell you if there are problems with the database. If so, check to make sure your backup is handy and then you can run the next level of this command. If find any errors in step 1 execution, execute the next level of DBCC command as below.

Step 2: USE MASTER; GO ALTER DATABASE TestDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO DBCC CHECKDB ('TestDB', REPAIR_FAST) WITH NO_INFOMSGS; GO ALTER DATABASE TestDB SET MULTI_USER;

This command will attempt to fix errors but won’t allow any data to be lost. If that doesn’t work, the next level of the command is in step 3. From 2014/2016 this option is only for backward compatibility. It doesn’t repair anything.

Step 3: USE MASTER; GO ALTER DATABASE TestDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO DBCC CHECKDB ('TestDB', REPAIR_REBUILD) WITH NO_INFOMSGS; GO ALTER DATABASE TestDB SET MULTI_USER; GO

The next level of this command has the potential for data loss. Now execute the below DBCC command as a last and final repair option.

Step 4: USE MASTER; GO ALTER DATABASE TestDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO DBCC CHECKDB ('TestDB', REPAIR_ALLOW_DATA_LOSS) WITH NO_INFOMSGS; GO ALTER DATABASE TestDB SET MULTI_USER; As you can probably guess, this command could potentially lose data or make your applications unusable. Use the REPAIR options only as a last resort. To repair errors, we recommend restoring from a backup. SQL Server Interview questions 4: How can I estimate the TEMPDB space required for running CHECKDB command?

The below command calculates the amount of space in “TEMPDB” required to run DBCC CHECKDB statement. However, the actual statement isn’t executed.

DBCC CHECKDB ('TestDB') WITH NO_INFOMSGS, ESTIMATEONLY Sample Output:

Estimated TEMPDB space (in KB) needed for CHECKDB on database TestDB = 6291456.

SQL Server Interview questions 5: How can I speed up the DBCC CHECKDB execution process?

The below command finds an exclusive lock on the database.

DBCC CHECKDB ('EMP') WITH NO_INFOMSGS, TABLOCK SQL Server Interview questions 6: How can I check data purity using DBCC CHECKDB?

This command is used to find column values which are invalid.

DBCC CHECKDB ('EMP') WITH NO_INFOMSGS, DATA_PURITY SQL Server Interview questions 7: I am writing a stored procedure which we are going to schedule for database maintenance for one of our databases. We have included DBCC CHECKDB also we have two major transactional tables for which we wanted to perform DBCC CHECKTABLE from the same procedure. Can we run both DBCC statements?

No, as it not required. Let me explain what DBCC CHECKDB validates on a database:

Running DBCC CHECKDB internally runs DBCC CHECKALLOC + DBCC CHECKTABLE + DBCC CHECKCATALOG on the database.

SQL Server Interview questions 8: What are the most commonly used DBCC commands?

People who started working on SQL Server from 2000/2005 can know the value of DBCC commands. Starting from 2008, there are a lot of changes that happened and the SQL Server team introduced alternatives for DBCC commands. For example, for INDEX related operations now we can use ALTER INDEX statements. However, still we get to benefit from DBCC commands for the quick results.

Here is the list of most commonly used DBCC commands:

DBCC SHOWCONTIG

DBCC SHOWCONTIG fragmentation information for table or view.

Syntax: DBCC SHOWCONTIG ( table / View / Index Name )

Example: DBCC

SHOWCONTIG (EMPDB)

DBCC OPENTRAN:

It displays information about an active transaction.

Syntax: DBCC OPENTRAN ( Database_Name )

Example: DBCC OPENTRAN (EMPDB) WITHNO_INFOMSGS

Observation:We can get the transaction opened time and id.

DBCC INPUTBUFFER:

It displays the last statement sent from a client to SQL Server.

Syntax: DBCC INPUTBUFFER (SessionID/RequestID)

Example: DBCC

INPUTBUFFER (97) WITHNO_INFOMSGS

DBCC SQLPERF:It provides transaction log space usage statistics for all databases.

Syntax: DBCC SQLPERF(LOGSPACE)

Example:DBCC SQLPERF (LOGSPACE) WITHNO_INFOMSGS

DBCC USEROPTIONS:It displays “Date Format,” “Isolation Level”

Syntax: DBCC USEROPTIONS

DBCC CHECKDB:

It checks Database consistency.

Syntax: DBCC CHECKDB ( ‘< DBName>’ ,[NOINDEX |REPAIR_FAST |REPAIR_REBUILD |REPAIR_ALLOW_DATA_LOSS]) WITHNO_INFOMSGS

NOINDEX:It Checks only database and no impact on indexes

REPAIR_FAST:It repairs errors without any data loss

REPAIR_REBUILD:It repairs rows in non-clustered indexes, rebuilding an index

REPAIR_ALLOW_DATA_LOSS:It repairs all errors, but it can cause data loss

DBCC CHECKIDENT:

It checks and changes the identity value for the table and is used to reset identity value to the initial state.

Syntax: DBCC CHECKIDENT (< table_name>, NORESEED/RESEED, new_reseed_value) WITH NO_INFOMSGS

Example: DBCC CHECKIDENT (TestTab) --Checks current identity value DBCC CHECKIDENT (TestTab, NORESEED) --Identity value shouldn’t be changed DBCC CHECKIDENT (TestTab, RESEED, 0) --Identity value reset to 0

Observation:Below functions find the incremental values for a table.

SELECT IDENT_SEED('MemberDetails') SELECT IDENT_INCR('MemberDetails') SQL Server Interview questions 9: Can you explain how to analyze the result from DBCC MEMORYSTATUS?

We can analyze using Audit Buffer, SQLCLR, Optimizer, SQLUtilities, Connection Pool etc.

SQL Server Interview questions 10: What is the quickest way to get SQL Server memory usage?

DBCC MEMORY STATUS


Viewing all articles
Browse latest Browse all 3160

Trending Articles