Tribler / tribler

Privacy enhanced BitTorrent client with P2P content discovery
https://www.tribler.org
GNU General Public License v3.0
4.74k stars 445 forks source link

[7.13.3] Some database queries can be very slow #7954

Closed kozlovsky closed 3 months ago

kozlovsky commented 3 months ago

I found a few queries to the TorrentState that can be pretty slow. On my (pretty fast) machine with SSD, such a query takes 4 seconds to execute after a cold start. On a machine with HDD, it can be slower in order of magnitude. This can be the reason for some CoreConnectTimeoutError errors.

The reason for the slowness is that to be fast, the query should use a partial index idx_torrentstate__last_check__partial, and for that, the query should contain exactly the following condition for the TorrentState table: has_data = 1. In that case, the SQLite query optimizer can use the index when constructing the query plan.

Several queries to the TorrentState table do not include this condition, so they do not use the index and execute slowly.

After the fix, the same query executes in 0.04 seconds, eliminating the problem.

kozlovsky commented 3 months ago

Fixed in #7955