November 202309:36107.4k views

Saving a SaaS on the weekend

About

Someone tweeted that he was thinking about shutting down his SaaS because of slow responses from PlanetScale. Using public tweets alone, I helped him find the problem: 131,000 rows read to return 10. I show three ways to spot that mismatch yourself, and the fix. It's almost always an index.

Transcript

Everything I said

00:00

This weekend, I was sitting around reading Twitter like I do every weekend, like I do every weekday, like I do every week minute. It's fine. It's not a problem, and I came across this tweet. "Had some thoughts about shutting down hunted.space. I got wrecked in under forty-eight hours 'cause of slow responses from PlanetScale." Uh-oh, PlanetScale is us. Slow responses are bad, so this piqued my interest. "SSR and function calls all slowed down to a crawl.

00:22

I exceeded Vercel allowances faster than I could understand what was going on. I'm on a free PlanetScale tier, so..." dot, dot, dot. So indeed, I'm gonna show you how from the outside, using, using public Twitter alone, no DMs, I didn't log into his PlanetScale account or anything, I just helped him debug the thing, and we saved his SaaS application.

00:43

His words, not mine. Very kind. But I, I just-- I'm gonna show you how I walked through thinking about this and the way that you might apply this to your own SaaS application. So, attached to his tweet are two screenshots, one of which is very important, one of which is the dashboard to his PlanetScale account. You can probably see the problem as well as I can. The rows read are at eleven billion.

01:05

On the free tier of PlanetScale, you get a billion rows read for free, which is a lot, and this is what, this is what was weird to me. As a nascent SaaS application on a free tier, how are you blowing through eleven billion row reads? That was the question. So, I hit him back with a tweet that said, "Those row reads seemed high. Have you checked Insights to see if there are queries that could be optimized?" Usually, a slow response from PlanetScale means a slow database query, and I love slow database queries because they can be made

01:36

fast. If a fast query is not fast enough, w-well, now that's, that's trouble. But a slow query, let's make it fast. Insights, so I'm asking him here about Insights. Insights is a PlanetScale feature that shows you all of the queries that are running on your database, um, how often they run, how many rows are being read, how many rows are being returned, and you can click in and get the explain plan.

01:57

Very, very helpful feature. If you don't have PlanetScale Insights, if you're not a PlanetScale customer, I'll show you how you can get some of this on your own. But if you have Insights, this is definitely the first place to start when you're looking at why is everything going so slowly. So, he hits me back with an answer. "On the front page, when it doesn't time out, I'm showing time series data for ten products synced every five minutes.

02:17

That's up to twenty-eight hundred data points per day for each uncached read." And then he says, uh, "A theoretical sixty-six million reads per month if they were all open." Sixty-six million reads a month, that's, that's a lot. You know what's a lot more? Eleven billion. So that, again, is telling me we're reading too much data, right?

02:40

Reading too much data usually means there's a pretty easy answer. So, I hit him back. "How's your rows read, rows returned ratio look in Insights? Go to Insights and look at the rows read and the rows returned." Boom. There is the answer that I was looking for. Rows read, a hundred and thirty-one thousand. Rows returned, ten.

03:01

Ten rows returned. So the database inspected a hundred and thirty thousand rows and found that ten of them were viable for this result set. That's the problem, and if you run that enough, it's gonna add up to eleven billion pretty quickly. The query is on the left. I didn't even need to look at the query, 'cause I know exactly what's going on here. Let me show you in TablePlus what is happening.

03:22

I'm gonna show you how we can read too many rows that we don't care about. Select star from people. So if we do select star from people, there should be about five hundred thousand and nine rows in this table. And if we do show indexes from people, we'll see we don't have any indexes at all, just the primary key. So, the situation that this guy is running into is he's reading too much data and returning too little data.

03:46

That's a huge mismatch. The database is doing a lot of work, and then it's throwing a lot of it away. We can do the same thing by saying select star from people where birthday between, and we'll do nineteen eighty-eight oh one oh one and nineteen eighty-eight, uh, we'll do oh one thirty-one, just the month of January.

04:08

