Skip to content
Advertisement

Optimize nested inner joins

The following TypeORM generated SQL query takes over 11 sec to complete :

Given the following database indexes :

It feels like some left joins could be indexed, but I am unsure how to do it the proper way. Besides from indexing, is there anything I could do from TypeORM (or other), to really speed up the request?

Here is the EXPLAIN output :

TypeORM code :

Feel free to ask for more information.

Advertisement

Answer

The 2 fold speed up from vacuuming is nice, but I thought it would be much more than that.

Far more than half the original time is going to this one node, so even with a 2 fold speed it still must be slow.

What this is doing is jumping to the end of the part of the index where root_id has the desired value, walking backwards down the order of start_rev until it finds the first row which meets status=ANY ('{PUBLISHED,RELEASED,CANCELED}') condition, then stopping. And on average it has to walk down over 1026 entries with the wrong status before finding one entry with a right status. I don’t know if that is because those statuses are very rare, or because they are biased to occur only early on in the sequence of start_rev.

The ideal index for this case would be:

With that index, it should be able to pull out the max value of start_rev which has the correct status, for each given root_id, pretty much immediately. And if those statuses are rare, then this index should also be pretty small.

Of course if those three listed statuses are not set in stone, but were just selected from a list of checkboxes and will be different each time, then this index won’t be much of a solution for you.

That is my bottom up analysis. Edouard’s top down analysis is also good, and it would be nice to know if you adopted any of those changes and how they worked.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement