0

What is the proper way to handle file sizes when using Sql Server's FILESTREAM feature to store documents in the database?

I know that you can use the TSQL command DATALENGTH() on the filestream column, but regarding to this Microsoft article this might not always be the best idea:

Avoid retrieving the data length of lots of BLOB files in an application. This is a time-consuming operation because the size is not stored in the SQL Server Database Engine. If you must determine the length of a BLOB file, use the Transact-SQL DATALENGTH() function to determine the size of the BLOB if it is closed. DATALENGTH() does not open the BLOB file to determine its size.

I am a bit unclear if the above says that you should avoid DATALENGTH() if possible or if it is the recommended way to retrieve the file size.

If I have to access document sizes often to show it to the user as metadata, what is the recommended way to retrieve them? Store them in a separate field? Or is datalength() fast enough?

4

1 回答 1

1

我相信这篇文章建议不要使用 DATALENGTH() 如果可以避免的话。

将文件大小以及有关文件的其他元数据(内容类型、扩展名、时间戳等)存储在单独的列中是一种合适的选择。这将允许您更有效地查询/过滤文件详细信息。

如果预计文件内容不会更改,那么在保留文件流数据的同时简单地更新文件大小列是最简单的。

如果预计内容会发生变化,您还需要记住更新文件大小。这也可以在 Sql 中使用持久计算列来完成

于 2014-03-28T17:08:46.340 回答