Assume I have a database set up and a table named MyTable
which contains a large number of records (tens of thousands). Assume a query as follows...
select * from MyTable where ColumnX = 'X'
... returns just a handful of records (< 10). Let's say I wanted to filter this result set further to only those records where ColumnY
matches 'Y1'
or 'Y2'
. Is it better from a speed and memory perspective to simply modify to the above query as follows...
select * from MyTable where ColumnX = 'X' and (ColumnY = 'Y1' or ColumnY = 'Y2')
... Or is it better to iterate over the (small) result set in code and filter out only those records where ColumnY
matches 'Y1'
or 'Y2'
? The reason I ask is because I have been told that OR
clauses are bad in database queries from a performance perspective (when dealing with large tables) and better avoided where possible.
Note: The scenario in which this applies for me is an Android application with a local SQLite database but I guess the question is a bit more generic than that.