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

H2 doesn't use index when I use IN and EQUAL in one query #643

Closed
lukdiman-developer opened this issue Oct 19, 2017 · 3 comments
Closed

Comments

@lukdiman-developer
Copy link

lukdiman-developer commented Oct 19, 2017

I have a table Person and two columns firstName, lastName. I have a composite index on two columns (firstName, lastName) as well. My query is SELECT * FROM Person WHERE firstName IN ("FirstName1", "FirstName2") AND lastName="LastName1". H2 doesn't use the index. In my case Y IN(..) will produce far less rows than X=?.

I decided to have a look on source code. I have found a method org.h2.index.IndexCursor#prepare. There is such a piece of code in the method:
if (isStart || isEnd) { // an X=? condition will produce less rows than // an X IN(..) condition inColumn = null; inList = null; inResult = null; }

I agree that X=? condition will produce less rows than X IN(..). But I doesn't agree that X=? will produce less rows than Y IN (..). This condition excludes IN values from Row.

I changed in debug mode isStart and isEnd values to false and H2 will start to use my index.

  1. How can I manage this situation?
  2. Does H2 have statistics like Oracle?
@grandinj
Copy link
Contributor

please ask this kind of question on the mailing list

@grandinj
Copy link
Contributor

Fixed

@lukdiman-developer
Copy link
Author

lukdiman-developer commented Oct 27, 2017

Great! Thank you! I've checked a solution from commit 7ec1dbe and it solves the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants