Saturday, January 22, 2011

Today I am going to post why you should NOT shrink log files periodically.Let us create a Test database.Let us name it as "Test"

Create Database Test
Go


Now get to get the logspace used by our newly created database you can use the following DBCC command.

DBCC SQLPERF(logspace)

In our case the logspace size for the newly created database is 0.484375MB.




Now let us create a single table say T with a single column id and insert about a million records in it and then run the DBCC SQLPERF(logspace) to get the log size.

As you can see the log size has now increased by 300 percent.



The following query will give the logical name of the logfile for the database we just created.

Use Test
Go

select name from sys.sysfiles

In our case it is Test_log.

Now let us shrink the log file.We will use the DBCC SHRINKFILE command.

DBCC SHRINKFILE(Test_log)

The o/p of the above command shrinks the log file to the approximately the original size as when the database was created.



Now let us try to delete the records from the the only table in the Database.This will cause lot of log records written to the log file.

Delete from T where id>0

again let us check the log file size.

Wow log file has now bloated to almost 460 percent of the original size.


So the conclusion from all the above would be that log file will increase no matter how many times you shrink it.

So always have a good backup strategy to keep the log file size in check.Log shrinking should only be done in very very emergency situations only.

Thursday, June 3, 2010

NULL values in Unique keys

Recently I posted a post at SQLTeam.com

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=145497

asking about NULL values in Unique keys.

Cleary BOL states

One of the differences between a primary key constraint and a unique key constraint is that null values are allowed in a column with a unique constraint.

But it never states how many NULL values it allows.
Just if you read at my above post at SQLTeam.com you will get nowhere.
Man talk about confusion Microsoft as usual is leading the way.

Monday, May 24, 2010

Lets play with XML

Below is a nested XML which contains Customer info and Order Details.I need to get the Order Details for each customer in the XML.One way to do it was using OPENXML and sp_xml_preparedocument.

With new enhancements for XML datatype from SQL 2005 and beyond it can be easily done by using Xquery.This is how it can be done.

declare @xmlData XML
set @XMLData =
'<root><row>
<custid>1</custid>
<custname>name1</custname>
<order>
<orderdetail orderid = "1" orderdate = "2008-01-01" />
<orderdetail orderid = "2" orderdate = "2008-01-02" />
</order>
</row>
<row>
<custid>2</custid>
<custname>name2</custname>
<order>
<orderdetail orderid = "3" orderdate = "2008-01-03" />
<orderdetail orderid = "4" orderdate = "2008-01-04" />
</order>
</row>
</root> '

select @xmldata

select x.i.value('../../custid[1]','varchar(40)') as custid,
x.i.value('../../custname[1]','varchar(40)')as custname,
x.i.value('@orderid','int')as orderid,
x.i.value('@orderdate','date')as orderdate
from @xmldata.nodes('/root/row/order/orderdetail')x(i)

The resultset for the above query is this.

Friday, May 7, 2010

Getting numeric or character values from an alphanumeric string

Sometimes it becomes necessary to get all the numeric or all the character values from a alphanumeric string.
The idea in the solution I have posted is to convert all the characters from the string into rows in a derived table
using a look table @tbl & then concating the charater values excluding the numeric values in my example below.
If you need to get the numeric values from the string simply replace like '[a-z]' to like '[0-9]'

Declare @str As Varchar(50)
Select @str='klasdjfkl034ioerut'
Declare @value As Varchar(40)=''

Declare @tbl As Table(number Int Identity)
Insert Into @tbl Default Values
While SCOPE_IDENTITY()< len(@str)
Insert Into @tbl Default Values

Select @value=@value + '' + value From
(
Select number,SUBSTRING(@str,number,1)As value From @tbl Where number Between 1 And LEN(@str)
)t Where value like '[a-z]'

Select @value

Wednesday, May 5, 2010

Difference between 2 dates in Hours

Sometimes it is necessary to show the difference between 2 dates in hours.The query can be modified to be used as an function accepting 2 parametrs Date1 and Date2
The accuracy can be checked hereto compare the results. Here is the query

Declare @dt1 datetime='12-May-2010 09:00:00'
Declare @dt2 datetime='13-May-2010 04:01:00'

Select
RIGHT('0'+CONVERT(varchar(2),DATEDIFF(mi,@dt1,@dt2)/60),2)+ ':' +
RIGHT('0'+CONVERT(varchar(2),DATEDIFF(mi,@dt1,@dt2)%60),2)+ ':' +
RIGHT('0'+CONVERT(varchar(2),DATEDIFF(SECOND,@dt1,@dt2)%60),2) As DiffInHours

Tuesday, May 4, 2010


Recently one of my friend had asked me a query by which he could get all such vendors
who sell all the ItemTypes present in the table.Of course for sake of illustration purpose I have de normalised the table as my blog is not for sake of teaching the fundamentals of database but rather to provide tweaks for non trivial problems a developer faces.
Here is the table structure


In the above condition the o/p should be vendor id 1 and 2
To my surprise on first glance I found the pivot query working much faster than other queries.
Will post more about the performance implications later.But as for now here is the query.


declare @tbl as table(vendor int,ItemType varchar(40))

insert into @tbl
select 1,'a' union all
select 1,'b' union all
select 1,'c' union all
select 2,'a' union all
select 2,'b' union all
select 2,'c' union all
select 3,'c'

select * from @tbl

--Method 1
select vendor from
(
select vendor,ItemType from @tbl
)u
pivot
(max(ItemType) for ItemType in ([a],[b],[c]))v
where a is not null and b is not null and c is not null

--Method 2
select vendor from @tbl where ItemType in('a','b','c')
group by vendor
having COUNT(case ItemType when 3 then 1 else 0 end)=3

--Method 3
select distinct vendor from
(
select vendor,(select COUNT(distinct ItemType)as rid from @tbl t2 where t1.vendor=t2.vendor and t2.ItemType in ('a','b','c'))as rid
from @tbl t1
)T where T.rid=3

--Method 4
select vendor from @tbl where ItemType in ('a','b','c')
group by vendor
having COUNT(distinct ItemType)=3