Archive

Archive for July, 2011

SQL DBA – SHRINK DATABASE & SHRINK FILE – SQL Server

July 2, 2011 3 comments

 
While working with huge databases with large tables there are times when you get your database and log files expand ridiculously and eating up the entire disks.
This happens when you are either on an:
– OLTP environment and doing lots of DMLs (INSERTS, UPDATES or DELETES) or
– OLAP environment when you are running huge SQL queries that goes on expanding your tempdb and finally eats up your entire drive space.

There are lot of ways to better configure your disks and databases there to avoid such situations. But these kind of situations come and you have to approach the DBA or become a DBA yourself.

 
Check the LOG Size and Space used, here:

DBCC SQLPERF (LOGSPACE)

 
Check if your database has adequate space available to free up:

SELECT 
	name, 
	size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS AvailableSpaceInMB
FROM sys.database_files;

 
Before shrinking the log file, it better to backup your transaction logs, as shown below:

BACKUP LOG <database_name> WITH TRUNCATE_ONLY
DBCC SHRINKFILE(database_name_log, 2)

 
OR, you can simply change the Database recovery mode to Simple, and run the above mentioned DBCC statement.

 
tempdb can be freed up by simply restarting SQL Server. When SQL Server shuts down it deletes the tempdb and on restart it automatically creates tempdb database. Thus you get fresh tempdb with minimal space. This is not advisable on production servers and should be handled by experts.
 


TSQL Interview Questions – Part 4

July 1, 2011 7 comments

Next part of TSQL Interview Questions, contd. from my previous post.

76. What are Running totals and how would you calculate them? Create a single SQL statement starting with SELECT or ;WITH.
https://sqlwithmanoj.com/2011/07/04/calculating-running-totals/

77. What are the various SSIS logging mechanisms?
– Text file
– SQL Server Profiler
– SQL Server Log Provider, link
– Windows Event Log
– XML File
MS BOL link: http://msdn.microsoft.com/en-us/library/ms140246.aspx

78. On which table the SSIS SQL Server logs are stored?
On 2005 its sysdtslog90 & on 2008 its sysssislog.
More on SSIS logging on: https://sqlwithmanoj.com/2011/06/15/logging-in-ssis-using-sql-server-log-provider/

79. Reverse a String value without using REVERSE() function and WHILE loop. Should be a single SQL statement/query.
https://sqlwithmanoj.com/2011/08/18/reverse-a-string-without-using-tsqls-reverse-function/

80. What is the use of BCP utility in SQL Server and how will you use it?
https://sqlwithmanoj.com/2011/09/09/bcp-in-out-queryout-the-bcp-util/

81. Is there any difference between Excel Source in SSIS 2005 & 2008?
http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/thread/ea770da2-6f0c-41c4-8093-52b5df73f460/#4267c1f1-74a0-4e2c-b5a4-e2909bf4e5f4

82. Difference between Bookmark/Key lookup & RID lookup?
Lookups happen when an Index does not cover a Query, or SELECTED columns in a Query are not available in an Index, thus it lookup in the Clustered Index or the table for missing information/columns. Bookmark/Key lookup happens when a Clustered Index is present, and RID lookup happen when there is no Clustered Index present.

83. How nested transactions behave?
Here is a scenario:
I have a transaction T and inside this there are 2 transactions T1 AND T2.
If TRANSACTION T2 fails then what happens to transaction T and T1.

There is no concept of Nested Transactions in SQL Server and creating them does not make sense. Any Transaction fails will Rollback all outer Transactions, and thus all inner/nested Transactions will also Rollback, check here: https://sqlwithmanoj.com/2015/05/26/sql-myth-nested-transactions-in-sql-server-and-hidden-secrets/

84. How will you check if a stored procedure with 500 line of code is running very slow? What steps will you take to optimize it?

