将文件名存储在 varchar 数据类型列中好吗?

Is storing filename in varchar datatype column good?

将文件名存储在 varchar datatype 列中是否合适?有没有其他方法可以为您的软件处理文件,例如通过数字 Id 创建文件夹并仅检查应用程序逻辑中文件夹中是否存在文件?

谢谢!

我建议文件名应该是varchar。

拥有一个 数字 ID 通常是 mysql 性能的一个好习惯。例如,int 最大大小为 4 字节。但是 varchar 最大大小可达 9-12 字节。 Varchar 还使用不同的字符集,如 utf 等,这就是为什么(其他一些原因)它需要更多 space.

If the varchar will be 20 characters, and the int is 4, then if you use an int, your index will have FIVE times as many nodes per page of index space on disk... Which means that traversing the index will require one fifth as many physical and/or logical reads.

这在某种程度上取决于您的 数字 ID 长度 文件名字符长度 。而且,拥有数字 id 可能会使您的情况变得有点复杂。因为即使数值的微小变化也可能会改变文件名,或者文件名的变化也可能会改变数值。

另外,请记住,当您输入新文件名时,您应该编写脚本或做一些事情。必须将文件名转换为数字 ID。如果文件名始终保持不变,数字 ID 可能是一个不错的选择。

Finally, I would say it all depends on your application, size of table (no of rows), length of file name, length of numeric id etc.