What is difference between storing data in a blob, vs. storing a pointer to a file?

后端 未结 5 1049
醉酒成梦
醉酒成梦 2020-12-13 00:54

I have a question about the blob data type in MySQL.

I read that the data type can be used to store files. I also read that an alternative is to store

5条回答
  •  渐次进展
    2020-12-13 01:39

    If you store data is BLOB field, you are making it part of your object abstraction.

    BLOB advantages:

    1. Should you want to remove row with BLOB, or remove it as part of master/slave table relationship or maybe the whole table hierarchy, your BLOB is handled automatically and has same lifetime as any other object in database.

    2. Your scripts do not have a need to access anything but database to get everything they require. In many situations, having direct file access open whole can of worms on how to bypass access or security restrictions. For example, with file access, they may have to mount filesystems which contain actual files. But with BLOB in database, you only have to be able to connect to database, no matter where you are.

    3. If you store it in file and file is replaced, removed or no longer accessible, your database would never know - in effect, you cannot guarantee integrity. Also, it is difficult to reliably support multiple versions when using files. If you use and depend on transactions, it becomes almost impossible.

    File advantages:

    1. Some databases handle BLOBs rather poorly. For example, while official BLOB limit in MySQL is 4GB, but in reality it is only 1MB in default configuration. You can increase this to 16-32MB by tweaking both client and server configuration to increase MySQL command buffer, but this has a lot of other implications in terms of performance and security.

    2. Even if database does not have some weird size limits, it always will have some overhead in storing BLOB compared to just a file. Also, if BLOB is large, some databases do not provide interface to access blob piece by piece, or stream it, which can be large impediment for your workflow.

    In the end, it is up to you. I typically try to keep it in BLOB, unless this creates unreasonable performance problems.

提交回复
热议问题