Does SQL Server optimize LIKE ('%%') query? -
i have stored proc performs search on records.
the problem of search criteria,which coming ui, may empty string. so, when criteria not specified, statement becomes redundant.
how can perform search or sql server? or, optimize like('%%') query since means there nothing compare?
the stored proc this:
alter proc [fra].[mcc_search] @mcc_code varchar(4), @mcc_desc nvarchar(50), @detail nvarchar(50) begin select mcc_code, mcc_desc, createdate, creatinguser fra.mcc (nolock) mcc_code ('%' + @mcc_code + '%') , mcc_desc ('%' + @mcc_desc + '%') , detail ('%' + @detail + '%') order mcc_code end
the short answer - no long answer - absolutely not
does optimize like('%%') query since means there nothing compare?
the statement untrue, because there is compare. following equivalent
where column '%%' column not null
is not null requires table scan, unless there few non-null values in column , indexed.
edit
resource on dynamic search procedures in sql server:
must read article erland sommarskog, sql server mvp http://www.sommarskog.se/dyn-search.html (pick version, or read both)
otherwise if need performance on contains style searches, consider using sql server fulltext engine.
Comments
Post a Comment