# 2.5 The Bouncers and Dominoes

When you look at a slow database, your first instinct is usually to blame the General (the Query Planner) or a missing map (an Index). But sometimes, the slowness isn't coming from the search. It's coming from the **Bureaucracy**.
Even the laziest elephant must obey the rules of the Depot. Two of the strictest forms of bureaucracy are **Constraints** and **Triggers**.
## The Bouncers (Constraints)
Imagine a hulking Database Bouncer standing at the heavy iron doors of your Depot. Before any suitcase (Tuple) is allowed inside, the Bouncer stops the elephant and checks the luggage to ensure it follows the rules.
There are a few types of Bouncers:
- **CHECK Constraints**: The bouncer physically opens the suitcase. If the `age` tag says `-5`, he throws the suitcase in the trash and yells an error at you. This is very fast.
- **NOT NULL Constraints**: The bouncer quickly checks if a required field is missing. Also very fast.
- **Foreign Key Constraints (The Intercom)**: This is where things get slow. If your suitcase says it belongs to `User ID 42`, the bouncer cannot just trust you. He picks up a heavy, static-filled intercom and yells across the entire Railyard to the supervisor of the `Users` Depot: *"Hey! Do you have a User 42?"*
If the `Users` Depot doesn't have an Index (a map) for its IDs, that supervisor has to perform a blistering **Sequential Scan**, opening every single boxcar in their Depot just to answer the intercom! This is why missing indexes on Foreign Keys can bring a seemingly simple `INSERT` to a grinding halt.
## The Domino Effect (Triggers)
If Constraints are Bouncers, then **Triggers** are complex **Rube Goldberg Machines**.
A developer might write a trigger by tying a hidden string to a suitcase. The trigger says: *"Whenever a suitcase is updated in the Orders Depot, automatically go update the 'last_active' timestamp in the Users Depot."*
To the developer sitting at their laptop, this looks like magic. To the Lazy Elephant, it is a highly coordinated, multi-step chore.
The elephant drops the suitcase in the Depot and turns to leave for his hammock. But the hidden string pulls a lever, which drops a bowling ball, which rings an incredibly loud bell. Three other elephants are jolted awake. They grab their clipboards, walk across the Railyard, lock the `Users` Depot door, find the user's suitcase, cross out the old timestamp, write a new one, and then log the entire interaction in the [[Chapter 4/4.1 - Write-Ahead Logging|Pocket Diary (WAL)]].
What started as a simple, lightning-fast assignment has cascaded into a multi-Depot operation. This **Domino Effect** isn't inherently bad—in fact, it's a fantastic way to ensure the Railyard stays perfectly synchronized without the developer having to remember every rule. However, it does hide the true cost of an action. When you use Triggers, the elephant is never *just* doing what you asked; he is faithfully executing a pre-programmed chain reaction. If you forget how many dominoes you set up, you might be surprised by how long it takes them all to fall!
---
[[Chapter 2/2.4 - Vector Search|← 2.4 - Vector Search]] | [[Chapter 2/2.0 - The Mighty Indexes|↑ 2.0 - The Mighty Indexes]] | [[Chapter 2/2.6 - The Cost of Fame|2.6 - The Cost of Fame →]]