Search code examples
djangodatabasedjango-1.10

How to get a queryset estimated size on the DB?


I'm running a cleanup process each month that deletes old unnecessary objects from the database. Is there a way to get an estimation of the file size gained from the cleanup process?

I'm working with both sqlite3 and PostgreSQL DBs, but of course a DB-independent solution would be preferred.


Solution

  • Looks like a good estimation would be to go to each object in the queryset, iterate though the __dict__ values, and call __sizeof__ upon every object.

    Given that the queryset is qs:

    total_est_size = sum([v.__sizeof__() for rec in qs for v in rec.__dict__.values()])
    

    Of course, this is a very basic estimation that doesn't take account many complicated implementations.