85. New features in SQL Server 2005 compared to SQL Server 2000 you’ve worked with.
– PIVOT, UNPIVOT, Ranking functions (row_number, rank, dense_rank, ntile), CTEs, Grouping sets (ROLUP, CUBE), Intersect, Except, OUTPUT clause, Merge statement, Try-Catch, BIDS (SSIS, SSRS, SSAS), CLR, SMO.
New datatypes: XML, VARCHAR(max), NVARCHAR(max), VARBINARY(max) deprecating the TEXT, NTEXT AND IMAGE datatypes.
– XML  indexes.
– Database (SMTP) mail, SSMS, DMVs, Express Edition, Service Broker, Data Encryption, MARS
For all the new SQL Server 2005 features discussed on this blog check here: https://sqlwithmanoj.com/category/sql-server-versions/sql-server-2005-sql-server-versions/

86. How will you copy unique records from duplicates in source to destination in SSIS?

87. What transformation will you use to concatenate First name and Last name in SSIS?

88. What do you mean by selectivity of a column/table?
The selectivity is what goes with the cardinality concept. The “cardinality” refers to the number of “distinct” values, as in the set theory so, take a column “SEX”.  The possible values are “male” and “female” (ignoring for the moment other possible values like “unknown” or even “other”) … so, your cardinality for that column would be 2, no matter how many rows you have in that table.

The selectivity is the “number of rows” / “cardinality”, so if you have 10K customers, and search for all “female”, you have to consider that the search would return 10K/2 = 5K rows, so a very “bad” selectivity.

The column for the primary key on the other side is “unique”, and hence the cardinality is equal to the number of rows, by definition.  So, the selectivity for searching a value in that column will be 1, by definition, which is the best selectivity possible.

89. Difference between EXISTS & IN, which one gives good performance?
EXISTS vs IN performance: https://sqlwithmanoj.com/2011/02/15/not-in-not-exists-joins-with-null-values/

90. What output will “SELECT 1/2” statement give?
0, it will give zero.

91. What is Database Partitioning?
This involves 4 steps:
1. Create Database with different file groups
2. Create Partition Function
3. Create Partition Scheme
4. Create Partitioned Table or Index

92. What is the use of NOLOCK option?
https://sqlwithmanoj.com/2013/10/04/difference-between-nolock-and-readpast-table-hints/

93. How many types of recovery models are available for a database?
1. Simple
2. Bulk logged
3. Full

94. How many types of temporary tables are there in SQL Server?
– Local Temp tables (#)
– Global temp tables (##)
– Table variables (@)
Check here to know about all these temporary tables: https://sqlwithmanoj.com/2010/05/15/temporary-tables-vs-table-variables/ | YouTube

95. In how many ways you can get a table’s row count?

--// 1. Using COUNT(*)
Select count(*) from Person.Contact

--// 2. using COUNT(1)
select count(1) from Person.Contact

--// 3. Using SUM() aggregate function
select sum(1) from Person.Contact

--// 4. Using sysindexes view
select object_name(id), rows from sys.sysindexes where object_name(id) = 'Contact' and indid&amp;lt;2

--// 5. Using sp_spaceused system SP
exec sp_spaceused 'Person.Contact' 

--// 6. Using DBCC CHECKTABLE function
DBCC CHECKTABLE('Person.Contact') 

--// Note: Before running 5 &amp;amp; 6 you may need to run this script:
DBCC UPDATEUSAGE ('AdventureWorks','Person.Contact') WITH COUNT_ROWS

Reference

96. In how many ways you can select distinct records from a table?

97. In how many ways you can select top 1 row?

98. What are the new features introduced in SQL Server 2008 R2?
SQL Server 2008 new features: https://sqlwithmanoj.com/category/sql-server-versions/sql-server-2008-sql-server-versions/

99. What are the new features added SQL Server 2012 (Denali)?
SQL Server 2012 (denali) new features: https://sqlwithmanoj.com/denali-2012/

100. What new feature has been added to TRY-CATCH construct?
Check here for all posts related to TRY-CATCH: https://sqlwithmanoj.com/tag/try-catch/

… more questions on next post Part-5.