PDF Version Available
This document is also available in PDF format: wk11sqlscaling.pdf
The PDF version includes bookmarks for easy navigation and is optimized for printing.
Accessibility Notice
This document is also available in HTML format at:
Link: https://aholdengouveia.name/AdvData/labs/wk11sqlscaling.html
The HTML version provides enhanced accessibility features including keyboard navigation, screen reader support, responsive design, dark mode support, and high contrast options.
Request
Request: DATA-4630
Requested by: Head of the data team
Data: Your database, and your data collection plan from the Big Data lab
Background
If your data collection plan goes ahead, your database is going to grow. Your manager wants to know how it would scale, how the data could be split across servers if needed, and what could go wrong for the business along the way. You'll warm up with a real business case study, then plan for your own company's growth.
Learning Outcomes
By the end of this lab, you will be able to:
- Write SQL queries to solve a real-world business case study
- Evaluate how a data set would scale, including vertical and horizontal scaling
- Choose a shard key for a data set, using queries on the data to justify it
- Explain how ACID transactions protect a business, and what happens when they fail
Lab Instructions
Use your own data set and company for Parts 2 to 4. Books are my example data only.
Screenshots
Several parts of this lab ask for a screenshot. Each screenshot should have your name, term, and year. One of the easiest ways to do that is make a text document with your name and term/year on your computer and save it to use all term. Any screenshots that don't include this information won't be counted.
Part 1: Case Study
Solve all 10 questions in Case Study 1, Danny's Diner, from the 8 Week SQL Challenge (linked under Resources). Each answer should be a single SQL statement. You can use the editor on the case study page, or load the data into SandboxSQL or another tool.
- Take a screenshot of each solution and its results.
- With each screenshot, explain how you solved it and what resources you used. If you needed a hint, say where you found it.
Optional, not required for this class: if you'd like more practice, try the Pizza Metrics questions in Case Study 2, Pizza Runner.
Part 2: How Would It Scale?
- Using the estimates in your data collection plan from the Big Data lab, roughly how many rows would your database have in 1 year, and in 5 years? Show your working.
- The topic page explains vertical scaling, adding more power to one server, and horizontal scaling, adding more servers. Which would your company need first, and why? Think about cost, and about the size of your company.
Part 3: Choose a Shard Key
If your company ever splits its data across servers, it needs a shard key: the column that decides which server each row goes to. The topic page lists 4 things to check: cardinality, frequency, whether the values keep increasing, and query patterns.
- Pick 3 columns in your main table that could be shard keys.
- For each one, run these queries on your own data, replacing the table and column names, and take a screenshot of the results. The first shows the cardinality, how many different values there are. The second shows the frequency, which values appear most often.
SELECT COUNT(DISTINCT genre) FROM books; SELECT genre, COUNT(*) FROM books GROUP BY genre ORDER BY COUNT(*) DESC LIMIT 5; - For each column, explain whether its values keep increasing, like a date or an ID that goes up by one, and whether it matches the queries your company runs most, such as your documented queries from the SQL Review labs.
- Pick the best shard key, and explain why using your results. What data skew, where one server ends up with much more data than the others, could still happen?
For example, in my book data, genre has only 10 different values, and 50 of my 99 books are Classics. If genre were the shard key, the server holding Classics would get half of all the data, so it would make a poor shard key.
Part 4: ACID and Your Business
Pick one real transaction your company would make, such as a sale that records the payment and updates the stock.
- For each of the 4 ACID properties, atomicity, consistency, isolation, and durability, explain what it means for this transaction, and what would go wrong for the business if it failed.
- Which property matters most to your company, and why?
- Would splitting your data across servers make that property harder to keep? Why or why not?
Acceptance Criteria
This request is done when:
- All 10 Danny's Diner questions are solved with single SQL statements, each with a screenshot and an explanation
- Your growth estimates for 1 and 5 years show your working, and your choice of vertical or horizontal scaling is explained
- 3 possible shard keys are checked with queries on your own data, with screenshots
- Your shard key choice is justified using your results, including any data skew that could still happen
- All 4 ACID properties are explained for a real transaction at your company
- Every screenshot follows the screenshot rules
Deliverables
One file that includes Parts 1 to 4.
Audience: the head of the data team.
Resources
References, a video, a PowerPoint and some notes are available at my website Link: https://www.aholdengouveia.name/AdvData/SQLscaling.html
Case Study 1, Danny's Diner Link: https://8weeksqlchallenge.com/case-study-1/
Optional: Case Study 2, Pizza Runner Link: https://8weeksqlchallenge.com/case-study-2/
Suggested tool: SandboxSQL, free, runs in your browser, and saves your database as a file with its Export button Link: https://www.sandboxsql.com/