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:

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.

Example screenshot: a web browser showing the Google home page, with a small text editor window in front of it that says YOUR NAME HERE and TERM YEAR
This is an image of what each of your screenshots should look like

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.

  1. Take a screenshot of each solution and its results.
  2. 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?

  1. 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.
  2. 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.

  1. Pick 3 columns in your main table that could be shard keys.
  2. 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;
  3. 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.
  4. 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.

  1. 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.
  2. Which property matters most to your company, and why?
  3. Would splitting your data across servers make that property harder to keep? Why or why not?

Acceptance Criteria

This request is done when:

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/