Speed up fetching range reports, evolution and sparkline reports #20121
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This index change will speed up all kind of reports fetching but especially range periods where the change will be most noticeable.
When fetching evolution graphs, then the method
getArchiveIds
can be very slow. Especially when looking for many periods.See eg below where it executed 30 queries and it took 7 seconds:
Or this range period requests where it took 217 seconds for 2240 calls (there might be still more tweaks possible to reduce the number of calls which be a separate fix).
I've been testing this on two of our instances with different queries and profiling and we can reproduce this.
I've then looked at the current numeric index which is:
The
ts_archived
is not very useful for an index though because most of the queries wouldn't lower the result set. By changing this part of the index toname(6)
. I was able to make the queries 150-170 times faster on our instances (we ran the same queries 100 times withSQL_NO_CACHE
and compared different queries with different indexes). That's roughly because our archives include 166 different record names. On some smaller instance it improved the query 40+.On our account 1 such query takes typically 200ms vs with this index change it takes 1.2ms.
That means above screenshot goes roughly down from 7 seconds to say 50ms. The 217 seconds goes down to 1.5 seconds.
While not changing the index size in the end as I've looked over the queries containing
ts_archived
and thets_archived
index typically doesn't help much there but even more importantly these queries contain also thename
column meaning the new index should be even more effective there.This query
getArchiveIds()
is also the TOP contributor on our cloud for load.ts_archived
was added initially 12 years ago in f593a3e but a better choice would have been to usename
column but that time there were likely certain MySQL limitations.Why
name(6)
?The done flag we use in those archive queries are typically
done
,done.VisitsSummary
ordone2499999
ordone249999.VisitsSummary
. By including the first 6 characters in the index we should be able to select more precise the best rows. Technically,name(5)
would do an equally good job as out of 1M records there are only 6done.*
records but figured rather give it one more.Other changes
ts_archived is not null
as it is always not null. I assume it was added for old MySQL versions as it otherwise wouldn't use the index unless all parts were covered.We have tested these changes on production on our account and reports are now loading pretty much instant.
Review