问题
I was reading MySQL documentation and I was wondering whether MyISAM's "static" table format also applies on InnoDB or not? Do I gain performance, if I use CHAR
instead of VARCHAR
when the real lengths varies between 0 and 100 characters or is using VARCHAR(100)
better?
回答1:
InnoDB does have fixed-width rows and there can be some advantage to declaring all columns in a table as fixed-width. The main benefit is that all "holes" in a page can be reused for any insertion, since all rows are the same length. This makes space re-use somewhat more efficient. I would not try to force fixed-width for any strings over a few dozen bytes though, as the cost of storing the extra data per page (and thus fewer rows per page) would quickly overwhelm any savings/gains you'd get from more efficient space re-usage.
However, having fixed-width rows does not allow for any optimization of row-scanning (like it does for MyISAM) whatsoever. InnoDB's row storage is always page-based and uses a B+tree with doubly-linked lists of pages, and singly-linked records within a page. Row traversal is always using these structures and can't be done any other way.
回答2:
With CHAR
fields, what you allocate is exactly what you get. For example, CHAR(15)
allocates and stores 15 bytes, no matter how characters you place in the field. String manipulation is simple and straightforward since the size of the data field is totally predictable.
With VARCHAR
fields, you get a completely different story. For example VARCHAR(15)
actually allocates dynamically up to 16 bytes, up to 15 for data and, at least, 1 additional byte to store the the length of the data. If you have the string 'hello' to store that will take 6 bytes, not 5. String manipulation must always perform some form of length checking in all cases.
I hope it's useful for you
来源:https://stackoverflow.com/questions/21596981/does-innodb-have-static-tables