MDF 文件大小比实际数据大得多

发布于 2024-09-14 06:51:28 字数 546 浏览 1 评论 0原文

由于某种原因,我的 MDF 文件有 154gig,但是,我只从平面文件加载了 7GB 的数据。为什么 MDF 文件比实际源数据大很多?

更多信息:

只有几个表,大约有 2500 万行。没有大型 varchar 字段(最大为 300,大多数小于 varchar(50)。不是很宽的表 < 20 列。此外,还没有对大型表建立索引。带有索引的表少于 100 万行。我不知道不使用 char,仅使用 varchar 表示字符串不是问题。

结果是日志文件,而不是 mdf 文件,这看起来更合理,但仍然很大

更新:

我修复了 。通过将恢复模式从 FULL 更改为 simple,可以解决 LDF(日志)文件的问题。这没关系,因为该服务器仅用于内部开发和 ETL 处理。此外,在更改为 SIMPLE 之前,我必须缩小 LOG 文件。在大多数情况下不建议这样做,但是,这是日志文件不应该增长得如此之大和如此之快的情况之一。要进一步阅读,请参阅这个

For some reason my MDF file is 154gigs, however, I only loaded 7 gigs worth of data from flat files. Why is the MDF file so much larger than the actual source data?

More info:

Only a few tables with ~25 million rows. No large varchar fields (biggest is 300, most are less than varchar(50). Not very wide tables < 20 columns. Also, none of the large tables are indexed yet. Tables with indexes have less than 1 million rows. I don't use char, only varchar for strings. Datatype is not the issue.

Turned out it was the log file, not the mdf file. The MDF file is actually 24gigs which seems more reasonable, however still big IMHO.

UPDATE:

I fixed the problem with the LDF (log) file by changing the recovery model from FULL to simple. This is okay because this server is only used for internal development and ETL processing. In addition, before changing to SIMPLE I had to shrink the LOG file. Shrinking is not recommended in most cases, however, this was one of those cases where the log file should have never grown so big and so fast. For further reading see this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老子叫无熙 2024-09-21 06:51:28

可能有很多原因,也许您正在使用 char(5000) 而不是 varchar(5000),也许您正在使用 bigint 而不是 int、nvarchar,而您需要的只是 varchar 等。也许您每个都使用了很多索引表,这些都会加起来。也许您的自动增长设置是错误的。您确定这是 MDF 而不是 LDF 文件,对吗?

Could be a lot of reasons maybe you are using char(5000) instead of varchar(5000), maybe you are using bigints instead of int, nvarchar when all you need is varchar etc etc etc. Maybe you are using a lot of indexes per table, these will all add up. Maybe your autogrow settings are wrong. You are sure this is the MDF and not the LDF file right?

小女人ら 2024-09-21 06:51:28

因为MDF分配了154Gb,或者通过各种操作已经增长到154Gb。数据库文件的大小至少与其中的数据大小相同,但它可以比已使用的数据量大任意数量。

一个明显的问题是如何衡量数据库中的数据量?您是否使用过sp_spaceused?您检查过 sys.allocation_units 吗?你猜到了吗?

如果使用的大小确实是 154Gb 中的 7Gb,那么您应该保持原样。数据库的大小是由某人调整到这个大小的,或者已经增长了,并且很可能会增长回来。如果您认为增长或预调整大小是偶然的,那么前一点仍然适用,您应该保持原样。

如果您绝对肯定过度分配是一个错误,则可以使用所有 收缩的负面后果

Because the MDF was allocated with 154Gb, or has grown to 154Gb through various operations. A database file has at least the size of the data in it, but it can be larger than the used amount by any amount.

An obvious question will be how do you measure the amount of data in the database? Did you use sp_spaceused? Did you check sys.allocation_units? Did you guess?

If the used size is indeed 7Gb out of 154Gb, then you should leave it as it is. The database was sized by somebody at this size, or has grown, and it is likely to grow back. If you believe that the growth or pre-sizing was accidental, then the previous point still applies and you should leave it as is.

If you are absolutely positive the overallocation is a mistake, you can shrink the database, with all the negative consequences of shrinking.

画▽骨i 2024-09-21 06:51:28

以防万一这对那里的人有用,在 dba.stackexchange 中找到这个查询,它使用 sys.dm_db_database_page_allocations 来计算每个对象的页面数,这包括内部存储,并为您提供了您所使用的空间的真实概述数据库。

SELECT sch.[name], obj.[name], ISNULL(obj.[type_desc], N'TOTAL:') AS [type_desc],
       COUNT(*) AS [ReservedPages],
       (COUNT(*) * 8) AS [ReservedKB],
       (COUNT(*) * 8) / 1024.0 AS [ReservedMB],
       (COUNT(*) * 8) / 1024.0 / 1024.0 AS [ReservedGB]
FROM sys.dm_db_database_page_allocations(DB_ID(), NULL, NULL, NULL, DEFAULT) pa
INNER JOIN sys.all_objects obj
        ON obj.[object_id] = pa.[object_id]
INNER JOIN sys.schemas sch
        ON sch.[schema_id] = obj.[schema_id]
GROUP BY GROUPING SETS ((sch.[name], obj.[name], obj.[type_desc]), ())
ORDER BY [ReservedPages] DESC;

感谢 Solomon Rutzky:

https://dba.stackexchange .com/questions/175649/sum-of-table-sizes-dont-match-with-mdf-size

Just in case this is useful for someone out there, found this query in dba.stackexchange, it uses the sys.dm_db_database_page_allocations which counts the number of pages per object, this includes internal storage and gives you a real overview of the spaced used by your database.

SELECT sch.[name], obj.[name], ISNULL(obj.[type_desc], N'TOTAL:') AS [type_desc],
       COUNT(*) AS [ReservedPages],
       (COUNT(*) * 8) AS [ReservedKB],
       (COUNT(*) * 8) / 1024.0 AS [ReservedMB],
       (COUNT(*) * 8) / 1024.0 / 1024.0 AS [ReservedGB]
FROM sys.dm_db_database_page_allocations(DB_ID(), NULL, NULL, NULL, DEFAULT) pa
INNER JOIN sys.all_objects obj
        ON obj.[object_id] = pa.[object_id]
INNER JOIN sys.schemas sch
        ON sch.[schema_id] = obj.[schema_id]
GROUP BY GROUPING SETS ((sch.[name], obj.[name], obj.[type_desc]), ())
ORDER BY [ReservedPages] DESC;

Thanks to Solomon Rutzky:

https://dba.stackexchange.com/questions/175649/sum-of-table-sizes-dont-match-with-mdf-size

苍白女子 2024-09-21 06:51:28

未启用自动收缩或初始大小设置为较大值。

Either AUTO SHRINK is not enabled or The initial size was set to the larger value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文