Motivation
There's lots of queries happening when updating the entity usage table (around 30 per second) in the addEntityUsage job. These queries are big and thus hold database lock for too long causing lots of issues, including long lock time of the database, database errors due to their size. And since this job gets triggered ~30 times every second, any improvement on its performance greatly effects database and the jobqueue.
Problem
The job instead of diffing the new and old entity usage of a page, removes all entity usages of the given page and puts the new ones in the database.
Suggested Solution / Technical Details
Diff against the current entity usage values and just put the ones that are needed and remove the removed ones. Most of entity usage in a page is unchanged and there's no need to remove and remake all of them all the time.