Skip Navigation

Help with understanding MySQL column and row size limits

To preface, I’m currently rewriting a personal webapp to use MySQL instead of storing everything in hundreds of JSON files. I’m currently in the testing phase of generating tables with the data from the JSON files, destroying the tables, adding more columns and data, repeat, all to make sure everything is working as intended.

My issue is that occasionally I’ll create too many columns and then I get an error saying something about the row being too large? I’ve also noticed that if I change the parameters of what data is allowed to go in the column, I can generate more columns. I know there is some relationship between number of columns, the data that can go in a column, data size, and row size but I don’t know what’s going on. I’d appreciate it if someone could broadly go over how row length(?) can affect number of columns.

Thank you

10

You're viewing a single thread.

10 comments
  • The answer is complicated, but TL;DR, MySQL has a hard coded limit of 65,535 bytes (that's 2^16 - 1, or 64k) per row. This excludes column types 'text' and 'blob,' which your storage engine will store separately. Each type of column you have requires a certain amount of storage space. For instance, this is why on a 'varchar' column you have to specify how many characters it can hold. Longer fields, more data required. Add all this up -- probably plus some overhead or other quirks I don't know off the top of my head -- and if the result is more than 64k per row, you're toast.

    MySQL supports multiple types of storage engines, and they can be different for each table. In general, InnoDB is the best one to use for most purposes (I'm sure I'll catch flak for this opinion) but each one technically has subtle pros and cons. InnoDB also has a column limit of 1017 columns per table, regardless of their composition. Exceed this, you are also toast.

    However. If your database design is such that you are running up against row storage limits, you're probably doing it wrong. You should reconsider how you're storing data and whether everything in whatever your app considers a single logical 'record' actually all has to be in the same table. (It probably doesn't.) So what are you doing that requires creating more than 1017 or greater than 64k of data per row?

    There is a complete, but mildly baffling per usual, explanation of MySQL's column count and row size limits here: https://dev.mysql.com/doc/refman/8.0/en/column-count-limit.html

10 comments