So if we do that, we get thirteen seventy-eight rows back. That is the result set. That is how many rows we actually want. In a perfect world, the database would examine thirteen seventy-eight rows and give us thirteen seventy-eight rows. But what is actually happening here is the database is examining a lot more rows than that. I'm gonna show you three ways that we can figure this out if you don't have PlanetScale Insights.

04:32

If you have Insights, just go to your dashboard and look. But if not, let's look at the first way, Explain. So, Explain is going to show us how many rows the database thinks it's going to have to read, not how many rows it's actually reading, how many it imagines it's going to read. It's a guess. It's a guess, but it's a good guess. It guessed four ninety-three one twenty-one.

04:54

So that is close to our five hundred thousand number. It is not perfectly accurate. It is based on heuristics and statistics. So Explain will get you close. So to satisfy this query, to satisfy this query of thirteen seventy-eight rows, it is reading five hundred thousand rows. Boom. There's our huge mismatch already. Looks at the entire table, gives us back a tiny bit.

05:16

That's a problem. There's another way that we can figure this out a little bit more accurately. We can run Explain Analyze. If we run Explain Analyze, the difference is it actually runs the query. So be careful. Huge warning with Explain Analyze. It actually runs the query. But it does give us back better data, so if we look down here, this is the result of the Explain Analyze.

05:39

It does a table scan on people. It estimates the cost, and then here's the actual, here's the actual time. And look, that is the actual number of rows that it had to read. So we know for sure now, based on an actual execution of the query, that it read five hundred thousand and nine rows, which ties out to the total count, which makes sense Because it's telling us it did a full table scan on people.

06:03

It had to read the entire table. So that's the second way you can figure out how many rows are being read per query. We're trying to minimize the difference between rows read and rows returned. The third way is kind of tricky because it, it relies on a global state, so it only works on your machine where you're running one query at a time, or maybe on a staging server that's super quiet.

06:24

But I'm gonna show it to you anyway just so you know, and it's this command right here, show global status like Innodb rows read. So if we run that, we'll see we get a number back, and we'll do select that, and we'll comment that out. So we get the number back that says how many rows have been read so far. Let's run this query again just as a select.

06:46

So we'll do select star and then run this query again, and if we calculate the before and after, you'll see it is 500 and thou- 500,009 again. So this is another extremely accurate way to keep track of how many rows are being read. Explain is an estimate, explain analyze is perfect, and Innodb rows read is perfect.

07:10

Now, that you and I are on the same page about what I think is going on with this guy's SAS application, let's head back to Twitter. I say, "Yep, there you go. You're likely lacking some indexes, so it's reading massive amounts of data to then just throw it away. I'd check the explain plan." He hits me back with, "Thanks. I guess the obvious one is to add an index for featured at. Let's see what that does." I think that is probably right.

07:32

There may be some more complex compound index that can be created to satisfy two or three things at once. But let's go back to our people table and add an index to birthday and see how those numbers change. We're just gonna say alter table people add index to birthday. Run that. We get the same 1378 back.

07:53

We'll just jump straight to explain analyze, and we'll take a look at that. If we pull down here, we see rows 1378. So instead of reading the entire table, which would be 500,000 rows, it's reading 1378 and returning 1378. That's what we're looking for.

08:14

Let's take a look at the actual explain instead of the analyze. So you can see here it's doing a range scan on the birthday key, and it assumes, it thinks it's gonna have to look at 1378, and boy is it right. I told him, "I'm guessing that this is gonna solve all of your problems. Do report back. I'm calling my shot here. I think this is definitely the answer.

08:35

The mismatch between rows read and rows returned just shouts missing index to me." And he gives me back a graph, the graph that we all wanna see with the response times falling off of a cliff. Now, the database is not working nearly as hard. It's not scanning the entire table, picking up one or two rows that match. It's scanning the index, grabbing all of the matching rows, and returning them.

08:59

That is what we're looking for, and this is the payoff. Thanks to Aaron Francis. Maybe I don't need to shut down my SAS after all. Databases are a marvel of technology. Guess what the solution was? An index. It's almost always an index. If you want to proactively watch your queries for rows read, rows returned, check out PlanetScale, PlanetScale Insights.

09:22

If you need to do it manually, explain, explain analyze, or Innodb rows read. If you wanna learn more about databases, please subscribe to this channel. See ya.

Databases

More videos on databases

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues