I have a large list with 15k entries in a MySQL table from which I need to select a few items, many times. For example, I might want all entries with a number field between 1 and 10.
In SQL this would be easy:
SELECT text FROM table WHERE number>=1 AND number<10;
If I extract the entire table to a Python list:
PyList = [[text1, number1], [text2, number2], ...]
I could then extract those same text values I want by running through the entire list
for item in PyList
if item[1] >=1 and item[1]<10:
result.append(item[0])
Now, the performance question between the two is that I have to do this for a sliding window. I want to get those between 1 and 10, then 2 and 11, 3 and 12, ... 14990 and 15000 What approach is faster for a list this big?
An improvement in Python I'm thinking about is to pre-order the Python list by number. When the window moves I could remove the lowest value from result
and append all elements verifying the next condition to get the new result
. I would also keep track of index in the PyList so I would know where to start from in the next iteration. This would spare me from running through the entire list again.
I don't know how to speed up the MySQL for successive Selects that are very similar and I don't know how it works internally to understand performance differences between the two approaches.
How would you implement this?