views:

72

answers:

3

I've got a table and the only time I'll be selecting or doing anything against it is to find all the rows where a certain date column is null. From there, I do stuff with it and update that column, and likely I'll never visit that row again, with the exception of auditing purposes.

So, is there a best practice to ensure that the rows where my column SentDate is null are more quickly found?

+1  A: 

You can set the Default Value or Binding of the field to something that will never be used and then look for that value. Otherwise you will be doing a table scan.

RandomBen
A: 

In SQL Server 2008, you can filter an index, so you could theoretically create your index to include only NULL values, i.e. WHERE SentDate IS NULL.

Paul Williams
+2  A: 

To optimize a table to find NULLs you use indexes, of course. NULLs are indexed and are seek-able like any other value:

create table foo (a int null, b varchar(100) null);
create clustered index cdxFoo on foo(a);
go

insert into foo (a,b) select Number, 'not null' from master..spt_values

insert into foo (a,b) values (null, 'null')

select * from foo where a is null

The query plan for the select clearly shows that a seek on the key 'NULL' is used to locate the row.

Remus Rusanu
Okay, completely unrelated to my original question...what the hell is master...spt_values? I actually looked it up, but I'm not sure I fully get it. And you can use dbname..tablename to skip the owner part? Where the hell have I been?
Matt Dawdy
master..spt_values is a table of numbers, used mostly by... forum answerers. The database..object syntax is valid, is an 'implicit schema three part name', the missing schema name will default to your default schema (usually dbo). I could easily argue that both use of spt_values and reliance on default schema are bad practices...
Remus Rusanu
I have upvoted this and requested my original be deleted. Was answering from the Oracle camp and was not aware of SQL Server (and several other DBs) indexing of nulls. It appears that Oracle and Postgres are in the minority of its treatment of NULL, since DB2, Sybase and MySQL will also index / apply unqiue constraint to nulls.
mrjoltcola