SQL if/else vs and/or in select with different params -


i wonder if there's general performance difference between dividing query in if/else or use long clause and/or when want make select on table using different parameters? eg.

if statements:

if @p1 null , @p2 null begin    select * table end else if @p1 not null , @p2 null begin    select * table t t.val1 = @p1 end   else if @p1 null , @p2 not null begin    select * table t t.val2 = @p2 end   

and/or statements:

    select * t1     (         (t1.val1 = @p1 or @p1 null)         ,        (t1.val2 = @p2 or @p2 null)     ) 

i'd use and/or statements since doesn't produce code , is, in opinion, easier read how impact performance

the short answer it depends. larger table , more disperate search criteria larger performance degredation of using or combine 2 differing search behaviours.

http://www.sommarskog.se/dyn-search.html

this shows one query = one execution plan, , in turn means appropriate indexes can not correctly used in cases.

although article specific ms-sql-server, generic principle across of rdbms have ever worked on.


for fact tables, or large, use if or dynamic sql. small or trivial, use concise maintainable code.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -