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

后端 未结 5 1039
醉酒成梦
醉酒成梦 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:42

    Yes, MySQL blobs that don't fit within the same page as a row get stored on overflow pages Note that some blobs are small enough that they're stored with the rest of the row, like any other column. The blob pages are not adjacent to the page their row is stored on, so they may result in extra I/O to read them.

    On the other hand, just like with any other page type, blob pages can occupy memory in the InnoDB buffer pool, so reading the blobs subsequently is very fast even if they are on separate pages. Files can be cached by the operating system, but typically they're read from disk.

    Here are a few other factors that may affect your decision:

    • Blobs are stored logically with a row. This means if you DELETE the row, the associated blob is deleted automatically. But if you store the blob outside the database, you end up with orphaned blob files after you delete rows from the database. You have to do manual steps to find and delete these files.

    • Blobs stored in the row also follow transaction semantics. For instance, a new blob or an updated blob is invisible to other transactions until you commit. You can also roll back a change. Storing blobs in files outside the database makes this a lot harder.

    • When you back up a database containing blobs, the database is a lot bigger of course, but when you backup, you get all the data and associated blobs in one step. If you store blobs externally, you have to back up the database and also back up the filesystem where you store blob files. If you need to ensure that the data and blobs are captured from one instant in time, you pretty much need to use some kind of filesystem snapshots.

    • If you use replication, the only automatic way of ensuring the blobs get copied to the replication slave automatically is to store blobs in the database.

提交回复
热议问题