Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django command for table size or tuple size (physical memory)?

I made a project in django, python.I would like to know Django command for knowing what's memory size of my table

like image 395
user578542 Avatar asked Nov 23 '11 14:11

user578542


2 Answers

Mixing both answers, you could calculate the size of the whole table calculating first the size of a single object of your model and then multiplying it with the number of objects you have in your table:

$ python manage.py shell

>>> import sys
>>> from myapp.models import MyModel
>>> my_model = MyModel()
>>> total_size = sys.getsizeof(my_model) * MyModel.objects.count()

This could work, but I think the right thing to do is to calculate the size of the table using the database. For example, if your using PostgreSQL, you just have to do:

SELECT pg_database_size('myapp_mymodel');

pg_database_size
------------------
         63287944
(1 row)
like image 58
juliomalegria Avatar answered Oct 17 '22 05:10

juliomalegria


from django.db import connection
with connection.cursor() as c:
    c.execute("SELECT pg_size_pretty(pg_total_relation_size('my_table'))")
    c.fetchall()

e.g. >> [('7403 MB',)]

like image 40
ZuLu Avatar answered Oct 17 '22 07:10

ZuLu