I am currently trying to optimise some DB queries that get run a lot, the queries are run by using a SELECT query against a view, this view does a lot of joins. I thought I might be able to speed things up by caching the results of the view into a table and selecting from the table instead of the view.
Let's say I have 2 tables
People:
PersonId | Name |
---|---|
1 | Anne |
2 | Brian |
3 | Charlie |
4 | Doug |
CustomerPeople:
CustomerId | PersonId |
---|---|
1 | 1 |
1 | 2 |
1 | 3 |
1 | 4 |
2 | 1 |
2 | 2 |
and I have a view that joins the two tables to give a list of people, by name, belonging to the customer:
CustomerId | PersonName |
---|---|
1 | Anne |
1 | Brian |
1 | Charlie |
1 | Doug |
2 | Anne |
2 | Brian |
When I query the view, I look at the Duration/Fetch
and it is 0.10 sec/4.00 sec
I decide to cache the view data into a table and create a new table:
CustomerNamedPeople
CustomerId | PersonName |
---|---|
1 | Anne |
1 | Brian |
1 | Charlie |
1 | Doug |
2 | Anne |
2 | Brian |
Which contains the exact same data, however now when I query the table, I look at the Duration/Fetch
and it is 0.05 sec/6.00 sec
My understanding is the Duration
is the time it takes MySQL engine to run the query, and Fetch
is the time it takes the data to be returned to the client (over the network). Unsurprisingly the Duration
was faster, and took only 50% of the time, which makes sense, there is no longer a join
occurring, however the Fetch
took 150% of the time, and is slower.
My question here is: Does MySQL do some sort of response stream compression, since it knows that Anne and Brian are repeated, it can send them only once and have the client "decompress" the data?
The reason I ask is because I am doing something similar but with 1,000,000 rows returned, the data in the two responses is identical, but the view Fetch
takes 20 seconds, and the table Fetch
is 60 seconds, most of the PersonName
s are repeated more than once, so I am wondering if perhaps there is some sort of compression occurring in the response, should I not expect MySQL to take the same time to Fetch
two sets of identical data?