Groups | Blog | Home
all groups > c# > september 2007 >

c# : Null or empty string in a database


Jay
9/27/2007 10:10:25 PM
For a column that contains a string (let's say varchar[50]), is there any
performance advantage in not allowing nulls, and using an empty string ("")
to instead?
Michael Starberg
9/28/2007 1:16:07 AM
[quoted text, click to view]

For perfomance, I have no idea. Probably none.

But I must confess I've done a not null on a varchar
- just to make sure I always get a string and not a null from the database.

I work a lot with ASP.NET and sporting a string as a null,
is a no no. It makes your whole webpage goes kaboom.

Enforcing rules on a database just to have your C# easier
is wrong in my opinion. But very safe in .NET 1.1

For .NET 2.0 and above, it is just stupid as you can always do:

string s = MyData[col, row].SomeValue ?? "";

....

As a sidenote, remember the thread on "" and String.Empty?
Here is a good example why "" is sometimes better...

1) string s = MyData[col, row].SomeValue ?? "";
2) string s = MyData[col, row].SomeValue ?? String.Empty;


1) readable, we want SomeValue
2) too verbose, if you read it fast, you might be looking for SomeEmpty
below. *s*

- Michael Starberg






Ciaran O''Donnell
9/28/2007 5:37:02 AM
In SQL Server, the is a performance advantage to search for col1 is null
rather than col1 = ''

This is because there is a flag in the row header for each column indicating
whether its null so it is easier to check this flag then do a string
comparison

HTH

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


[quoted text, click to view]
Jay
9/28/2007 5:42:54 PM
Thanks Ciaran,

Yes, I see what you mean, although presumably if the column isn't nullable,
SQL Server wouldn't have this flag, so wouldn't need to check it first. I
assume that checking it wastes time for strings that aren't null.

Jay

"Ciaran O''Donnell" <CiaranODonnell@discussions.microsoft.com> wrote in
message news:3589431B-63F8-4F83-824B-FFFEDCB4A761@microsoft.com...
In SQL Server, the is a performance advantage to search for col1 is null
rather than col1 = ''

This is because there is a flag in the row header for each column indicating
whether its null so it is easier to check this flag then do a string
comparison

HTH

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


[quoted text, click to view]
Arne_Vajhøj
10/2/2007 10:16:58 PM
[quoted text, click to view]

In many cases NULL and "" would mean something completely
different.

I would be very reluctant to reduce data reliability for
a performance gain I doubt exist.

AddThis Social Bookmark Button