简体   繁体   中英

Calculate and minimize row size to get around effective MySQL column limit

I've read the more nuanced responses to the question of how many columns is too many and would like to ask a follow up.

I inherited a pretty messy project (a survey framework), but one could argue that the DB design is actually properly normalized, ie a person really has as many attributes as there are questions.
I wouldn't defend that notion in a debate, but the more pressing point is that I have very limited time, I'm trying to help users of this framework out and the quickest fix I can think of right now is reducing the row size. I doubt I have the skill to change the DB model in the time I have.

The column number now is 4661, but they can hopefully reduce it to at least 3244, probably less (by reducing the actual number of questions). The hard column limit is 4096, but right now I don't even succeed in adding 2500 columns, presumably because of the row size limit which is 65,535 bytes.

However, when I calculate my row size I end up with a much lower value, because nearly all columns are TINYINTs (survey responses ranging from 1-12). It doesn't even work with 2000 TINYINTs ( an example query that fails ).

Using the formula given in the documentation I get 4996 bytes or less.

column.lengths = tinyints * 1
null.columns = length(all.columns)
variable.lengths.columns = 0
(row.length = 1+
    (column.lengths)+ 
    (null.columns + 7)/8+ 
    (variable.lengths.columns)
)
## 4996

What did I misunderstand in that row length calculation?

I overlooked this paragraph

Thus, using long column names can reduce the maximum number of columns, as can the inclusion of ENUM or SET columns, or use of column or table comments.

I had long column names, replacing them with sequential numbers allowed me to have more columns (ca. 2693), I'll have to see if the increase is sufficient. I don't know how they're saved, presumably as strings, so maybe I can reduce them even further using letters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM