-2

I am trying to optimize this sql query and I see it's using tons of OR statements throughout it, particularly again and again with the same parameter. I see if I comment out the heaviest use of it, execution time reduces by 150%.

the part I commented out looks like this for example:

declare @variable bit = 1
select 1 where @variable = 1 or not exists(select 1 where 1 = 2)

can anyone suggest a way to rewrite this without the OR ?

I am using Sql Azure

Lizi
  • 93
  • 16
  • Microsoft SQL Azure (RTM) - 12.0.2000.8 – Lizi Jan 20 '22 at 14:32
  • `Select 1` will do just the same. Is it just a part of a bigger picture? – Serg Jan 20 '22 at 14:33
  • yes bigger picture. the issue here is not the subquery, but rather the use of the word OR. The subquery is fast enough. but the OR slows things down – Lizi Jan 20 '22 at 14:38
  • The simplified example unfortunately won't help us. Also, `where @variable = 1` kind of catch all queries often cause performance issues. Does it help if you create two queries depending on the value of `@variable` and execute one or the other. – Salman A Jan 20 '22 at 14:41
  • What's wrong with OR? – jarlh Jan 20 '22 at 16:06

1 Answers1

1

It is not very clear what your query does - you simplified it too much - but you could try:

SELECT ... WHERE condition1
UNION
SELECT ... WHERE condition2
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • what about if I would do this? Would this also work? declare variable bit = 1 select 1 where not exists(select 1 where 1 = 2 and variable =0) – Lizi Jan 20 '22 at 17:20