Lego Organization and Databases

Photo by Xavi Cabrera on Unsplash

While driving my kids to school this morning, my oldest was lamenting a project he had to do in computer class. They had a big box of Legos and have to physically construct them to match pixel art. He had the usual questions of “What is the point of this?” and “How is this going to help me later in life?”

My answer, since he had been dabbling with programming Python, is that it teaches him attention to detail. “You know how you get all of those error messages in Replit when you have typos in your code?”

His other criticism of the project revolved around how difficult it was to find the needed pieces in the Lego bin. He needed specific shapes, sizes, and colors and those are hard to find when searching through thousands of pieces.

He felt that they should be organized somehow. When I inquired as to how he thought they should be organized he replied, “I needed a 1×3 blue piece but couldn’t find one. If those were all separated then it would have been easy to find.”

The combination of shapes, sizes, and colors would certainly reach the hundreds or thousands and that many bins wouldn’t fit in the classroom. So, having three dimensions to organize by would be too many.

I pointed out to him that we had this same problem at home and I had tried to organize the Legos by a single dimension: width. All straight Legos one dots wide went into a large Ziplock bag. All straight Legos two dots wide went into a second bag. (I also did all flat pieces in a third bag.)

He agreed that my simple organization model would have made his life easier while making his Lego pixel art. So, any amount of organization would be better than none in this case.

Ultimately even my organization system failed and the Legos again ended up mixed together in big bins. Why?

While having bins of separated Legos makes them easier to find, it also makes it much harder to put them away. The more dimensions you need to separate by the longer it will take to find the correct bin to place it in.

It turns out that with Legos, one dimension to sort by is one too many.

This analogy is a very simple one for describing databases. The more tables you split your data across, the longer it takes to insert. If we had database tables and put all of the flat pieces in one table and all of the straight pieces in another table (don’t even get me started on all of the one-off shapes the sets come with these days) then with each Lego you would need to figure out what table it goes into before inserting it. A notebook that lists all of the Legos and the bins they are in would be similar to a database index.

The more tables and indexes you have the slower it is to put the data into the database but the quicker it is to retrieve the data you are looking for.

I assume somewhere out there is a Raspberry Pi robot that applies speech and image recognition to sort through your Lego bins for you. Maybe my kid will build that with his Python skills.