T-SQL Tuesday #131 : My favorite SQL Server analogy

T-SQL Tuesday #131: Data Analogies, or: Explain Databases Like I’m Five!For this month's T-SQL Tuesday, Rob Volk asks us to share our favorite analogies that help explain database concepts to people who aren’t database experts. I recently wrote about something similar, where my daughter asked what I do for Wayfair, and I compared making database servers respond quickly to how impatient she can be for Google image searches:

But I have another analogy that I like even more. It involves cursors which, while sometimes necessary, are a terribly inefficient way to process data. SQL Server is optimized to work in sets, not step through one row at a time. Explaining that to technical people can be challenging, but when it's a non-technical person, an analogy can be a better first approach.

The analogy: Imagine going to the grocery store, buying eggs, bringing them home, then going back to the grocery store, buying milk, bringing it home, then going back to the grocery store, buying bread, bringing it home…

For a 5-year old: "Go get me all your socks from upstairs, one sock at a time!"

My adult friends immediately see the pain involved with wrapping an entire trip around each item on their shopping list, as all those trips have a significant amount of overhead (mostly gas and time), much like a cursor does. Even though they can demonstrate that the end result is the same: they got everything on their list, and they spent the same total amount of money at the store.

They can also agree with my kids that a trip up and down the stairs for each sock is at least as painful. Maybe more so if, like me, a trip to the grocery store during the pandemic is actually kind of peaceful.

A girl I dated early on had a mother who was a coupon clipper. She'd cut the 10 cents off a can of peas coupon from the Food City flyer, then 20 cents off corn from IGA, then 15 cents off carrots from A & P. She'd then drive to Food City and buy two cans of peas, drive across town to the IGA and buy two cans of corn, then finally drive to A & P on her way home to buy two cans of carrots. She'd spend more time finding the perfect parking spot at each store than she'd spend actually shopping, never mind all the time, gas, and wear and tear on her car driving in between. I always thought she was crazy if she thought that was in any way worthwhile; and I often likened those legs in between as somewhat analogous to a cursor (multiple trips when one would do). But in 2020 I have to wonder: maybe she was onto something?

Aaron Bertrand

I am a passionate technologist with industry experience dating back to Classic ASP and SQL Server 6.5. I am a long-time Microsoft MVP, write at SQLPerformance and MSSQLTips, and have had the honor of speaking at more conferences than I can remember. In non-tech life, I am a father of two, a huge hockey and football fan, and my pronouns are he/him. If I've helped you out, consider thanking me with a coffee. :-)