ββββββββββ
β n_rows β
β int64 β
ββββββββββ€
β 59024 β
ββββββββββ
Lecture 26 - Revision: Scaling, SQL and Containers
Source: The Turing Way Community (2025)
1. Scaling and performance
2. SQL revision
WHERE and HAVING, COUNT, joins3. SQL practice
4. Containers
docker run5. Quiz 05 preparation
| Your task | Tool |
|---|---|
| Number crunching | ProcessPoolExecutor |
| Downloading many URLs | ThreadPoolExecutor |
| sklearn, numeric loops | joblib (n_jobs=-1) |
| Bigger than RAM | Dask |
concurrent.futures ships with Python, so it needs no installnn doubles, the runtime stays the same for O(1), doubles for O(n) and quadruples for O(nΒ²)N cores turn O(n) into O(n/N + overhead), but the class stays the sameAmdahlβs law: the serial share \(s\) limits the speed-up
\(\text{speedup} = \dfrac{1}{s + p/N}\)
90% parallel on 8 cores: 1 / (0.1 + 0.9/8) = 1 / 0.2125 = 4.7x
The maximum speed-up is \(1/s\): 10Γ here, 2Γ if half the program is serial
delayed).compute().persist() keeps a small result in memorylocalhost:8787 shows the tasks livevalue by indicator and decade, on 200,681,600 rows, Apple M5 laptop:| pandas | Dask | DuckDB |
|---|---|---|
| 9.65 s | 5.39 s | 0.53 s |
| Your situation | Reach for |
|---|---|
| Fits in RAM, exploring, plotting | pandas |
| One large file on one machine | DuckDB |
| Bigger than the machine, or a cluster | Dask |
WHERE filters rows before grouping, and HAVING filters groups afterWHERE, because the groups do not exist yetSELECT column is in GROUP BY or inside an aggregateCOUNT(*) counts rows, and COUNT(value) counts rows with a valueCOUNT(*) - COUNT(value) gives the number of missing valuesAVG, SUM, MIN and MAX skip missing values without warningAVG returns NULLvalue = NULL never matches. Test with IS NULL or IS NOT NULLn_rows stays the same each year, because the panel has a row for every countrywith_value falls when countries stop reporting, so missing rises../lecture-19/data/wdi_panel.parquetββββββββββ
β n_rows β
β int64 β
ββββββββββ€
β 59024 β
ββββββββββ
country, iso3, indicator, year, valuegdp_per_capita, life_expectancy, co2_per_capita and internet_users_pctHow many rows and columns come back?
What changes without DESC?
What changes without LIMIT 3?
Answer: Appendix 01
Line 4 changed from Question 1: look at the quotes
What happens when you run it?
Answer: Appendix 02
gdp
| iso3 | gdp_pc |
|---|---|
| BRA | 8435 |
| IND | 1807 |
| JPN | 35363 |
life
| iso3 | life_exp |
|---|---|
| BRA | 74.5 |
| IND | 70.2 |
| USA | 77.0 |
pip freeze lists every package with an == pin, dependencies included== pins one exact version, and >= accepts any newer one| Tool | File you commit | Key commands |
|---|---|---|
venv + pip |
requirements.txt |
pip freeze, pip install -r |
| conda | environment.yml |
conda env export --from-history |
uv |
pyproject.toml, uv.lock |
uv add, uv sync, uv export |
pip freeze inside conda writes @ file:/// paths that break on other machinesuv: Appendix 08Whatever tool you use, submit requirements.txt with == pins, because the Dockerfile reads it
# 1. Base image
FROM ubuntu:24.04
# 2. Build-time settings
ENV DEBIAN_FRONTEND=noninteractive
SHELL ["/bin/bash", "-c"]
# 3. System packages
RUN apt-get update && apt-get install -y [...]
# 4. Quarto
RUN ARCH=$(dpkg --print-architecture) && [...]
# 5. Python environment
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY requirements.txt /project/requirements.txt
RUN pip install --no-cache-dir -r /project/requirements.txt
# 6. Project files
WORKDIR /project
COPY . /project
# 7. Default command
ENV QUARTO_PYTHON=/opt/venv/bin/python
CMD ["bash", "-c", "quarto render report.qmd && [...]"]FROM pins the base imageDEBIAN_FRONTEND=noninteractive stops apt asking questionsdpkg --print-architecture picks the arm64 or amd64 Quarto .deb/opt/venv, because Ubuntu protects its own PythonCOPY requirements.txt comes first, so editing the report keeps pip install cachedRUN runs a command while the image buildsENV sets a variable that later steps and the container keepWORKDIR sets the folder where later commands run, and creates it if neededDockerfilerequirements.txt reruns pip-t names the image--rm deletes the container when it exits-v host:container links a laptop folder to a container folder-v, the report disappears with the container-p host:container maps ports. The project needs only -v.dockerignore keeps .git, caches and outputs out of the imagedata/raw/, and check with git ls-files data/raw==, to a version on PyPIls output should show a fresh reportIOException: No files found-v: the report disappears with the containerData.csv and data.csv as one file, but Linux does not.gitattributes~/), not /mnt/c/README.mdWHERECOUNT does with NULLsvenv, conda and uv to their filespip freeze writes and what == meansDockerfile block doesCOPY requirements.txt comes before COPY .-vFROM ubuntu:24.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y [...]
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY requirements.txt /project/requirements.txt
RUN pip install --no-cache-dir -r \
/project/requirements.txt
WORKDIR /project
COPY . /project
CMD ["bash", "-c", "quarto render report.qmd"]FROM ubuntu:24.04 pin, and why do we avoid latest?WORKDIR /project do, and why does CMD need it?ENV PATH="/opt/venv/bin:$PATH" change?report.qmd, and which if you edit requirements.txt?output/report.html if you run the container without -v?Answers: Appendix 07
.compute()FROM, WHERE, GROUP BY, HAVING, SELECTWHERE filters rows, and HAVING filters groupsCOUNT(*) counts rows, and COUNT(value) skips NULLsLEFT JOIN keeps every row of the left tablerequirements.txt with == pins, whatever tool wrote it-v to keep the reportcd to building a whole Linux container with DockerSource: datasci350-project-starter
βββββββββββ¬βββββββββ
β country β co2_pc β
β varchar β double β
βββββββββββΌβββββββββ€
β Palau β 71.7 β
β Qatar β 41.3 β
β Bahrain β 25.0 β
βββββββββββ΄βββββββββ
country and co2_pcDESC, the sort is ascending, so the three lowest emitters come backLIMIT 3, every country comes back: 217 rows, 14 of them with NULLROUND(value, 1) creates a new column, and AS co2_pc names itBinder Error: Referenced column "co2_per_capita" not found in FROM clause!
Candidate bindings: "country", "year", "indicator"
Binder Errorco2_per_capita'co2_per_capita'Binder Error: WHERE clause cannot contain aggregates!
βββββββββββ¬ββββββββββββββββ
β country β mean_internet β
β varchar β double β
βββββββββββΌββββββββββββββββ€
β Iceland β 98.7 β
β Bahrain β 98.4 β
β Bermuda β 98.2 β
βββββββββββ΄ββββββββββββββββ
WHERE runs before GROUP BY, so the averages do not exist yetHAVINGyear stays in WHERE, because it tests rowsORDER BY and LIMIT 3 show the top threeββββββββββ¬βββββββββββββ
β n_rows β with_value β
β int64 β int64 β
ββββββββββΌβββββββββββββ€
β 217 β 184 β
ββββββββββ΄βββββββββββββ
COUNT(*) counts every row, and COUNT(value) skips NULLswith_value is never bigger, and it equals n_rows only when there are no NULLsAVG(value) would use only the 184 values, without a warningβββββββββββ¬ββββββββββ¬βββββββββββ
β iso3 β gdp_pc β life_exp β
β varchar β double β double β
βββββββββββΌββββββββββΌβββββββββββ€
β BRA β 8435.0 β 74.5 β
β IND β 1807.0 β 70.2 β
β JPN β 35363.0 β NULL β
βββββββββββ΄ββββββββββ΄βββββββββββ
INNER JOIN: 2 rows, because only Brazil and India are in both tablesLEFT JOIN: 3 rows, every row of gdp, with NULL for Japanβs life_expFULL OUTER JOIN: 4 rows, adding the US with NULL for gdp_pcFROM ubuntu:24.04 pins one Ubuntu release
latest meant 24.04 last year and 26.04 today, so it breaks reproducibilityWORKDIR /project makes /project the folder where later commands run, and creates it if needed
CMD runs quarto render report.qmd from there, so Quarto finds the reportENV PATH="/opt/venv/bin:$PATH" puts /opt/venv/bin first, so python and pip mean the venvβs copiesreport.qmd rebuilds from COPY . /project, so pip stays cached
requirements.txt reruns pip and every step after it-v, the report renders inside the container
--rm then deletes the container, and the report with ituv in more detailuv: a fast package manager from Astraluv add installs a package and records ituv sync rebuilds the environment from the lock fileuv run runs code with no activatepyproject.toml: your direct dependenciesuv.lock: exact versions of every packageuv export --format requirements-txt --no-hashes > requirements.txt writes the file the Dockerfile readsWhich countries have been the most connected since 2015?
wdi view existsinternet_users_pctvalue, and call it n_yearsvalue, rounded to one decimal, as mean_internetHints:
year and the two filters on the group go in different clausesCOUNT(*) would pass every country, so use COUNT(value) for the five yearsSolution: Appendix 10
ββββββββββββββ¬ββββββββββ¬ββββββββββββββββ
β country β n_years β mean_internet β
β varchar β int64 β double β
ββββββββββββββΌββββββββββΌββββββββββββββββ€
β Iceland β 9 β 98.7 β
β Bahrain β 9 β 98.4 β
β Luxembourg β 9 β 97.9 β
β Qatar β 9 β 97.8 β
β Denmark β 9 β 97.5 β
ββββββββββββββ΄ββββββββββ΄ββββββββββββββββ
WHERE filters rows, and both HAVING conditions test the groupCOUNT(value) is 9 for these countries: every year from 2015 to 2023COUNT(*) would be 9 for every country, even those with no valuesORDER BY can use mean_internet, the alias from SELECT