SQL Server Performance Forum – Threads Archive
Performing multi criteria search
Hello there!<br /><br />The problem I am facing is probably a very common one but I haven’t found any elegant solution out there so I decided to post this.<br /><br />PROBLEM<br /><br />** I need to implement a multi criteria search across multiple tables (~4).<br />** Search routine is implemented as a stored procedure that takes search criteria as input parameters <br />** Any number of search criteria can be NULL, thus we are looking for dynamic queries that can adjust accordingly<br />** The task is performance critical<br />** We run SQL Server 2005<br /><br />IDEAS<br /><br /> The first approach I took is a very basic SELECT statement with tons of INNER JOINS. The problem with NULL search criteria is solved by using COALESCE statement like this<br /><br /> <pre id="code"><font face="courier" size="2" id="code"><br /> –inside one of the joins<br /> INNER JOIN SomeTable st ON st.id = basic_table.id AND<br /> st.field_name = COALESCE(@search_param,st.field_name)<br /> </font id="code"></pre id="code"><br /> <br /> Now this works all right. Kind of <img src=’/community/emoticons/emotion-1.gif’ alt=’
Move the filter criteria into a WHERE clause. This keeps the JOINs tidy. If the filter criteria are for tables from which you are not returning columns, then remove those tables from the FROM clause and turn each JOIN into a subquery like this: WHERE ((@search_param IS NULL) OR (basic_table.id IN (SELECT st.id FROM SomeTable st WHERE st.field_name = @search_param)))
@Adriaan<br /><br />Thank you. This is exactly what I’ve been thinking about for the last 5 minutes. <img src=’/community/emoticons/emotion-1.gif’ alt=’

quote:Originally posted by Adriaan
Move the filter criteria into a WHERE clause. This keeps the JOINs tidy.
If the filter criteria are for tables from which you are not returning columns, then remove those tables from the FROM clause and turn each JOIN into a subquery like this:
WHERE ((@search_param IS NULL) OR (basic_table.id IN (SELECT st.id FROM SomeTable st WHERE st.field_name = @search_param)))
Why do you ommit using COALESCE instead of checking for a NULL value? How about WHERE (basic_table.id IN (SELECT st.id FROM SomeTable st WHERE st.field_name = COALESCE(@search_param,st.field_name)))
If you have an index on st.field_name, the COALESCE call causes SQL Server to do a table scan, which you want to avoid. Drop the COALESCE call, and SQL Server can do an index seek, which is what you want for best performance.
Another question here… Would using a view be a good alternative to a super huge query with tons of joins?
Thanx
SQL Server doesn’t mind super huge queries. If you’re using the same complex query over and over again, then a view might make your life as a developer easier. Whatever you’re comfortable with.
there’s a good essay on dynamic search conditions here:
http://www.sommarskog.se/dyn-search.html
www.elsasoft.org
@Adriaan, @jezemine Thank you guys for prompt replies. I am really liking this forum for this. I am sure SQL Server doesn’t mind huge queries. My concern is the productivity.
]]>