Skip to content

Consider calculating Trending Score on the database side #95

@davidyuk

Description

@davidyuk

The current approach reduces the database normalization by adding a score column based on the data from other tables. Potentially, it can improve performance, but at the cost of extra code and data management.

While these computations can be delegated to the database, so we can rely on its internal optimizations.

SQL has enough features for that. This is how it can be implemented:

SELECT
    0.6 * COUNT(transactions.tx_hash) / <max>
      * 0.4 * SUM(transactions.volume) / <max>
      / LEAST(EXTRACT(EPOCH FROM current_timestamp - token.created_at::timestamp)::INTEGER, 60 * 60 * 24) / (60 * 60 * 24)
    as score,
    token.sale_address
FROM public.token
INNER JOIN public.transactions ON token.sale_address = transactions.sale_address
WHERE transactions.created_at > current_date - 1000
GROUP BY token.sale_address
ORDER BY score DESC

<max> can also be evaluated in the same query, but it takes a significant time for me to remember

found while checking #91

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions