Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Top-level disjunctive causes sub-optimal query plan #3

Closed
glebm opened this issue Sep 4, 2014 · 6 comments
Closed

Top-level disjunctive causes sub-optimal query plan #3

glebm opened this issue Sep 4, 2014 · 6 comments
Milestone

Comments

@glebm
Copy link
Owner

glebm commented Sep 4, 2014

Issue from a comment by @fatalmind:

Besides the row values, there is also a performance problem with the logic as shown in the README:

x0 OR 
y0 AND (x1 OR
        y1 AND (x2 OR
                y2 AND ...))

The problem is that this kind of where-clause cannot use and index on these columns, even if it exists. No matter which database.

See here: http://use-the-index-luke.com/sql/partial-results/fetch-next-page#sb-equivalent-logic

To use an index, you must not have an OR on the top level comparison.

Let me know if you have any questions.

-Markus Winand

@glebm glebm changed the title Performance Performance issue with a top-level disjunctive query Sep 4, 2014
@glebm glebm changed the title Performance issue with a top-level disjunctive query Top-level disjunctive query uses index sub-optimally Sep 4, 2014
@glebm glebm changed the title Top-level disjunctive query uses index sub-optimally Top-level disjunctive causes sub-optimal query plan Sep 4, 2014
@glebm
Copy link
Owner Author

glebm commented Sep 4, 2014

Note that Oracle seems to be doing something else here: it plans a union for each top-level OR clause. Not sure if it does anything beyond this in our case. https://blogs.oracle.com/optimizer/entry/or_expansion_transformation

@fatalmind
Copy link

Yes, it may do that. Or not, that really depends on many factors. For the general case, in my experience, it is better to go for the "read too much" solution as shown in my article. Row-Values would be best of course, but that's only properly handled by PostgreSQL.

glebm added a commit that referenced this issue Sep 4, 2014
glebm added a commit that referenced this issue Sep 4, 2014
@glebm glebm added this to the v0.1.2 milestone Sep 4, 2014
@glebm
Copy link
Owner Author

glebm commented Sep 4, 2014

@fatalmind Released in v0.1.2, thank you :)

@glebm glebm closed this as completed Sep 4, 2014
@glebm
Copy link
Owner Author

glebm commented Sep 7, 2014

@fatalmind I wonder if the extra condition helps for enumerated order values, e.g.:

SELECT "issues".* FROM "issues"  WHERE 
  ("issues"."pinned" IN ('t','f') AND 
    ("issues"."pinned" = 't' OR
    "issues"."pinned" = 'f' AND ("issues"."priority" = 'high' ... 

@fatalmind
Copy link

No, because IN-Lists are disjunct conditions by themselves.

In that case I'd recommend just going for the simple way.

Generally: you need to have a so-called SARGable (search-argument-able) condition at the top-level. These are basically equals (=), ranges (>, <, but potentially also LIKEs without prefix-wildcard).

glebm added a commit that referenced this issue Sep 8, 2014
@glebm
Copy link
Owner Author

glebm commented Sep 15, 2014

@fatalmind Thank you, order_query is now free of bugs that prevent index usage 🎱

Good725 added a commit to Good725/ruby_order that referenced this issue Feb 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants