在mysql中 information_schema 数据库中有一个 TABLES 表, 记录了所有库已经所有表的详情信息,包括: 表名,表容量, 表内数据行数, 引擎,时间, 索引大小等
django中获取表大小的方法:
from django.db import connection
class DeleteAuditLog(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAdminUser,)
throttle_classes = (UserRateThrottle,)
def get(self, request):
cursor = connection.cursor()
cursor.execute(
"select truncate(data_length/1024/1024, 2) from information_schema.tables where table_schema='库名' and table_name='表名'"
)
ret = cursor.fetchone()
logging.info(ret[0])
cursor.close()
return Response({'size':'%s MB'%ret[0]})