Simple question: Should OPTIONAL User Settings be Lazy-Initialized into the DB or always created with a new registration?
A user can set additional settings which are optional, so should a row for that optional setting be created for every user upon registration or only created when a user makes use of those settings for the first time?
Lazy-Initialization saves space, so I’m leaning towards doing it this way, but I’m not sure if there are any drawbacks.
Here is one particular drawback I ran into when using lazy initilization in MySQL. It all boils down to this quote from the MySQL docs:
First: The setup
Connect to your DB (in my case MySQL) Then create a simple function SetBarProperty() that sets the @bar property EXPLICITLY to the value passed to the function. The function itself simply returns the value passed to it.
Now if we query for the @bar property, it will be NULL just as expected.
Again, if we call the SetBarProperty(3); the result is just as expected.
What do you think?
What do you think happens if we call the following query?
You’ve guessed right! @bar is 4.
And now the tricky part…
Disconnect from your DB server (this is very important):
Now re-connect and call the same query as before:
Did you see what just has happened?
@bar was NULL and this is because it hasn’t been initialized in the caller space of the function SetBarProperty().
If you disconnect from your DB and explicitly initialize the property BEFORE calling the function (that itself initializes it explicity) your query works just as expected:
Recommendation
If you immediately use a property in a query just after you’ve called a function that modifies the same property you should initialize the property before calling the function.
I hope this helps!
Regards,
Konrad