optimization - is there a "best way" to short circuit a mysql query -
i have situation i'm assembling query based on user provided criteria , want figure out efficient way this.
if have table looks this:
int id | varchar phone | varchar email | varchar rfid
and user pass in array defines order (and items) they'd user *could this:
["id","email","phone"]
or this:
["email"]
or this:
["phone","rfid"]
or other possible combination of 4 fields.
based on receive need user in order in these fields arrived , if find match, don't want keep looking.
in other words if input is
["email","rfid","phone"]
and db , find user provided email, don't want keep looking see if rfid matches, want return said user. however, if don't find such email, want move on rfid.
so, in various tests i've done (mostly playing case
statement in where
clause) results have been terrible. taking second return value, opposed taking <50ms when simplify where
search individual field.
i should note these fields indexed.
so... question is, should bite bullet , make many sql calls there items in incoming array, or there efficient way structure single query not bog down system various attempts have.
i recognize may abstract question, hoping there's mechanism such use i'm overlooking.
i don't think there's way short-circuit in sql. can construct where
clause uses or
combine critiera, doing prevents using indexes. can use union
this:
select * (select 1 precedence, table.* table field1 = 'value' union select 2 precedence, table.* table field2 = 'value' ... ) x order precedence limit 1
where replace field1
, field2
, etc. field names input array. produce desired results in 1 query, have perform sub-queries, won't short-circuit.
the best solution solve in application code. loop through fields in input, , perform query field. when result, break out of loop , return it.
Comments
Post a Comment