title: How do I force an OR clause over another in a WHERE clause in a query in SQL Server?
body: <p>I have a big query and in the WHERE clause I have something like:</p>
  
  <p>Do something</p>
  
  <p>OR</p>
  
  <p>Do something else</p>
  
  <p>(only 1 OR)...</p>
  
  <p>Right now if I run this query, SQL is taking about 13 seconds to finish.</p>
  
  <p>If I remove the second part of the OR clause, the query runs VERY fast.</p>
  
  <p>Shouldn't SQL execute the first part of the OR and, if true, finish the query by NOT looking at the other part of the OR?</p>
  
  <p>Why is SQL looking at both sides of the OR? Why does it not start with the first one?</p>
  
  <p>If I remove the second part of the OR clause, the query runs very fast.</p>
  
  <p>Can someone give me some tips on how to make this happen?</p>
  
  <p>EDIT - here's the query:</p>
  
  <pre><code>SELECT  msc.ID,
          msc.MediaCompanyID,
          msc.AiringNumber,
          msc.ClientNumber,
          cl.ClientName,
          MediaCompanyName,
          CONVERT(VARCHAR, StandardDate, 101) StandardDateOnly,
          CONVERT(DATETIME, CONVERT(VARCHAR, StandardDate, 101)) StandardDateOnlyDateType,
          CONVERT(VARCHAR, StandardDate, 101) + ' ' + CONVERT(VARCHAR, StandardDate, 108) StandardDate,
          CONVERT(VARCHAR, BroadcastDate, 101) + ' ' + CONVERT(VARCHAR, BroadcastDate, 108) BroadCastDate,
          Station,
          StationID,
          msc.MediaCodeID,
          msc.MediaCode,
          MediaCodeDescr,
          PhoneNumber,
          DMA = CASE WHEN DMA IS NULL THEN MarketType
                     ELSE DMA
                END,
          msc.MarketID,
          m.MarketDescr,
          MIN(GrossCost) GrossCost,
          MIN(ClientCost) ClientCost,
          msc.CountryID,
          CASE CountryName
            WHEN 'UNITED STATES' THEN 'USA'
            ELSE CountryName
          END AS CountryName,
          msc.LanguageID,
          l.Language,
          CASE IsConfirmed
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'
          END AS Teletrax
  FROM    dbo.MediaScheduleComplete msc
  JOIN    TVTraffic.dbo.Country c
  ON      msc.CountryID = c.CountryId
  JOIN    TVTraffic.dbo.Language l
  ON      msc.LanguageID = l.LanguageID
  JOIN    TVTraffic.dbo.Client cl
  ON      msc.ClientNumber = cl.ClientNumber
  JOIN    dbo.MediaCode mc
  ON      msc.MediaCodeID = mc.MediaCodeID
  JOIN    dbo.Market m
  ON      msc.MarketID = m.MarketID
  LEFT JOIN TVTraffic.dbo.Offer o
  ON      mc.OfferID = o.OfferID
  INNER JOIN @temp n
  ON      n.i = msc.ID
  WHERE   (
           @MediaScheduleCompleteIDs IS NOT NULL
           AND msc.ID IN (SELECT  i
                          FROM    TVTraffic.dbo.ufnListToSelectInt(@MediaScheduleCompleteIDs))
          )
          OR (
              @MediaScheduleCompleteIDs IS NULL
              AND (
                   StandardDate BETWEEN @from AND @to
                   AND (
                        msc.MediaCompanyID = @mediaCompanyID
                        OR @mediaCompanyID = 255
                       )
                   AND (
                        MarketType = @MarketType
                        OR @MarketType = ''
                       )
                   AND (
                        msc.ClientNumber = @clientID
                        OR @clientID = 0
                       )
                  --  and     MediaCodeTypeID in (1,2,@SpecialTypeID)
                   AND (
                        msc.MediaCodeTypeID = @mediaCodeTypeID
                        OR @mediaCodeTypeID = 0
                       )
                   AND (
                        msc.CountryID = @CountryID
                        OR @CountryID = 0
                       )
                   AND (
                        msc.LanguageID = @LanguageID
                        OR @LanguageID = 0
                       )
                   AND (
                        mc.OfferID = @OfferID
                        OR @OfferID = 0
                       )
                  )
             )
  GROUP BY msc.ID,
          msc.MediaCompanyID,
          msc.AiringNumber,
          msc.ClientNumber,
          cl.ClientName,
          MediaCompanyName,
          StandardDate,
          BroadcastDate,
          Station,
          StationID,
          msc.MediaCodeID,
          msc.MediaCode,
          MediaCodeDescr,
          PhoneNumber,
          DMA,
          msc.MarketID,
          MarketDescr,
          MarketType,
          msc.CountryID,
          CASE CountryName
            WHEN 'UNITED STATES' THEN 'USA'
            ELSE CountryName
          END,
          msc.LanguageID,
          l.Language,
          CASE IsConfirmed
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'
          END
  ORDER BY ClientCost DESC,
          StandardDate ASC
  </code></pre>