Improve querying for active pools
By using new field in the pool history entry table, we can improve the query time for finding active cauldron pools. This change also fixes a datatype issue in the table where TEXT was used instead of INT.
This commit is contained in:
parent
73e29b1bbe
commit
058f751a21
2 changed files with 42 additions and 27 deletions
|
|
@ -35,7 +35,7 @@ pub fn create_table(conn: &Connection) {
|
|||
utxo TEXT PRIMARY KEY REFERENCES utxo_funding(new_utxo_hash) ON DELETE CASCADE,
|
||||
pool TEXT REFERENCES pool(creation_utxo) ON DELETE CASCADE,
|
||||
txid TEXT REFERENCES tx(txid) ON DELETE CASCADE,
|
||||
tx_pos TEXT NOT NULL,
|
||||
tx_pos INT NOT NULL,
|
||||
mtp_timestamp BIGINT,
|
||||
first_seen_timestamp BIGINT,
|
||||
sequence BIGINT NOT NULL,
|
||||
|
|
@ -45,6 +45,18 @@ pub fn create_table(conn: &Connection) {
|
|||
[],
|
||||
)
|
||||
.expect("failed to create table pool_history_entry");
|
||||
|
||||
// these two indexes are for speeding up looking for pools owned by user (active rpc with pkh filter)
|
||||
conn.execute(
|
||||
"CREATE INDEX idx_pool_history_entry_pool_sequence ON pool_history_entry(pool, sequence)",
|
||||
params![],
|
||||
)
|
||||
.unwrap();
|
||||
conn.execute(
|
||||
"CREATE INDEX idx_pool_owner_pkh ON pool(owner_pkh)",
|
||||
params![],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn get_pool_by_utxo(conn: &Connection, utxo_hash: &OutPointHash) -> Result<Option<OutPointHash>> {
|
||||
|
|
|
|||
|
|
@ -169,39 +169,42 @@ fn db_list_active_pools(
|
|||
pkh: Option<&str>,
|
||||
conn: &Connection,
|
||||
) -> Result<Vec<ActivePool>> {
|
||||
let mut query: String = "
|
||||
SELECT
|
||||
p.owner_pkh,
|
||||
uf.sats,
|
||||
uf.token_amount,
|
||||
uf.new_utxo_txid,
|
||||
uf.new_utxo_n,
|
||||
p.token_id
|
||||
let filters = match (token_id.is_some(), pkh.is_some()) {
|
||||
(true, true) => "AND p.token_id = ?1 AND p.owner_pkh = ?2",
|
||||
(true, false) => "AND p.token_id = ?1",
|
||||
(false, true) => "AND p.owner_pkh = ?1",
|
||||
(false, false) => "",
|
||||
};
|
||||
|
||||
let params = match (token_id, pkh) {
|
||||
(None, None) => params![],
|
||||
(None, Some(pkh)) => params![pkh.to_string()],
|
||||
(Some(token), None) => params![token.to_string()],
|
||||
(Some(token), Some(pkh)) => params![token.to_string(), pkh.to_string()],
|
||||
};
|
||||
|
||||
let query: String = "
|
||||
SELECT
|
||||
p.owner_pkh,
|
||||
phe.sats,
|
||||
phe.token_amount,
|
||||
phe.txid,
|
||||
phe.tx_pos,
|
||||
p.token_id
|
||||
FROM
|
||||
pool p
|
||||
JOIN pool_history_entry phe ON p.creation_utxo = phe.pool
|
||||
JOIN utxo_funding uf ON phe.utxo = uf.new_utxo_hash
|
||||
LEFT JOIN utxo_spending us ON uf.new_utxo_hash = us.spent_utxo_hash
|
||||
WHERE
|
||||
p.withdrawn_in_utxo IS NULL
|
||||
AND us.spent_utxo_hash IS NULL
|
||||
"
|
||||
.to_string();
|
||||
|
||||
if token_id.is_some() {
|
||||
query.push_str(" AND p.token_id = ?");
|
||||
}
|
||||
if pkh.is_some() {
|
||||
query.push_str(" AND p.owner_pkh = ?");
|
||||
}
|
||||
{filters}
|
||||
GROUP BY
|
||||
p.creation_utxo
|
||||
ORDER BY
|
||||
phe.sequence DESC"
|
||||
.replace("{filters}", filters);
|
||||
|
||||
let mut stmt = conn.prepare(&query)?;
|
||||
let mut rows = match (token_id, pkh) {
|
||||
(Some(t), Some(k)) => stmt.query([t, k])?,
|
||||
(None, Some(k)) => stmt.query([k])?,
|
||||
(Some(t), None) => stmt.query([t])?,
|
||||
(None, None) => stmt.query([])?,
|
||||
};
|
||||
let mut rows = stmt.query(params)?;
|
||||
|
||||
let mut pools: Vec<ActivePool> = vec![];
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue