Whether you learn how to build applications for Ruby, Node.js, or iOS, it can be easy — at first — to lose sight of how you store data, like user records. Frameworks like Rails simplify the way you interact with databases, so it can often seem like the actual database you’re using behind the scenes doesn’t matter.
This illusion slowly dissolves over time, though, as your applications grow and become more complex. You may start noticing performance bottlenecks impacting them in strange new ways. One of those bottlenecks is due to N+1 queries — which happens when fetching a single record results in fetching a surprisingly gigantic amount of associated records. It’s difficult to understand why N+1 queries are so problematic before discovering how SQL JOIN queries work and what type of JOIN is more appropriate for your specific situation. You may trigger 100 individual queries when fetching 10 records simply because each of them has 10 associated records.
It’s also important to understand the benefits (and tradeoffs) of data normalization— breaking down records into neatly specific tables and columns in order to reduce redundancy. While a normalized database often makes sense to avoid storing the same data in multiple places, there are many situations in which denormalization (the reverse process) becomes a necessary step. So for instance, when you have too many JOIN queries between an increasing number of tables, some data that’s often queried for together should belong in the same table to improve database performance.
It might even make sense to switch to non-relational document-oriented databases, like MongoDB, in situations where associated data should instead be nested or unstructured — user records and user preferences, for example. In that case, there are always going to be more preferences added to your application over time, so it’s important to think about if you really need to add new columns every single time.
Are databases scaring you away? Don’t worry, they shouldn’t be — but they’re definitely something to keep on your radar. If you’re ready to dive into them, check out our brand new Database Path! It includes both SQL and NoSQL courses, so you can learn the intricacies of relational databases and discover good uses for document databases.