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. 30
      server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala

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

@ -94,26 +94,12 @@ 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
|SELECT t.txid, t.blockhash, t.time, t.size, a.sent, a.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}
|)
|INNER JOIN address_transaction_details a USING (txid)
|WHERE a.address = {address}
|$orderBy
|OFFSET {offset}
|LIMIT {limit}
@ -129,16 +115,8 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi
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
| FROM address_transaction_details
| WHERE address = {address}
|)
""".stripMargin
).on(
'address -> address.string

Loading…
Cancel
Save