Mysql – Does combining columns into 1 column helps to optimize a large table

MySQLoptimization

I have user, items and user_items tables, and as usual user has many items.
Tables contains:
User: ~50K
Items: ~3.5M
UserItems: ~5M

I am using MySQL and most of my queries are selecting items of a user.
So I wonder If it helps to keep data on UserItems table as follows,

user_id item_ids
1 1,22,23,45,66…

Then after getting ids, simply parse to get item ids.
Any suggestion/experience?

Best Answer

This is a bad idea.

Putting more than one data element into a single field invariably leads to headaches.

You will be much better off making a new table to store this data, like:

Table UserItems

  • UserID int
  • ItemId int

And then one row per item per user. You can query this as needed and update it without any issues.

Think about adding one additional item to your proposed structure - it would be a challenge since you need to concatenate another space and another id to the end of the existing string.

It will also be a pain to check for a single item in that structure since you need to do a string search for every record.