I have a table of user entries, and for every entry I have an array of (2-byte) integers to store (15-25, sporadically even more). The array elements will be written and read all at the same time, it is never needed to update or to access them individually. Their order matters. It makes sense to think of this as an array object.
I have many millions of these user entries and want to store this with the minimum possible amount of disk space. I'm however struggling with MySQL's lack of Array
datatype.
I've been considering the following options.
- Do it the MySQL way. Make a table
my_data
with columnsuser_id
,data_id
anddata_int
. To make this efficient, one needs an index onuser_id
, totalling well over 10 bytes per integer. - Store the array in text format. This takes ~6.5 bytes per integer.
- making 35-40 columns ("enough") and having -32768 be 'empty' (since this value cannot occur in my data). This takes 3.5-4 bytes per integer, but is somewhat ugly (as I have to impose a strict limit on the number of elements in the array).
Is there a better way to do this in MySQL? I know MySQL has an efficient varchar
type, so ideally I'd store my 2-byte integers as 2-byte chars in a varchar
(or a similar approach with blob
), but I'm not sure how to do that. Is this possible? How should this be done?