Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ object DBInitializer extends StrictLogging {
logger.info("Create Indexes")
run(for {
_ <- BlockHeaderSchema.createBlockHeadersIndexes()
_ <- TransactionSchema.createMainChainIndex()
_ <- InputSchema.createMainChainIndex()
_ <- OutputSchema.createMainChainIndex()
_ <- TransactionPerAddressSchema.createIndexes()
_ <- OutputSchema.createNonSpentIndex()
_ <- InputSchema.createOutputRefAddressNullIndex()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import org.alephium.util.discard
object Migrations extends StrictLogging {

// scalastyle:off magic.number
val latestVersion: MigrationVersion = MigrationVersion(11)
val latestVersion: MigrationVersion = MigrationVersion(12)

def migration1(implicit ec: ExecutionContext): DBActionAll[Unit] = {
// We retrigger the download of fungible and non-fungible tokens' metadata that have sub-category
Expand Down Expand Up @@ -222,6 +222,41 @@ object Migrations extends StrictLogging {
""".map(_ => ())
}

private val unusedIndexes: ArraySeq[String] =
ArraySeq(
"utransactions_last_seen_idx",
"token_info_category_idx",
"contracts_std_interface_id_guessed_idx",
"contracts_category_idx",
"transactions_history_interval_type_idx",
"token_tx_per_address_token_idx",
"token_outputs_token_idx",
"token_outputs_spent_timestamp_idx",
"token_tx_per_addresses_timestamp_idx",
"transaction_per_token_hash_idx",
"token_tx_per_addresses_block_timestamp_tx_order_idx",
"token_tx_per_address_hash_idx",
"token_tx_per_address_groupless_idx",
"inputs_main_chain_idx",
"block_headers_main_chain_idx",
"outputs_main_chain_idx",
"transaction_per_addresses_main_chain_idx",
"transactions_main_chain_idx",
"txs_chain_to_idx",
"txs_chain_from_idx",
"txs_per_address_tx_hash_idx"
)

private def dropUnusedIndex(indexName: String)(implicit
ec: ExecutionContext
): DBActionAll[Unit] =
sqlu"DROP INDEX IF EXISTS public.#$indexName".map(_ => ())

def migration12(implicit ec: ExecutionContext): DBActionAll[Unit] =
DBIOAction
.sequence(unusedIndexes.map(indexName => dropUnusedIndex(indexName)))
.map(_ => ())

private def migrations(implicit
explorerConfig: ExplorerConfig,
ec: ExecutionContext
Expand All @@ -236,7 +271,8 @@ object Migrations extends StrictLogging {
migration8,
migration9,
migration10,
migration11
migration11,
migration12
)

def backgroundCoinbaseMigration()(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ object BlockQueries extends StrictLogging {

def explainMainChainQuery()(implicit ec: ExecutionContext): DBActionR[ExplainResult] =
mainChainQuery.result.explainAnalyze() map { explain =>
val fullIndexUsed = explain.mkString contains "block_headers_full_index"
ExplainResult(
queryName = "mainChainQuery",
queryInput = "Unit",
explain = explain,
messages = Iterable.empty,
passed = explain.mkString contains "block_headers_main_chain_idx"
messages = Iterable(s"Used block_headers_full_index = $fullIndexUsed"),
passed = fullIndexUsed
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,22 +399,19 @@ object OutputQueries {
key match {
case Some(key) =>
getTxnHashBuilder(key).explainAnalyze() map { explain =>
val explainString = explain.mkString
val outputs_pk_used = explainString contains "outputs_pk"
val outputs_main_chain_idx_used = explainString contains "outputs_main_chain_idx"
val passed = outputs_pk_used && outputs_main_chain_idx_used
val explainString = explain.mkString
val outputs_pk_used = explainString contains "outputs_pk"
val message =
ArraySeq(
s"Used outputs_main_chain_idx = $outputs_main_chain_idx_used",
s"Used outputs_pk = $outputs_pk_used"
s"Used outputs_pk = $outputs_pk_used"
)

ExplainResult(
queryName = queryName,
queryInput = key.toString(),
explain = explain,
messages = message,
passed = passed
passed = outputs_pk_used
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ object BlockHeaderSchema extends SchemaMainChain[BlockHeader]("block_headers") {
/** Joins all indexes created via raw SQL
*/
def createBlockHeadersIndexes(): DBIO[Unit] =
DBIO.seq(fullIndex(), createMainChainIndex())
DBIO.seq(fullIndex())

val table: TableQuery[BlockHeaders] = TableQuery[BlockHeaders]
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ object ContractSchema extends SchemaMainChain[ContractEntity]("contracts") {

def pk: PrimaryKey = primaryKey("contracts_pk", (contract, creationBlockHash))

def contractIdx: Index = index("contracts_contract_idx", contract)
def parentIdx: Index = index("contracts_parent_idx", parent)
def stdInterfaceIdGuessedIdx: Index =
index("contracts_std_interface_id_guessed_idx", stdInterfaceIdGuessed)
def categoryIdx: Index = index("contracts_category_idx", category)
def contractIdx: Index = index("contracts_contract_idx", contract)
def parentIdx: Index = index("contracts_parent_idx", parent)
def interfaceIdIdx: Index = index("contracts_interface_id_idx", interfaceId)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package org.alephium.explorer.persistence.schema

import slick.jdbc.PostgresProfile.api._
import slick.lifted.{Index, ProvenShape}
import slick.lifted.ProvenShape

import org.alephium.explorer.persistence.model.MempoolTransactionEntity
import org.alephium.explorer.persistence.schema.CustomJdbcTypes._
Expand All @@ -23,8 +23,6 @@ object MempoolTransactionSchema extends Schema[MempoolTransactionEntity]("utrans
column[U256]("gas_price", O.SqlType("DECIMAL(80,0)")) // U256.MaxValue has 78 digits
def lastSeen: Rep[TimeStamp] = column[TimeStamp]("last_seen")

def lastSeenIdx: Index = index("utransactions_last_seen_idx", lastSeen)

def * : ProvenShape[MempoolTransactionEntity] =
(hash, chainFrom, chainTo, gasAmount, gasPrice, lastSeen)
.<>((MempoolTransactionEntity.apply _).tupled, MempoolTransactionEntity.unapply)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ object TokenInfoSchema extends SchemaMainChain[TokenInfoEntity]("token_info") {
(token, lastUsed, category, interfaceId)
.<>((TokenInfoEntity.apply _).tupled, TokenInfoEntity.unapply)

def categoryIdx: Index = index("token_info_category_idx", category)
def interfaceIdIdx: Index = index("token_info_interface_id_idx", interfaceId)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ object TokenOutputSchema extends SchemaMainChain[TokenOutputEntity]("token_outpu

def pk: PrimaryKey = primaryKey("token_outputs_pk", (key, token, blockHash))

def keyIdx: Index = index("token_outputs_key_idx", key)
def blockHashIdx: Index = index("token_outputs_block_hash_idx", blockHash)
def txHashIdx: Index = index("token_outputs_tx_hash_idx", txHash)
def addressIdx: Index = index("token_outputs_address_idx", address)
def timestampIdx: Index = index("token_outputs_timestamp_idx", timestamp)
def tokenIdx: Index = index("token_outputs_token_idx", token)
def spentTimestampIdx: Index = index("token_outputs_spent_timestamp_idx", spentTimestamp)
def keyIdx: Index = index("token_outputs_key_idx", key)
def blockHashIdx: Index = index("token_outputs_block_hash_idx", blockHash)
def txHashIdx: Index = index("token_outputs_tx_hash_idx", txHash)
def addressIdx: Index = index("token_outputs_address_idx", address)
def timestampIdx: Index = index("token_outputs_timestamp_idx", timestamp)

def * : ProvenShape[TokenOutputEntity] =
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ object TokenPerAddressSchema

def pk: PrimaryKey = primaryKey("token_tx_per_address_pk", (txHash, blockHash, address, token))

def hashIdx: Index = index("token_tx_per_address_hash_idx", txHash)
def blockHashIdx: Index = index("token_tx_per_address_block_hash_idx", blockHash)
def addressIdx: Index = index("token_tx_per_address_address_idx", address)
def tokenIdx: Index = index("token_tx_per_address_token_idx", token)
def tokenAddressIdx: Index = index("token_tx_per_address_token_address_idx", (token, address))

def * : ProvenShape[TokenTxPerAddressEntity] =
Expand All @@ -53,15 +51,11 @@ object TokenPerAddressSchema
}

def createIndexes(): DBIO[Unit] =
DBIO.seq(
CommonIndex.blockTimestampTxOrderIndex(this),
CommonIndex.timestampIndex(this)
)
DBIO.seq()

def createConcurrentIndexes()(implicit ec: ExecutionContext): DBActionW[Unit] =
for {
_ <- createTokenGrouplessAddressIndex()
_ <- createGrouplessIndex()
} yield ()

def createTokenGrouplessAddressIndex(): DBActionW[Int] =
Expand All @@ -71,14 +65,5 @@ object TokenPerAddressSchema
WHERE groupless_address IS NOT NULL;
"""

def createGrouplessIndex(): DBActionW[Int] =
sqlu"""
CREATE INDEX CONCURRENTLY IF NOT EXISTS token_tx_per_address_groupless_idx
ON token_tx_per_addresses (
token, address, groupless_address, block_timestamp, tx_order, tx_hash, block_hash
)
WHERE groupless_address is not null AND main_chain = true;
"""

val table: TableQuery[TokenPerAddresses] = TableQuery[TokenPerAddresses]
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ object TransactionHistorySchema extends Schema[TransactionHistoryEntity]("transa

def pk: PrimaryKey =
primaryKey("transactions_history_pk", (intervalType, timestamp, chainFrom, chainTo))
def intervalTypeIdx: Index = index("transactions_history_interval_type_idx", intervalType)
def timestampIdx: Index = index("transactions_history_timestamp_idx", timestamp)
def timestampIdx: Index = index("transactions_history_timestamp_idx", timestamp)

def * : ProvenShape[TransactionHistoryEntity] =
(timestamp, chainFrom, chainTo, count, intervalType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ object TransactionPerAddressSchema

def pk: PrimaryKey = primaryKey("txs_per_address_pk", (txHash, blockHash, address))

def hashIdx: Index = index("txs_per_address_tx_hash_idx", txHash)
def blockHashIdx: Index = index("txs_per_address_block_hash_idx", blockHash)
def addressIdx: Index = index("txs_per_address_address_idx", address)
def addressTimestampIdx: Index =
Expand All @@ -54,7 +53,6 @@ object TransactionPerAddressSchema

def createIndexes(): DBIO[Unit] =
DBIO.seq(
createMainChainIndex(),
CommonIndex.blockTimestampTxOrderIndex(this),
CommonIndex.timestampIndex(this)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ object TransactionPerTokenSchema

def pk: PrimaryKey = primaryKey("transaction_per_token_pk", (hash, blockHash, token))

def hashIdx: Index = index("transaction_per_token_hash_idx", hash)
def blockHashIdx: Index = index("transaction_per_token_block_hash_idx", blockHash)
def tokenIdx: Index = index("transaction_per_token_token_idx", token)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ object TransactionSchema extends SchemaMainChain[TransactionEntity]("transaction

def timestampIdx: Index = index("txs_timestamp_idx", timestamp)
def blockHashIdx: Index = index("txs_block_hash_idx", blockHash)
def chainFromIdx: Index = index("txs_chain_from_idx", chainFrom)
def chainToIdx: Index = index("txs_chain_to_idx", chainTo)

def * : ProvenShape[TransactionEntity] =
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ object IndexChecker {
b <- BlockQueries.explainListMainChainHeadersWithTxnNumber(
Pagination.Reversible.unsafe(10000, 20)
) // far page
c <- BlockQueries.explainMainChainQuery()
oldestOutputEntity <- OutputQueries.getMainChainOutputs(true).headOrEmpty
latestOutputEntity <- OutputQueries.getMainChainOutputs(false).headOrEmpty
d <- OutputQueries.explainGetTxnHash(oldestOutputEntity.map(_.key).headOption)
Expand All @@ -42,6 +41,6 @@ object IndexChecker {
latestInputEntity <- InputQueries.getMainChainInputs(false).headOrEmpty
f <- InputQueries.explainInputsFromTxs(oldestInputEntity.map(_.hashes()))
g <- InputQueries.explainInputsFromTxs(latestInputEntity.map(_.hashes()))
} yield ArraySeq(a, b, c, d, e, f, g).sortBy(_.passed)
} yield ArraySeq(a, b, d, e, f, g).sortBy(_.passed)

}
Loading