Reducing database operation time Django/ Mysql
<p>So, the thing is I am having a moderately large list of emails ~ 250,000 entries.</p>

<p>I have another table containing list of invalid emails ~ 50,000 which i need to remove (mark inactive) from 1st table. For that I have ran a simple django function which is taking 3-4 seconds in each loop. The code is:</p>

<pre><code>def clean_list():
    id = 9
    while id&lt;40000:
        i = Invalid.objects.get(id=id)
        y = i.email.strip()
        f = IndiList.objects.get(email__contains=y)
        f.active = False
        f.save()
        id +=1
</code></pre>

<p>What would be a better way to do it? Either a SQL query or a better piece of django code or some other way.</p>

<p>Help!</p>

<p>After a few iterations, i used this function which was more than <strong>1000 times faster.</strong></p>

<pre><code>def clean_list3():
    pp = Invalid.objects.filter(id__gte=9)
    listd = [oo.email.strip() for oo in pp]
    for e in IndiList.objects.all():
        if e.email.strip() in listd:
            e.active=False
            e.save()
            print(e.id)
</code></pre>

<p>The trick is simple, instead of hitting database every time, I saved the 250,000 objects in a queryset in memory. and also the list of emails from invalid list in memory. </p>

<p>And then i had to hit database only when we found matching emails so as to save it as inactive.</p>

python mysql django database