Browse Source

Server: Optimized the getBy and countBy methods

changing the transaction_inputs and transaction_outputs tables for address_transaction_details table which is indexed
prometheus-integration
Adinael Perez Ruelas 6 years ago
parent
commit
8c805a95d3
  1. 40
      server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala

40
server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala

@ -94,29 +94,15 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi
val orderBy = fieldOrderingSQLInterpreter.toOrderByClause(ordering) 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( SQL(
s""" s"""
|SELECT t.txid, blockhash, time, size, |SELECT t.txid, t.blockhash, t.time, t.size, a.sent, a.received
| (SELECT COALESCE(SUM(value), 0) FROM transaction_inputs WHERE txid = t.txid AND address = {address}) AS sent, |FROM transactions t
| (SELECT COALESCE(SUM(value), 0) FROM transaction_outputs WHERE txid = t.txid AND address = {address}) AS received |INNER JOIN address_transaction_details a USING (txid)
|FROM transactions t |WHERE a.address = {address}
|WHERE t.txid IN ( |$orderBy
| SELECT txid |OFFSET {offset}
| FROM transaction_inputs |LIMIT {limit}
| WHERE address = {address}
|) OR t.txid IN (
| SELECT txid
| FROM transaction_outputs
| WHERE address = {address}
|)
|$orderBy
|OFFSET {offset}
|LIMIT {limit}
""".stripMargin """.stripMargin
).on( ).on(
'address -> address.string, 'address -> address.string,
@ -128,17 +114,9 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi
def countBy(address: Address)(implicit conn: Connection): Count = { def countBy(address: Address)(implicit conn: Connection): Count = {
val result = SQL( val result = SQL(
""" """
|SELECT COUNT(*) | SELECT COUNT(*)
|FROM transactions | FROM address_transaction_details
|WHERE txid IN (
| SELECT txid
| FROM transaction_inputs
| WHERE address = {address}
|) OR txid IN (
| SELECT txid
| FROM transaction_outputs
| WHERE address = {address} | WHERE address = {address}
|)
""".stripMargin """.stripMargin
).on( ).on(
'address -> address.string 'address -> address.string

Loading…
Cancel
Save