Images in MySQL

download Images in MySQL

of 24

Transcript of Images in MySQL

  • 8/14/2019 Images in MySQL

    1/24

    Storing Images in MySQL

    Boston MySQL Meetup GroupMonday June 12th, 2006

    Sheeri [email protected]

    http://www.sheeri.com

    mailto:[email protected]:[email protected]
  • 8/14/2019 Images in MySQL

    2/24

    A BLOB By Any Other Name

    Text is data

    BLOB is data

    Images are BLOBs

  • 8/14/2019 Images in MySQL

    3/24

    Similarities

    Syncing images across multiple servers

    Web server image caching

    Performance

    Adding another server

  • 8/14/2019 Images in MySQL

    4/24

    Similarities

    A filesystem is a DB

    Isolation locking

    Durability transactions committed not lost

  • 8/14/2019 Images in MySQL

    5/24

    Why Not?

    Filesystems store files

    Hard to backup/restore/repair

    Partial restore tedious

  • 8/14/2019 Images in MySQL

    6/24

    Why Not?(continued)

    Binary logging doubles space

    More db connections, queries

    Bottleneck

  • 8/14/2019 Images in MySQL

    7/24

    Why Not?(continued)

    Database fragmentation

    Portability

    Single storage engine for all data ???

  • 8/14/2019 Images in MySQL

    8/24

    Why?

    Snapshots easier, less expensive

    Scalability

    Indexing and sorting

    Access Control

  • 8/14/2019 Images in MySQL

    9/24

    Why?(continued)

    Atomicity

    Consistency

    Database features

    Remote storage easier

  • 8/14/2019 Images in MySQL

    10/24

    Why?(continued)

    Replication easy

    Single storage engine for all data

    Dual-level caching

  • 8/14/2019 Images in MySQL

    11/24

    Why?(continued)

    Oracle recommends it

    http://www.oracle.com/technology/products/intermedia

    /htdocs/why_images_in_database.html

  • 8/14/2019 Images in MySQL

    12/24

    Caveats

    SELECT *

    Full table scan

    max_allowed_packet

  • 8/14/2019 Images in MySQL

    13/24

    Compromise

    Do both

    Originals in db

    Written to disk

    Transformations also written to disk

  • 8/14/2019 Images in MySQL

    14/24

    What We Chose

    700,000 users, 6 pictures each

    20,000 online at peak time

    Online user image retrieval

  • 8/14/2019 Images in MySQL

    15/24

    What We Chose

    Add 17-19,000 users a week

    Possible 102-114,000 pictures a week

    Need referential integrity

  • 8/14/2019 Images in MySQL

    16/24

    BLOB Sizes

    TINYBLOB 2^8 bytes

    BLOB 2^16 bytes MEDIUMBLOB

    LONGBLOB

    variable-length

    256 bytes

    64 Kb16 Mb

    4 Gb

  • 8/14/2019 Images in MySQL

    17/24

    What Storage Engine?

    1 datafile per table?

    Table-level or row-level locking?

    Compressed, read-only storage?

    Fulltext indexing?

  • 8/14/2019 Images in MySQL

    18/24

    What Storage Engine?(continued)

    InnoDB for row-level locking

    Can specify 1 datafile per table if desired

    No captions or text

  • 8/14/2019 Images in MySQL

    19/24

    Architecture

    2 tables 1 fixed-length

    Single write master to many read slaves

  • 8/14/2019 Images in MySQL

    20/24

    Creating the Table

    CREATE TABLE ImageMd (imageNum int unsigned not null primary key,

    ownerUid int unsigned not null,

    modified timestamp,

    uploaded datetime) ENGINE=InnoDB

  • 8/14/2019 Images in MySQL

    21/24

    Creating the Table

    CREATE TABLE Images (

    imageNum int unsigned not null primary keyauto_increment,

    modified timestamp,

    image mediumblob) ENGINE=InnoDB

  • 8/14/2019 Images in MySQL

    22/24

    Populating the Tables

    LOAD_FILE('path/to/file.jpg')(max_allowed_packet)

    Application code

  • 8/14/2019 Images in MySQL

    23/24

    Retrieving the Images

    Browser needs to know the data is an image

  • 8/14/2019 Images in MySQL

    24/24

    Questions?

    Who's done this?

    Other stories....