I have a very high-traffic table with a char(50) field which takes part in several indexes. This char(50) field allows NULLS, and in that case a NULL value is considered to be the same as a non-NULL, zero-length string for my purposes.
I also disregard leading & trailing whitespace and, while I scrub the data before I insert it, it may also be inserted by means beyond my control.
I have a sproc that is used to copy data from one table to the main table, and it needs to be high-performance. I need to delete duplicate records before inserting the new data and I am using the method discussed in this thread to perform the deletes.
My delete statement looks like this (simplified):
delete masterTable from masterTable t join incomingDataTable inc on ( LTRIM(RTRIM(COALESCE(inc.TextField,''))) = LTRIM(RTRIM(COALESCE(t.TextField,''))) ) where LTRIM(RTRIM(COALESCE(t.TextField,''))) <> ''
I have read that constructs like LTRIM(RTRIM(…)) are bad. Can my delete statement be improved, and if so, how?
EDIT: Just to clarify, TextField does take part in indexes on both tables. EDIT2: TextField is defined as char(50) in both tables. It is not of type TEXT.
It is bad because your JOIN will have to scan the whole index, this is because your condition is not SARGable
Are you also sure is is a TEXT datatype? Last I checked you could not use LTRIM or RTRIM against a Text datatype column?
in response to the char against varchar comment, run this