We have some large tables and are trying to optimize against. (Note: in this example id, user_id, and delete all have indexes).
These are lightning fast:
select *
from sites
where (sites.id = 242 and sites.user_id = 425)
select *
from sites
where (sites.id = 242 or sites.user_id = 425)
But once we add any mixed conditional checking it just tanks
select *
from sites
where (sites.id = 242 or sites.user_id = 425) and sites.deleted = 1
We have found that this query (hack) works, but we are currently using SQLAlchemy for query building and want to avoid any subqueries
select *
from sites
where (sites.id = 242 or sites.user_id = 425) and site.id in (
select id from sites where sites.deleted = 1
)
It confuses me that mysql is choking on the mixed case. But is there anyway to optimize this?
You could try to add a compound index on
user_id, is_deleted (the order does matter, and I’m not sure which way would be better for this particular scenario). If you are searching primarily for single or a limited number of user_ids, this order would be the correct way.
That might help you out.
Is ID your unique primary key? If so, do you need to search for it if it’s deleted?
You might be able to use a UNION if that’s available, since the primary key search would be very fast.
Brian