If you do not every piece of data in every case, you could divide record in several tables and make 1-to-1 relationship between them. And add default eager load to it.
Too many Columns / Fields in Table, ideas on how to resolve?
TLDR: My table has hundreds of columns. (Why explained below) skip to my question on how to combine multiple tables to act as 1.
So on my site I have a settings page where users enable and create specific connections to features on my site, and then enable/disable specific settings for each of the connections. They do this by inputting their unique id's for each connection, and then choosing which settings they want to use for each.
All of the "connections" are then passed through a resource json API, and the data is consumed with code elsewhere.
So I have a "connections" table that looks something like this:
id
user_id
connection1_id
connection1_settings1
connection1_settings2
connection1_settings3
connection2_id
connection2_settings1
connection2_settings2
connection2_settings3
3. 4. etc...
Then it uses a Model and Eloquent belongsTo to tie each one to a user.
Which was fine when I originally created it, because I only had a few connections and the table was quite small and seemed to fall within database normalization rules as all of the data is unique to each user, it's never duplicated and it's all related.
But my site has recently became quite popular, and many open source projects have asked to be added as a unique connection/option.
So this Table has grown from about a dozen columns, to over 200 columns. At about 120 Columns I started getting table size errors when migrating/adding new fields, but was able to temporarily resolve this by changing all the "settings" fields to tinyInteger's instead of strings, saving some space.
So my question:
Inevitably this table is going to get too large, with too many columns/fields. So to be proactive and resolve this before it grows into a bigger problem, what do you guys think would be the best way to split the future growth into multiple tables, and interlink them all into the one json?
I have used Pivot tables before to help spread out fields across multiple tables and keep things cleaner. But since the site already has thousands of users/rows in the original table, unless I'm mistaken it seems each time I add a new connection option. (Say Connection100_id, 200, 500, whatever.) I would have to "attach" each of those new options to the thousands of rows/users in the original table manually, for them to all link together with the existing table. (to prevent a bunch of "cannot find connection200_id" errors, etc.)
Is there any way to use Eloquent and a model to effectively continue expanding this into multiple tables without creating too much back end work to consume it? (I want it to act as much like a single table as possible.)
Thanks for any help/advice.
I would take it even one step further than @snapey's suggestion and have these fields in my table
- connection_id
- setting
- setting_type
and use a one to many relationship.
Please or to participate in this conversation.