I have a MySQL table with just under 200 million rows. I have a Django query that looks like this:
foo.objects.filter(field1_id__in=[about 27 items],
field2_id__in=[about 25 values],
field3=value)
I noticed that the page that runs this filter is hanging today. The page yesterday rendered in about one second. The field1 list grows over time as more data is added, the field2 list is constant size. Playing with these queries interactively I determined that there is a cliff where if I only specified the first 9 values of the field2 "in," the query returns in about a second, but if I move to 10 values in the field2 list, the query hangs "forever."
Does such a severe degradation make sense? There are no joins and no dependent queries, just a WHERE with 3 clauses ANDed together, two of them being the INs. Feels like a MySQL bug...? Or "that's just life with MySQL?"
Edit: the 10-item field2_id__in query just returned: took about 45 minutes!
Raw Query
SELECT `mytable`.`id`, `mytable`.`field1_id`, `mytable`.`field2_id`, `mytable`.`field3_id`, `mytable`.`field4_id`, `mytable`.`data` FROM `mytable` WHERE (`mytable`.`field2_id` IN (44942, 42953, 43099, 43330, 45165, 45468, 43518, 45620, 43693, 45760, 43790, 45930, 43885, 46026, 46120, 44158, 46298, 44314, 42204, 46492, 44441, 42327, 44586, 42515, 44726, 44835, 42802) AND `mytable`.`field3_id` IN (3, 17, 696, 150, 170, 51, 6528, 2383, 3342, 2289, 6491, 6375,2070, 6186, 318, 6498, 5197, 6011, 5833, 7803, 5195, 4871, 6928, 6531) AND `mytable`.`field4_id` = 11 )
Explain Output
select_type: simple
type: range
possible_keys: (3 keys)
key: (key)
key_len: 4
ref: NULL
rows: 14160
extra: Using index condition; Using where
EXPLAINto see what the execution plan is?