tags:

views:

69

answers:

2

Some articles I found on the internet compared ISNULL with COALESCE, so I think my question is a little different.

I'm wondering which is better in terms of performance?

SELECT * FROM mytable WHERE mycolumn IS NOT NULL AND mycolumn <> '';

Or

SELECT * FROM mytable WHERE COALESCE(mycolumn,'') <> '';

Other than performance, are there any other concerns I should consider when deciding?

EDIT:

I'm using Teradata.

+4  A: 

This version is slightly more sargable and allows an index to be (potentially) used

SELECT * FROM mytable WHERE mycolumn IS NOT NULL AND mycolumn <> '';

It can be simplified to

SELECT * FROM mytable WHERE mycolumn <> '';

The reason why I say "slightly" and "potentially" is that the non equality predicate may well mean you end up with a full scan anyway.

Martin Smith
@Martin Smith, As a follow up question, if we knew that an index will be used, we might be able to automatically optimize the sql (behind the scenes) and translate COALESCE(mycolumn,'')<>'' into mycolumn IS NOT NULL and mycolumn <>''. Am I making sense?
Russell
I believe you may find that NUSI may not be used to satisfy an inequality comparison. It'd be worth looking at the EXPLAIN to confirm whether or not the optimizer is using the NUSI.
RobPaller
A: 

You might also consider using the ANSI_NULL ON setting. This will implicitly filter out null values on a column where you issue a search argument. Doing this simplifies your query, i'm not sure if it makes it faster. Logically in theory it should though, since less filter arguments need to be evaluated and no functions are being used in where clause which should enable full index selectivity. As an example you could re-factor your query like this:

SET ANSI_NULLS ON;
SELECT * FROM mytable WHERE NOT mycolumn = '';

I hope this helps.

James