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>There are a couple optimisations you might want to take a look at. Instead of looping over a get for each object try getting a values list:</p>

<p><code>queryset = Invalid.objects.filter(id__range=(9,40000))
queryset_list = queryset.values_list('email' flat=True)</code></p>

<p><a href="https://docs.djangoproject.com/en/1.10/ref/models/querysets/#values-list" rel="nofollow">https://docs.djangoproject.com/en/1.10/ref/models/querysets/#values-list</a></p>

<p>then looping over the values list and doing a .get() on the email. At the end you can also do:</p>

<p><code>f.active = False
f.save(update_fields=['active'])</code></p>

<p>Which will only update the boolean field.
<a href="https://docs.djangoproject.com/en/1.10/ref/models/instances/#updating-attributes-based-on-existing-fields" rel="nofollow">https://docs.djangoproject.com/en/1.10/ref/models/instances/#updating-attributes-based-on-existing-fields</a></p>

<p>Also try to find a way to .get() the object via id or some other field than string if possible.</p>

python mysql django database