Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6785645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:09:09+00:00 2026-05-26T17:09:09+00:00

Simple question: Should OPTIONAL User Settings be Lazy-Initialized into the DB or always created

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:09:10+00:00Added an answer on May 26, 2026 at 5:09 pm

    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:

    User-defined variables are connection-specific.

    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.

    $ mysql
    mysql> DROP FUNCTION IF EXISTS SetBarProperty;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> DELIMITER $$
    mysql> CREATE FUNCTION SetBarProperty( myValue INT(30) )
        -> RETURNS INT(30)
        -> DETERMINISTIC
        -> READS SQL DATA
        -> BEGIN
        ->     SET @bar := myValue;
        ->     RETURN myValue;
        -> END $$
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> DELIMITER ;
    

    Now if we query for the @bar property, it will be NULL just as expected.

    mysql> SELECT @bar;
    +------+
    | @bar |
    +------+
    | NULL |
    +------+
    1 row in set (0.00 sec)
    

    Again, if we call the SetBarProperty(3); the result is just as expected.

    mysql> SELECT SetBarProperty(3);
    +-------------------+
    | SetBarProperty(3) |
    +-------------------+
    |                 3 |
    +-------------------+
    1 row in set (0.00 sec)
    

    What do you think?

    What do you think happens if we call the following query?

    SELECT SetBarProperty(4), @bar;
    

    You’ve guessed right! @bar is 4.

    mysql> SELECT SetBarProperty(4), @bar;
    +-------------------+------+
    | SetBarProperty(4) | @bar |
    +-------------------+------+
    |                 4 |    4 |
    +-------------------+------+
    1 row in set (0.00 sec)
    

    And now the tricky part…

    Disconnect from your DB server (this is very important):

    Ctrl+C
    

    Now re-connect and call the same query as before:

    $ mysql
    mysql> SELECT SetBarProperty(4), @bar;
    +-------------------+------+
    | SetBarProperty(4) | @bar |
    +-------------------+------+
    |                 4 | NULL |
    +-------------------+------+
    1 row in set (0.00 sec)
    

    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:

    Ctrl+C
    $ mysql
    mysql> SET @bar := NULL;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> SELECT SetBarProperty(4), @bar;
    +-------------------+------+
    | SetBarProperty(4) | @bar |
    +-------------------+------+
    |                 4 | 4    |
    +-------------------+------+
    1     row in set (0.00 sec)
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question that keeps bugging me. Should I HTML encode user input right away
This should be a simple question, but I just can't recall the relevant API.
This should be a simple question, but I can't get it to work :(
This should be a simple question, but I just can't seem to figure it
This should be fairly simple question. I'm using DocX library to create new word
I always stumble upon this (rather simple question) when making e.g. Wordpress blogs. Should
Simple question. Should I declare any method that can be const a const method?
This should be a simple question, but I can't seem to figure it out.
I'm just trying to figure out a probably simple question. Should views set model
I have what should be a relatively simple question that I can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.