Skip to content

Commit

Permalink
Improve MySQL db query index usage (uber#2598)
Browse files Browse the repository at this point in the history
  • Loading branch information
longquanzheng authored Sep 25, 2019
1 parent 6ab9324 commit 609b526
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
11 changes: 10 additions & 1 deletion common/persistence/sql/sqlHistoryV2Manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ func (m *sqlHistoryV2Manager) ReadHistoryBranch(
BranchID: sqldb.MustParseUUID(request.BranchID),
MinNodeID: &minNodeID,
MaxNodeID: &maxNodeID,
MinTxnID: &lastTxnID,
PageSize: &request.PageSize,
ShardID: request.ShardID,
}
Expand Down Expand Up @@ -220,6 +219,16 @@ func (m *sqlHistoryV2Manager) ReadHistoryBranch(
// event batches with larger node ID
// -> batch with lower transaction ID is invalid (happens before)
// -> batch with higher transaction ID is valid
if row.NodeID < lastNodeID {
return nil, &shared.InternalServiceError{
Message: fmt.Sprintf("corrupted data, nodeID cannot decrease"),
}
} else if row.NodeID > lastNodeID {
// update lastNodeID so that our pagination can make progress in the corner case that
// the page are all rows with smaller txnID
// because next page we always have minNodeID = lastNodeID+1
lastNodeID = row.NodeID
}
continue
}

Expand Down
4 changes: 2 additions & 2 deletions common/persistence/sql/storage/mysql/eventsV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
`VALUES (:shard_id, :tree_id, :branch_id, :node_id, :txn_id, :data, :data_encoding) `

getHistoryNodesQry = `SELECT node_id, txn_id, data, data_encoding FROM history_node ` +
`WHERE shard_id = ? AND tree_id = ? AND branch_id = ? AND node_id >= ? and node_id < ? and txn_id < ? ORDER BY shard_id, tree_id, branch_id, node_id, txn_id LIMIT ? `
`WHERE shard_id = ? AND tree_id = ? AND branch_id = ? AND node_id >= ? and node_id < ? ORDER BY shard_id, tree_id, branch_id, node_id, txn_id LIMIT ? `

deleteHistoryNodesQry = `DELETE FROM history_node WHERE shard_id = ? AND tree_id = ? AND branch_id = ? AND node_id >= ? `

Expand Down Expand Up @@ -62,7 +62,7 @@ func (mdb *DB) InsertIntoHistoryNode(row *sqldb.HistoryNodeRow) (sql.Result, err
func (mdb *DB) SelectFromHistoryNode(filter *sqldb.HistoryNodeFilter) ([]sqldb.HistoryNodeRow, error) {
var rows []sqldb.HistoryNodeRow
err := mdb.conn.Select(&rows, getHistoryNodesQry,
filter.ShardID, filter.TreeID, filter.BranchID, *filter.MinNodeID, *filter.MaxNodeID, -*filter.MinTxnID, *filter.PageSize)
filter.ShardID, filter.TreeID, filter.BranchID, *filter.MinNodeID, *filter.MaxNodeID, *filter.PageSize)
// NOTE: since we let txn_id multiple by -1 when inserting, we have to revert it back here
for _, row := range rows {
*row.TxnID *= -1
Expand Down
4 changes: 1 addition & 3 deletions common/persistence/sql/storage/sqldb/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ type (
MinNodeID *int64
// Exclusive
MaxNodeID *int64
// Exclusive
MinTxnID *int64
PageSize *int
PageSize *int
}

// HistoryTreeRow represents a row in history_tree table
Expand Down

0 comments on commit 609b526

Please sign in to comment.