Created 07-31-2017 10:51 AM
I have a table named Accounts as shown below in phoenix
AccountId | Name | Branch | Transactiondate |
123 | Rajesh | abc | 20170413 |
124 | Sachin | def | 20170112 |
125 | Rajesh | mno | 20170314 |
126 | Bahubali | mno | 20170314 |
127 | Sachin | pqr | 20170202 |
AccountId is the primary key
An index is created as
The following query hits the index and index is used
select AccountId,Name,Transactiondate from Accounts where Name = 'Rajesh' AND Branch = 'abc'
whereas the below query doesn't make use of the index
select AccountId,Name,Transactiondate from Accounts where Name = 'Rajesh' OR Branch = 'abc'
Why is it so, AND works whereas OR doesn't work.
Any thoughts would be great
Created 08-01-2017 08:45 AM
Your OR condition include a filter on non-primary key, so Phoenix has to read the full table anyways,
There are two ways (you can try):-
* you may try adding an index hint in the query( if you want your particular index to be used)
* If you have another index on Branch (leading in primary key) as well, then you can use UNION of two queries to get result fast(select AccountId,Name,Transactiondate from Accounts where Name = 'Rajesh' UNION select AccountId,Name,Transactiondate from Accounts where Branch = 'abc')