A SQL Query, An Obvious Addition and A New Morning Routine — 3 Problems I solved this week
By: Kumar Ashish
Tags: Sql, First Post, Software Engineering, Freshers

A SQL Query, An Obvious Addition and A New Morning Routine — 3 Problems I solved this week Kumar Ashish Dec 1, 2019·3 min read

This week, I solved a handful of problems and among those, the three that I feel were the most important are the subject of this article

The title mentions a SQL Query, which is of course the first problem that I solved. So, I had a table whose single row corresponded to a user, a custom field and a custom field value, something like

User_1, City, Pune

User_1, City, Mumbai

User_1, Sport, Football

User_2, Sport, Cricket

The fields weren’t predefined, i.e. they were custom and the query that you had to write was given a set of custom fields and their custom values, you had to return a list of all the users who matched the criteria.

Custom Field — City, Values — Pune, Mumbai

Custom Field — Sport, Value — Football

The result set is supposed to contain all users whose city is Pune AND Mumbai, AND whose sport is Football.

After trying many incorrect versions and some correct but slow recursive calls, a query with self join gave a fast and accurate result. Going from incorrect iterations to a solution which was in seconds, to the final solution which worked in milliseconds was obviously a moment of joy for me.

The second problem that I solved wasn’t as such a problem, and the solution wasn’t exactly something out of the box. I was supposed to migrate the data from ElasticSearch to MySQL, and the process of writing the script involved a lot of iterations of optimisations. After the migration was done, there was a separate table to denote the completion status of migration, i.e. whether all docs were migrated successfully or there was some failure. After every optimisation, I wouldn’t know exactly if this version of the script was the fastest of the lot. So, I made some simple changes to migration status table, added a column for total time taken for the last migration, time taken during the fastest migration and the version of the fastest migration (which I maintained by storing the epoch time corresponding to the start time of the migration). Definitely not something out of the box, probably something I should have done from the beginning, but something that definitely is helpful.

The third problem that I solved was that of not giving enough time for personal growth. I understand that just completing the work that I am given in my profession alone isn’t going to help me grow, and that I need to take out some time everyday to learn, read or write something new. I was determined to assign few hours everyday after work for this, but that seldom did work as I was mostly tired and easily got distracted by notifications on my cell phone.

This article https://medium.com/better-humans/how-to-wake-up-at-5-a-m-every-day-ceb02e29c802 provided a perfect solution for my woes, go to sleep early, wake up early and just devote this time for my personal growth. I think I am indeed mesmerised by the simplicity of this routine. I have been doing it for just 3 days now, but going to work after 5 hours of pure learning and doing things I want has been blissful for me.

By the way, my solution for the SQL query

SELECT user_id

FROM user_filter filter1

JOIN user_filter filter2 ON filter1.user_id = filter2.user_id

WHERE

filter1.filed_name = ‘city’ AND filter1.field_value IN (‘Pune’, ‘Mumbai’)

AND filter2.field_name = ‘sport’ AND filter2.filed_value IN (‘Football’);

You would need to have as many self joins as the number of filters. Also, I made sure to use indexed fields / primary keys in my query to reduce time. There might obviously be better solutions, let me know if you have them.