Skip to content

Commit

Permalink
Server: Optimized the getBy and countBy methods
Browse files Browse the repository at this point in the history
 changing the transaction_inputs and transaction_outputs tables for address_transaction_details table which is indexed
  • Loading branch information
adinael committed Nov 26, 2018
1 parent fc7ef37 commit 8c805a9
Showing 1 changed file with 9 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,15 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi

val orderBy = fieldOrderingSQLInterpreter.toOrderByClause(ordering)

/**
* TODO: The query is very slow while aggregating the spent and received values,
* it might be worth creating an index-like table to get the accumulated
* values directly.
*/
SQL(
s"""
|SELECT t.txid, blockhash, time, size,
| (SELECT COALESCE(SUM(value), 0) FROM transaction_inputs WHERE txid = t.txid AND address = {address}) AS sent,
| (SELECT COALESCE(SUM(value), 0) FROM transaction_outputs WHERE txid = t.txid AND address = {address}) AS received
|FROM transactions t
|WHERE t.txid IN (
| SELECT txid
| FROM transaction_inputs
| WHERE address = {address}
|) OR t.txid IN (
| SELECT txid
| FROM transaction_outputs
| WHERE address = {address}
|)
|$orderBy
|OFFSET {offset}
|LIMIT {limit}
|SELECT t.txid, t.blockhash, t.time, t.size, a.sent, a.received
|FROM transactions t
|INNER JOIN address_transaction_details a USING (txid)
|WHERE a.address = {address}
|$orderBy
|OFFSET {offset}
|LIMIT {limit}
""".stripMargin
).on(
'address -> address.string,
Expand All @@ -128,17 +114,9 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi
def countBy(address: Address)(implicit conn: Connection): Count = {
val result = SQL(
"""
|SELECT COUNT(*)
|FROM transactions
|WHERE txid IN (
| SELECT txid
| FROM transaction_inputs
| WHERE address = {address}
|) OR txid IN (
| SELECT txid
| FROM transaction_outputs
| SELECT COUNT(*)
| FROM address_transaction_details
| WHERE address = {address}
|)
""".stripMargin
).on(
'address -> address.string
Expand Down

0 comments on commit 8c805a9

Please sign in to comment.