diff --git a/server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala b/server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala index f7e02d7..081d8c0 100644 --- a/server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala +++ b/server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala @@ -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, @@ -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