…
…
Find the slow queries in a 20,000-row cargo table from their EXPLAIN plans, choose the right index for each, drop the ones that do not help, and verify every decision with the plan again.
Swift Parcel is a small cargo company. The parcel table has reached 20,000 rows and the team says "the system got slow": the customer service screen opens late, the branch dashboards freeze, and the operations manager suggests putting an index on every column. In this project you will find the slow queries from their plans instead of guessing, choose the right index for each, remove the indexes that do not help, and verify every decision with a new plan.
Data: two tables, fictional sample data.
branch: id, name, city (20 branches)parcel: id, tracking_no (unique, like SP0012345), branch_id, customer_id, status (delivered, in_transit, waiting, returned), ship_date, weight_kg, recipient_email (20,000 parcels; some of the addresses were entered in capital letters or with a leading space)Lessons used: indexes and performance (39); LOWER and TRIM in the email step (26). The only new piece is EXPLAIN (COSTS OFF): the plans from lesson 39 with the cost parentheses removed. Cost numbers change with the environment; in this project you will look only at node names (Seq Scan, Index Scan, Bitmap Index Scan), the Filter and Index Cond lines, and the name of the index used.
Project cells are persistent: The indexes you create and remove stay in the database. Cells that create an index first delete the old one with DROP INDEX IF EXISTS; you can rerun a cell as often as you like. If you rerun an earlier cell, run the ones after it in order as well. A cell shows only the result of its last query; do not change the -- Check query lines or the ready-made EXPLAIN lines. The first line of every cell starts with "Why this query:"; complete it with one sentence (it is not checked).
What you'll do
Required topics
parcel.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Swift Parcel's parcel table has reached 20,000 rows and the team says "the system got slow." Before touching anything, see what you have: which indexes exist on the parcel table today, and on which column is each?
List the index names (indexname) and definitions (indexdef), sorted by name. As you read the output, ask two questions: which ones came from constraints automatically, and which ones did the previous developer create by hand? Do you see an index for the customer number (customer_id)?
This step's code cell is for members.Sign up free
The customer service screen fetches all parcels of the customer on the phone; the query runs thousands of times a day: SELECT * FROM parcel WHERE customer_id = 314;. Searching by tracking number, on the other hand, never gets complaints.
The ready-made line in the cell shows the plan of the tracking number query. First run only that and look at the node name. Then add below it the line that asks for the plan of the customer query; because the cell shows the result of the last query, you will now see the customer query's plan. Why do the first lines of the two plans differ? Which index in the inventory explains the difference?
This step's code cell is for members.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The customer query reads all 20,000 rows to keep 25 of them. Create an index for this query and name it parcel_customer_idx. After creating it, make the planner decide with up-to-date statistics and ask for the plan again.
The first line of the cell is ready: it drops the index if it exists, so you can rerun the cell. Did the plan change? In which node does the new plan mention the index name?
This step's code cell is for members.Sign up free
Every branch has a dashboard on its screen, and it runs two queries: "waiting parcels at this branch" (WHERE branch_id = 7 AND status = 'waiting') and "all parcels of this branch" (WHERE branch_id = 7). Look at the plan of the first query: the previous developer's parcel_branch_idx index is used, but the plan also contains a Filter line. The branch has 1,000 parcels, 20 of which are waiting.
Create a single index that serves both queries, and name it parcel_branch_status_idx. You decide the column order. Then update the statistics and run the ready-made EXPLAIN line: did the Filter line disappear?
This step's code cell is for members.Sign up free
The operations manager says the daily delivery report is slow and brings a suggestion: "Let's put an index on the status column." The report runs this query: SELECT * FROM parcel WHERE status = 'delivered';.
First apply the suggestion: create an index on the status column named parcel_status_idx and update the statistics. To see that the index works, the ready-made line asks for the plan of a rare value: in the whole company only 400 parcels have the status waiting. The new index's name should appear in this plan.
This step's code cell is for members.Sign up free
Now return to the manager's real concern: the daily delivery report. Ask for the plan of this query and compare it with the plan in step 5.
After you see the plan, complete the comment line in the cell in your own words: although the index exists, why did the planner choose this route? Of the 20,000 parcels in the table, 18,000 are delivered. What do you tell the manager: will this index speed up the delivery report? (The comment is not checked; in step 8 you will act on this decision.)
This step's code cell is for members.Sign up free
Recipients look up their parcels by email address. Addresses were sometimes entered in capital letters and sometimes with a leading space; so the login screen's query cleans the address while searching, and this query cannot be changed. A teammate created the index in the cell for this query, but the plan is still a scan. The old parcel_email_idx in the inventory is not used either.
Run the cell, compare the Filter line in the plan with the index definition, and fix the index. Keep the name the same (parcel_login_idx); do not touch the query line.
This step's code cell is for members.Sign up free
Every index is rewritten for every row that is inserted or has its status updated; Swift Parcel adds thousands of parcels a day. The queries the system runs regularly are:
tracking_no = ...)customer_id = ...)branch_id = ... AND status = ... and just branch_id = ...)LOWER(TRIM(recipient_email)) = ...)status = 'delivered')Remove the indexes that no query on this list uses, or that do the same job as another index. You decide which ones go; use the inventory (step 1) and the plans so far. Do not touch the indexes that come from constraints. The check query lists the remaining indexes.
This step's code cell is for members.Sign up free
Removing an index is also a decision, and it is verified by measuring. parcel_branch_idx is gone; does the branch dashboard's second query ("all parcels of this branch", branch 7) still run with an index, or did it turn back into a scan?
Update the statistics and ask for the plan of this query. Which index is used? If you had chosen the column order the other way around (status, branch_id) in step 4, would removing parcel_branch_idx still be safe?
This step's code cell is for members.Sign up free