TiDB Cloud Logo
TiDB LogoSign up for free
Top
Time range
-- It is a particularly resource-intensive analytical query.
-- The events table generates an average of 1.8 million records per day.
WITH top_merchandises AS (
      SELECT 
          product_id, 
          COUNT(*) AS viewed
      FROM events
      WHERE event_type = 'view'
          AND event_time >= '${start_time}'
          AND event_time <= '${end_time}'
      GROUP BY product_id
      ORDER BY viewed DESC
      LIMIT ${top_n}
)
SELECT 
      products.product_id, 
      products.product_name, 
      top_merchandises.viewed
FROM top_merchandises
JOIN products ON top_merchandises.product_id = products.product_id
ORDER BY viewed DESC
MySQL RDS
your results will display here
TiDB Logo
TiDB Cloud Serverless
your results will display here

Copyright © PingCAP 2024.