Files
mostlymatter/store/sqlstore
Agniva De Sarker d938d70dcd MM-21211: Optimize Channelstore.UpdateLastViewedAt (#13529)
* MM-21211: Optimize Channelstore.UpdateLastViewedAt

For postgres, we use CTEs to do both queries at one
and return the data.

For mysql, CTEs aren't available until version 8. So we just optimize the
existing query.

- We use IN instead of multiple OR conditions for the channel membership check.
According to mysql https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_in:

If no type conversion is needed for the values in the IN() list,
they are all constants of the same type, and expr can be compared to each of them
as a value of the same type (possibly after type conversion), an optimization takes place.

The values the list are sorted and the search for expr is done using a binary search,
which makes the IN() operation very quick.

- We use the previous column LastViewedAt to replace the value of LastUpdateAt
instead of doing a switch case again.

https://dev.mysql.com/doc/refman/5.6/en/ansi-diff-update.html

If you access a column from the table to be updated in an expression, UPDATE uses the
current value of the column.

Results (ran with getchannel profile):

Postgres:
| Metric | Baseline | Actual | Delta | Delta % |
| --- | --- | --- | --- | --- |
| Hits | 1594 | 1574 | -20 | -1.25%
| Error Rate | 0.00% | 0.00% | 0% | 0% |
| Mean Response Time | 7.08ms | 6.36ms | -0.72ms | -10.20% |
| Median Response Time | 5.00ms | 4.00ms | -1.00ms | -20.00% |
| 95th Percentile | 9.00ms | 8.00ms | -1.00ms | -11.11% |

Mysql:
| Metric | Baseline | Actual | Delta | Delta % |
| --- | --- | --- | --- | --- |
| Hits | 1600 | 1566 | -34 | -2.12%
| Error Rate | 0.00% | 0.00% | 0% | 0% |
| Mean Response Time | 7.39ms | 7.02ms | -0.37ms | -4.94% |
| Median Response Time | 6.00ms | 6.00ms | 0ms | 0% |
| 95th Percentile | 10.00ms | 10.00ms | 0ms | 0% |

* Add comment on using CTEs for mysql8

* incorporate review comments

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
2020-01-09 22:04:30 +05:30
..