{"id":5392,"date":"2022-04-08T12:50:23","date_gmt":"2022-04-08T19:50:23","guid":{"rendered":"https:\/\/coderpad.io\/?p=5392"},"modified":"2023-06-05T14:35:03","modified_gmt":"2023-06-05T21:35:03","slug":"sql-vs-nosql-with-james-quick","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/sql-vs-nosql-with-james-quick\/","title":{"rendered":"Dev Discussions: SQL vs NoSQL with James Quick of PlanetScale"},"content":{"rendered":"\n<p>We recently shared <a href=\"https:\/\/coderpad.io\/blog\/getting-hired\/dev-discussions-how-to-get-a-great-job-james-quick\/\" target=\"_blank\" rel=\"noreferrer noopener\">some excellent job advice<\/a> from YouTuber and Developer Advocate James Quick. <\/p>\n\n\n\n<p>But Corbin and James\u2019 conversation didn\u2019t end with that \u2013 they also covered some great information about the differences between SQL and NoSQL and how to choose the proper database for your particular use case.<\/p>\n\n\n<aside class=\"\n    cta-banner\n        \"\ndata-block-name=\"cta-banner\">\n    <div class=\"inner\">\n        <div class=\"content\">\n                            <h2 class=\"headline\">Need a better way to interview candidates? Try CoderPad.<\/h2>\n            \n                            <div class=\"cta-buttons\">\n                                    <a href=\"\/demo\/\" class=\"button  js-cta--get-a-demo\"  data-ga-category=\"CTA\" data-ga-label=\"Need a better way to interview candidates? Try CoderPad.|Get a demo\">Get a demo<\/a>\n                                <\/div>\n                    <\/div>\n            <\/div>\n<\/aside>\n\n\n\n<h2 class=\"wp-block-heading\">The difference between relational and non-relational databases<\/h2>\n\n\n\n<p>Developers have traditionally stored data in the form of a series of tables that relate to one another through a combination of primary and foreign keys \u2013 also known as relational databases. The main way we interact with relational databases is through Structured Query Language (SQL).&nbsp;<\/p>\n\n\n\n<p>For example, two relational database tables might look like this:<\/p>\n\n\n\n<p><em>hero_table<\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>name&nbsp;<\/strong><\/td><td><strong>superpower<\/strong><\/td><\/tr><tr><td>Flash<\/td><td>Really fast<\/td><\/tr><tr><td>Storm<\/td><td>Controls weather<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><em>villain_table<\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>name&nbsp;<\/strong><\/td><td><strong>superpower<\/strong><\/td><\/tr><tr><td>Magneto<\/td><td>Great with magnets<\/td><\/tr><tr><td>Bellatrix Lestrange<\/td><td>Powerful witch<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>A SQL statement to merge two tables would look something like this.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">SELECT name, superpower FROM hero_table\nUNION\nSELECT name, superpower FROM villain_table;<\/code><\/span><\/pre>\n\n\n<p>SQL and relational databases work great when you have structured and orderly data. However, as data capturing has increased on such a massive scale \u2013 think of the terabytes of real-time data captured on users cruising the internet every day \u2013 issues with scaling and tracking data that don\u2019t have a consistent structure have become rampant.&nbsp;<\/p>\n\n\n\n<p>Enter Not Only SQL (NoSQL) and non-relational databases. Instead of using tables consisting of rows and columns to store data, a non-relational database generally uses a JSON object stored with similar objects in a collection. All relationships between collections are informal.&nbsp;<\/p>\n\n\n\n<p>So rather than having a bunch of different tables that need to link to one another, you can store all the information you need in the collection of objects. So our superheroes and villains data collection might look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json shcb-wrap-lines\">&#91;\n\t{\n\t\t<span class=\"hljs-attr\">\"name\"<\/span>: <span class=\"hljs-string\">\"Flash\"<\/span>,\n\t\t<span class=\"hljs-attr\">\"superpower\"<\/span>: <span class=\"hljs-string\">\"really fast\"<\/span>,\n\t\t<span class=\"hljs-attr\">\"type\"<\/span>: <span class=\"hljs-string\">\"hero\"<\/span>\n\t},\n\t{\n\t\t<span class=\"hljs-attr\">\"name\"<\/span>: <span class=\"hljs-string\">\"Storm\"<\/span>,\n\t\t<span class=\"hljs-attr\">\"superpower\"<\/span>: <span class=\"hljs-string\">\"Controls weather\"<\/span>,\n\t\t<span class=\"hljs-attr\">\"type\"<\/span>: <span class=\"hljs-string\">\"hero\"<\/span>\n\t},\n\t{\n\t\t<span class=\"hljs-attr\">\"name\"<\/span>: <span class=\"hljs-string\">\"Magneto\"<\/span>,\n\t\t<span class=\"hljs-attr\">\"superpower\"<\/span>: <span class=\"hljs-string\">\"Great with magnets\"<\/span>,\n\t\t<span class=\"hljs-attr\">\"type\"<\/span>: <span class=\"hljs-string\">\"villain\"<\/span>\n\t},\n\t{\n\t\t<span class=\"hljs-attr\">\"name\"<\/span>: <span class=\"hljs-string\">\"Bellatrix Lestrange\"<\/span>,\n\t\t<span class=\"hljs-attr\">\"superpower\"<\/span>: <span class=\"hljs-string\">\"Powerful witch\"<\/span>,\n\t\t<span class=\"hljs-attr\">\"type\"<\/span>: <span class=\"hljs-string\">\"villain\"<\/span>\n\t}\n]\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The actual syntax of the NoSQL queries you make to a NoSQL database will depend on the database provider. For example, if you were using MongoDB, you\u2019d use this query to list all the objects from your \u201csupernatural characters\u201d collection:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">db<\/span><span class=\"hljs-selector-class\">.supernatural_characters<\/span><span class=\"hljs-selector-class\">.find<\/span>()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So when should you use one and not the other?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SQL vs NoSQL? It depends on the data<\/h2>\n\n\n\n<p>As we mentioned earlier, SQL is great if you have lots of structured data that won\u2019t have too many regular schema or column changes.&nbsp;<\/p>\n\n\n\n<p>An excellent example of structured data is a database that tracks baseball statistics. Generally, you\u2019ll always have a player table with a player name, team name, homeruns, at bats etc\u2013as well as a number of calculated averages like batting average and earned run average. You\u2019ll also probably have a team table with the team\u2019s name, team home runs, location, etc.&nbsp;<\/p>\n\n\n\n<p>There is a risk to having data structured like this, however \u2013 if you have a set of data that regularly has empty or null values for specific fields (e.g., a calculated field like batting average for a year when a player was out on injury), then you\u2019re unnecessarily increasing your disk space, storage costs, and data retrieval time because the database has to store and recall those empty fields.&nbsp;<\/p>\n\n\n\n<p>NoSQL objects can contain whatever you want \u2013or don\u2019t want \u2013 them to have. For that reason, NoSQL databases can be a bit more performant than SQL databases when it comes to reading data.&nbsp;<\/p>\n\n\n\n<p>You may want to use NoSQL over SQL if you need to scale your data across many machines horizontally.&nbsp;<\/p>\n\n\n\n<p>Horizontal scaling allows you to spread processing power across multiple machines, increasing the speed of your <a href=\"https:\/\/en.wikipedia.org\/wiki\/Create,_read,_update_and_delete\" target=\"_blank\" rel=\"noopener\">CRUD operations<\/a>. Because of the way relational databases are designed, it\u2019s challenging to get them spread across multiple processing nodes.&nbsp;<\/p>\n\n\n\n<p>On the flip side, NoSQL databases can fall apart if a data access pattern changes since there is no way to enforce schemas and relationships like there is with SQL databases. Without carefully managing what data gets logged and where, then it&#8217;s very easy to make a mess of the data.<\/p>\n\n\n\n<p>For example, you could have a collection that tracks user demographics like their name, location, and age.&nbsp;<\/p>\n\n\n\n<p>After three years the product pivots to track health data for users and stops tracking demographic data, so now the product is collecting height, weight, and birth date in the same user collection.&nbsp;<\/p>\n\n\n\n<p>A month after that the product owner decides they want the birthday collected as a UTC string instead of a normal date string.&nbsp;<\/p>\n\n\n\n<p>Now you have not only a collection with two different schemas (demographics and health info), but you have a field (birthday) that has two different data types. This can cause lots of processing issues and is one of the reasons you may want to have the schema-enforcement that SQL provides.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Power of Object-Relational Mapping (ORM)<\/h2>\n\n\n\n<p>James dove into object-relational mapping (ORM) towards the end of the database discussion.<\/p>\n\n\n\n<p>If you\u2019re unfamiliar with ORM, it\u2019s a way for developers to interact with both relational and non-relational databases that doesn\u2019t involve generating complex SQL statements \u2013 or any SQL at all.<\/p>\n\n\n\n<p>ORMs are an abstraction layer that sits above a database and takes human-readable function calls and translates them into SQL or NoSQL. This is a considerable advantage to developers as it reduces errors from having to write complex queries and the same function definitions can be used to manage multiple different databases.<\/p>\n\n\n\n<p>Here\u2019s what it would look like to insert a row into a relational database with Prisma, an ORM for JavaScript:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-number\">2<\/span>  <span class=\"hljs-keyword\">await<\/span> prisma.user.create({\n<span class=\"hljs-number\">3<\/span>    data: {\n<span class=\"hljs-number\">4<\/span>      name: <span class=\"hljs-string\">'Alice'<\/span>,\n<span class=\"hljs-number\">5<\/span>      email: <span class=\"hljs-string\">'alice@prisma.io'<\/span>,\n<span class=\"hljs-number\">6<\/span>      posts: {\n<span class=\"hljs-number\">7<\/span>        create: { <span class=\"hljs-attr\">title<\/span>: <span class=\"hljs-string\">'Hello World'<\/span> },\n<span class=\"hljs-number\">8<\/span>      },\n<span class=\"hljs-number\">9<\/span>      profile: {\n<span class=\"hljs-number\">10<\/span>        create: { <span class=\"hljs-attr\">bio<\/span>: <span class=\"hljs-string\">'I like turtles'<\/span> },\n<span class=\"hljs-number\">11<\/span>      },\n<span class=\"hljs-number\">12<\/span>    },\n<span class=\"hljs-number\">13<\/span>  })\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can find an ORM for just about every language; Hibernate is a popular one for Java, and Django ORM is often used with Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Serverless computing, horizontal scaling SQL with PlanetScale<\/h2>\n\n\n\n<p>James discusses more or his database knowledge with Corbin along with a brief introduction to serverless computing.&nbsp;<\/p>\n\n\n\n<p>You can find those conversations in the <a href=\"https:\/\/www.twitch.tv\/videos\/1425572449?t=0h0m1s\" target=\"_blank\" rel=\"noopener\">Twitch replay video here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>James Quick of PlanetScale highlights some important differences between SQL and NoSQL databases, and when to use each one.<\/p>\n","protected":false},"author":12,"featured_media":5396,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[27,29],"blog-programming-language":[66,67],"keyword-cluster":[],"class_list":["post-5392","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/5392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=5392"}],"version-history":[{"count":15,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/5392\/revisions"}],"predecessor-version":[{"id":7843,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/5392\/revisions\/7843"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/5396"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=5392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=5392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=5392"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=5392"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=5392"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=5392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}