getting rid of "OR"

  • I have a statement that has a where clause that looks like this

    WHERE iv.AccountCode = @AccountCode

    AND iv.ItemID = @ItemID

    AND iv.VarianceID = @VarianceID

    AND (

    Active <> @Active

    OR SKU <> @SKU

    OR Position <> @Position

    OR MilliLitres <> @MilliLitres

    OR FluidOunces <> @FluidOunces

    OR Grams <> @Grams

    OR Ounces <> @Ounces

    OR Calories <> @Calories

    OR KiloJoules <> @KiloJoules

    OR TotalFat <> @TotalFat

    OR SaturatedFat <> @SaturatedFat

    OR Carbohydrates <> @Carbohydrates

    OR NaturalSugar <> @NaturalSugar

    OR AddedSugar <> @AddedSugar

    OR TotalSugar <> @TotalSugar

    OR Fiber <> @Fiber

    OR Protein <> @Protein

    OR Salt <> @Salt

    OR Caffine <> @Caffine

    );

    anyone got any ideas on how to make this a tiny bit better - we have indexes in place, but i'm looking for better solutions

     

    MVDBA

  • Here's a way of writing it without ORs, but I'm not sure it's any better:

    WHERE iv.AccountCode = @AccountCode
    AND iv.ItemID = @ItemID
    AND iv.VarianceID = @VarianceID
    AND NOT (
    Active = @Active
    AND SKU = @SKU
    AND Position = @Position
    AND MilliLitres = @MilliLitres
    AND FluidOunces = @FluidOunces
    AND Grams = @Grams
    AND Ounces = @Ounces
    AND Calories = @Calories
    AND KiloJoules = @KiloJoules
    AND TotalFat = @TotalFat
    AND SaturatedFat = @SaturatedFat
    AND Carbohydrates = @Carbohydrates
    AND NaturalSugar = @NaturalSugar
    AND AddedSugar = @AddedSugar
    AND TotalSugar = @TotalSugar
    AND Fiber = @Fiber
    AND Protein = @Protein
    AND Salt = @Salt
    AND Caffine = @Caffine
    );

    you might have a bit more success with this:

    SELECT *
    FROM iv
    WHERE iv.AccountCode = @AccountCode
    AND iv.ItemID = @ItemID
    AND iv.VarianceID = @VarianceID
    EXCEPT
    SELECT *
    FROM iv
    WHERE iv.AccountCode = @AccountCode
    AND iv.ItemID = @ItemID
    AND iv.VarianceID = @VarianceID
    AND Active = @Active
    AND SKU = @SKU
    AND Position = @Position
    AND MilliLitres = @MilliLitres
    AND FluidOunces = @FluidOunces
    AND Grams = @Grams
    AND Ounces = @Ounces
    AND Calories = @Calories
    AND KiloJoules = @KiloJoules
    AND TotalFat = @TotalFat
    AND SaturatedFat = @SaturatedFat
    AND Carbohydrates = @Carbohydrates
    AND NaturalSugar = @NaturalSugar
    AND AddedSugar = @AddedSugar
    AND TotalSugar = @TotalSugar
    AND Fiber = @Fiber
    AND Protein = @Protein
    AND Salt = @Salt
    AND Caffine = @Caffine

     

  • I think creating a table and using EXCEPT / INTERSECT would perform better than a long string of ORs.  Either would also take care of NULL values for you.  So, maybe try something along this line:


    --setup code, prior to main query
    CREATE TABLE #comparison_values (
    Active <data_type> NULL,
    SKU <data_type> NULL,
    Position <data_type> NULL,
    MilliLitres <data_type> NULL, ...
    FluidOunces
    Grams
    Ounces
    Calories
    KiloJoules
    TotalFat
    SaturatedFat
    Carbohydrates
    NaturalSugar
    AddedSugar
    TotalSugar
    Fiber
    Protein
    Salt
    Caffine
    )

    INSERT INTO #comparison_values
    SELECT
    @Active, @SKU, @Position, @MilliLitres,
    @FluidOunces, @Grams, @Ounces, @Calories,
    @KiloJoules, @TotalFat, @SaturatedFat, @Carbohydrates,
    @NaturalSugar, @AddedSugar, @TotalSugar, @Fiber,
    @Protein, @Salt, @Caffine

    ------------------------------------------------------------------------------------------
    --back to the main query code for mods there

    WHERE iv.AccountCode = @AccountCode
    AND iv.ItemID = @ItemID
    AND iv.VarianceID = @VarianceID

    AND EXISTS(
    SELECT
    Active, SKU, Position, MilliLitres,
    FluidOunces, Grams, Ounces, Calories,
    KiloJoules, TotalFat, SaturatedFat, Carbohydrates,
    NaturalSugar, AddedSugar, TotalSugar, Fiber,
    Protein, Salt, Caffine
    EXCEPT
    SELECT
    *
    FROM #comparison_values
    )


     

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • I'd also vote for the temp table option. Makes the code cleaner.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • i'm going to try all the suggestions and see what works (when my hands get warm it's still snowing here)

    MVDBA

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply