From 9a427c26ac983feb40b605a8e35a092448c99d7e Mon Sep 17 00:00:00 2001 From: David Reiss Date: Tue, 18 Oct 2022 14:31:35 -0700 Subject: [PATCH] Rewrite mysql PaginateBranchesFromHistoryTree query (#3509) --- common/persistence/sql/sqlplugin/mysql/events.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/common/persistence/sql/sqlplugin/mysql/events.go b/common/persistence/sql/sqlplugin/mysql/events.go index 250028fe36a..304b6ed947e 100644 --- a/common/persistence/sql/sqlplugin/mysql/events.go +++ b/common/persistence/sql/sqlplugin/mysql/events.go @@ -62,11 +62,13 @@ const ( getHistoryTreeQuery = `SELECT branch_id, data, data_encoding FROM history_tree WHERE shard_id = ? AND tree_id = ? ` + // conceptually this query is WHERE (shard_id, tree_id, branch_id) > (?, ?, ?) + // but mysql doesn't execute it efficiently unless it's spelled out like this paginateBranchesQuery = `SELECT shard_id, tree_id, branch_id, data, data_encoding - FROM history_tree - WHERE (shard_id, tree_id, branch_id) > (?, ?, ?) - ORDER BY shard_id, tree_id, branch_id - LIMIT ?` + FROM history_tree + WHERE (shard_id = ? AND ((tree_id = ? AND branch_id > ?) OR tree_id > ?)) OR shard_id > ? + ORDER BY shard_id, tree_id, branch_id + LIMIT ?` deleteHistoryTreeQuery = `DELETE FROM history_tree WHERE shard_id = ? AND tree_id = ? AND branch_id = ? ` ) @@ -210,6 +212,8 @@ func (mdb *db) PaginateBranchesFromHistoryTree( page.ShardID, page.TreeID, page.BranchID, + page.TreeID, + page.ShardID, page.Limit, ) return rows, err