简体   繁体   English

需要在python中找到表的memory大小

[英]Need to find the memory size of the table in python

I need to find the memory size of the table using Python. I have used this command:我需要使用 Python 找到表的 memory 大小。我使用了这个命令:

memory_size = self.df.memory_usage(deep=True).sum() memory_size = self.df.memory_usage(deep=True).sum()

and I got the result of "37718821".我得到了“37718821”的结果。 I'm not sure which units the value is in.for example, KB or MB.我不确定该值的单位是什么。例如,KB 或 MB。

For the same table, I got the memory size in SQL, which is "19.757812 MB", I am so confused.对于同一张表,我在 SQL 中得到了 memory 大小,即“19.757812 MB”,我很困惑。

Please help me to get the memory size of the specific data frame in MB using Python.请帮助我使用 Python 获取特定数据帧的 memory 大小(以 MB 为单位)。

Response for the replayed answer:对重播答案的回应:

memory_size = self.df.memory_usage(deep=True).sum()
memory_size_mb = memory_size / (1024 * 1024)

I used the above command I got the answer as 35.97147083282 MB, but while checking the same table size in SQL using this command.我使用上面的命令得到的答案是 35.97147083282 MB,但是在使用此命令检查 SQL 中的相同表大小时。

SELECT   
o.name AS [table name],
sum(p.reserved_page_count) * 8.0 / 1024  AS [size_in_mb],
p.row_count AS [records]
FROM  
sys.dm_db_partition_stats AS p,
sys.objects AS o
WHERE   
p.object_id = o.object_id
AND o.is_ms_shipped = 0 
GROUP BY o.name , p.row_count
ORDER BY o.name DESC

I got the size as 19.757812 MB我得到的大小为 19.757812 MB

I am confused我很迷惑

Which is Correct?哪个是正确的?

Thanks Manoranjini谢谢马诺兰吉尼

The value you're getting from the memory_usage() method is in bytes.您从 memory_usage() 方法获得的值以字节为单位。 To convert it to megabytes, you can divide it by 1,048,576 (which is 2^20, the number of bytes in a megabyte).要将其转换为兆字节,您可以将其除以 1,048,576(即 2^20,即 1 兆字节中的字节数)。 Here's an example of how you could do the conversion:以下是如何进行转换的示例:

memory_size_mb = memory_size / (1024 * 1024)
print(memory_size_mb, "MB")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM