Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 190697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:16:42+00:00 2026-05-11T16:16:42+00:00

I’ve got a complex SQL where clause that just got more complex because of

  • 0

I’ve got a complex SQL where clause that just got more complex because of a requirements change. There are four basic sets of cases, each with a different combination of other factors. It’s more readable (in my opinion) to have the four cases as separate branches of the where clause, and to repeat the redundant criteria in each branch. But I don’t know how well the database engine will optimize that.

Here’s the expression in its redundant form. I’ve replaced the actual criteria with letters. A is the “branching” criterion, which comes in four forms. All of the expressions are in the form field='value', unless otherwise noted.

A1 AND B AND C AND D
OR A2 AND B AND C AND D AND E AND F1 AND G
OR A3 AND A3a AND B AND C AND D AND E AND F1 AND G
OR A4 AND B AND C AND D AND F2

All of the A’s except A4 are in the form field in ('value1','value2'). D is field > 'value'. G is in the form field not in (subquery).

Here’s the expression, factored to (I think) its least redundant form.

B AND C AND D AND (
    A1
    OR (
        E AND F1 AND G AND (
            A2
            OR (A3 AND A3a)
        )
    )
    OR (A4 AND F2)

My question is whether I should factor this expression into its simplest (least redundant) logical form, or whether it’s OK to keep it in it’s more redundant but also more readable form. The target database is Sybase, but I’d like to know the answer for RDMBSs generally.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:16:42+00:00Added an answer on May 11, 2026 at 4:16 pm

    In an RDBMS world I wouldn’t bother of redundancy much, efficiency is more important here.

    In your case, I would UNION all the four queries using A‘s as a top condition, like this:

    SELECT  *
    FROM    mytable
    WHERE   A1 AND B AND C
    UNION
    SELECT  *
    FROM    mytable
    WHERE   A2 AND B AND C AND D AND E AND F1 AND G
    …
    

    I didn’t look into Sybase for more than 7 years, but in all major RDBMS‘s UNION‘s are more efficient than OR‘s.

    See this article in my blog for approach to a silimar problem in Oracle:

    • Selecting lowest value: efficient usage of UNION in Oracle

    and also this article for comparison of UNION versus OR in MySQL:

    • Selecting friends: efficiency of UNION compared to one of OR in MySQL

    I think these approaches will work well for Sybase too.

    You also need to create indexes on columns used in your conditions to benefit from UNION‘s

    Update:

    Since condition G is a subquery, it may probably happen that it needs a HASH JOIN to perform fast. HASH JOIN requires a full scan on all unfiltered values, that’s why it may probable be better to filter all values in a single fullscan and then perform a HASH JOIN:

    SELECT  *
    FROM    (
            SELECT  *
            FROM    foo
            WHERE   condition_set_1
            UNION
            SELECT  *
            FROM    foo
            WHERE   condition_set_2_but_no_g
            …
            ) q
    WHERE   G
    

    To make any further judgements, it will be much better to see the query itself, really.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at the CodeIgniter manual on Sessions and… May 11, 2026 at 11:48 pm
  • Editorial Team
    Editorial Team added an answer You can obfuscate it, but there's no way of protecting… May 11, 2026 at 11:48 pm
  • Editorial Team
    Editorial Team added an answer Use XOR: SELECT value ^ 256 So in your case,… May 11, 2026 at 11:48 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.