Profile a Table First
A profile is a single query that tells you how big a table is, where the nulls are, and what range it covers.
The fastest way to write a wrong report is to assume what a table contains.Profiling is the habit of asking first: how many rows, how many of them are missing the column you are about to filter on, and what range of dates you are actually looking at.
Most of a profile comes from one detail about COUNT. count(*) counts rows, but count(column) counts only the rows where that column is not null. Subtract one from the other and you have a null count without writing a second query.
select count(*) as rows,
count(reorder_level) as non_null,
count(*) - count(reorder_level) as missing,
count(distinct category_id) as categories
from productsRanges and repetition
Two more numbers finish the picture. count(distinct column) tells you whether a column is an identifier (distinct count equals the row count) or a category (a handful of repeated values). min and max on a date column tell you which period the table covers, which is how you catch a load that stopped three weeks ago.
When a distinct count comes back small, follow it with aGROUP BY to see the values themselves. Four order statuses is a short enough list to read in full.
select status,
count(*) as orders
from orders
group by status
order by orders desc, statusSchema · Garden ShopTable · orders6 columns · 24 rows
One row per order. Unshipped orders have a null shipped_date.
Profile the orders table in one query. Returnrows, shipped (orders with a shipped date),missing_shipped_date, customers (distinct customers),statuses (distinct statuses),first_order, and last_order. The result is a single row.
- Columns: rows, shipped, missing_shipped_date, customers, statuses, first_order, last_order.
- Rows: 1. A profile summarises the whole table into a single row.
- 24 orders from 16 customers, 7 of them with no shipped date yet.
Keep the profile query. Run it before a report and again after a load, and the two results together tell you whether anything moved that you did not expect. The next lesson turns that instinct into checks that give a pass-or-fail answer.
Related
Write checks that return nothing when the data is clean.
Pick the right guard so a setup script survives a second run.
Remove tables, views, schemas, and more without breakage.
NOT NULL, DEFAULT, CHECK, UNIQUE, and keys.