+21.000 row reads in query, how can I improve performance?
My planetscale free tier has been absolutely demolished, and the most expensive query has a rows read / returned ratio of 14,196.
according to their docs, this ratio is: The result of dividing total rows read by rows returned in a query. A high number can indicate that your database is reading unnecessary rows, and they query may be improved by adding an index.
the query that is causing this is the following:
61 Replies
The most important parts of my schema look like this:
I was pretty sure that adding indexes on foreign keys and created_at would be enough but apparently it is not.
First thing, you obviously have mode: planetscale right?
Uff
You are ordering by createdAt in the post table and limiting 16
But the order is desc
I guarantee that's your problem right there
Your index in createdAt is doing absolutly nothing
You have to create it descending
I am using
drizzle-orm/planetscale-serverless
, which I think doesn't require specifying the modeUnfortunatly you'll have to create it manually in the console because drizzle doesn't support it in the kit yet
Which is not bad
ohhh alright
I did not know indexes could be descending
thanks!
Do this: go to the console and run
show index from posts
Do I have to drop then add the index or can I do something like ALTER
Lol, yeah, your indexes are ascending
You don't have to drop them
run
alter table posts add index (created_at desc);
Then run show index from posts again and let me seecollation went from A to D so I guess that's it
Hmmm, if you don't ever run asc then that's it
Do you have an index in dabates.name?
I think it was added automatically when I specified .unique() in drizzle
because it's not present here
Can you run that query again and see the difference?
let me see if I can
I am getting these metrics from users
Wow!
Ikr
Check your indexes on degates
debates
there are ~150 debates so I never thought about adding a key to the name
but it was added automatically so I won't complain
There's a limit on indexes on text, I don't remember, but it shouldn't be relevant in this case
Let's do some investigating
I mean finding the debate shouldn't be hard work for the db cause there are only 153 of them
but there are a ton of posts
run this on the console:
explain select * from posts order by created_at desc limit 16
35.985 to be precise
Show me what you get
should I try adding a WHERE debate_id?
Nah
What columns do you need from posts?
just
content
and created_at
, then the ids to merge the author's dataI think that index just solved your biggest problem
oh man
If you want, drop it, run the explain again and compare
you won't beleive this
lmao
that is the time in ms from all queries
after modifying the index to be descending it dipped
there's something weird though
Lol, you were reading ALL of the columns
I mean rows
this is the time in ms specifically in the query I am having problems with
I thought it was fixed but apparently something else produced the dip?
I think you just need to wait
Can you show just the last 10 min?
Hmmm
that's just the latency but row reads have the same shape
run this
explain analyze select * from posts order by created_at desc limit 16
-> Limit: 16 row(s) (cost=0.0617 rows=16) (actual time=0.0289..0.0876 rows=16 loops=1) -> Index scan on posts using created_at_idx (cost=0.0617 rows=16) (actual time=0.0281..0.0856 rows=16 loops=1)
I think that looks good?Can't get any better
there's an option in planetscale to run an explain with some parameters
I'm gonna run the full query with all of them
and see how it goes
Do explain first
No analyze
on the full query?
Sure
this explain output is bigger than my db lol
Ohhhh, you're also getting the league and the team
Also the team of the author
But that explain is pretty good tbh
Ikr??? There's zero reason for 30.000 rows to be read
I guess more investigation is needed
Is it still like that?
yup
it is rising steadily
I guess more rows need to be read as more posts are written
its in 33.000 now 💀
There's no way 16 rows and from posts causes that many reads
Because the rest are one relations
I would need to investigate it more
Man I'm sorry for taking so much time from you
Maybe adding a covering index somewhere idk
Oh
guess I have something new to learn for tomorrow
thanks for all the help
I think this goes a little beyond drizzle help. dm me if you'd like some private consultation
hey, @titongo
did the index worked?
can you show the rest of the schema? I'll be able to check