Optimize SQL Server Query using "OR" or "UNION" -
we have sql server table called deals
general table including financial trade entries, number of can hundreds of thousands or millions. each row in deals
has 1 column called product
, of type varchar , corresponds different trade type: bond trade, equity trade, future or option trade, simple , more complex. want search whole table deals not matured. depending on type of trade, term "matured" has different meanings. example, product type "bond" , others, check value of column maturitydate
in deals
table, larger given date, i.e., maturitydate > @date. , deal type "future", have column valuedate
in same table larger given date , maturitydate
larger given date. first draft of query this:
select * deals ( (product in ('proda', 'prodb') , maturitydate > @cd) or (product in ('prodc', 'prodd') , (maturitydate > @cd , valuedate > @cd)) or (product in ('prode', 'prodf', some_other_product_type) , (maturitydate > @cd , valuedate > @cd) , some_other_criteria) or ... )
we have on 30 product
values , and least 8 sets of criteria (the common criteria set applies maybe 7 or 8 product
values, , least common criteria sets apply 1 or 2 product
values). column product
indexed, columns (but not all) in criteria, example, maturitydate
indexed. of criteria sets check column values in deals
table, there indeed criteria sets involve join other tables , check values there well.
so question is, how can optimize such query (as sw developer, not expert in db , seldom write data access code)? because read somewhere might idea replace or clause union. however, when executed queries using union , using or, former took 5 seconds on development machine (with less 100,000 items in deals
table) while latter took 3 seconds. , said, have limited knowledge don't know if there other way optimize such query. share experience? thanks!
Comments
Post a Comment