目前我正在使用此查询从prices表中获取具有最新价格和库存的所有产品:
SELECT
products.*,
prices.price AS latest_price,
prices.stock AS latest_stock
FROM products
LEFT JOIN prices ON prices.id = (
SELECT id
FROM prices
WHERE prices.product_id = products.id AND prices.created_at >= '2018-08-18 00:00:00'
ORDER BY created_at DESC
LIMIT 1
)
WHERE products.shop_id = 1
小提琴
但这是缓慢的许多产品(数百万)和许多价格(数十亿)。如何改进此查询?一般来说,连接速度更快,但是使用连接可以实现这一点吗?我正在尝试:
SELECT
ANY_VALUE(products.id),
ANY_VALUE(products.name),
prices.price, -- Which aggregate function to use?
MAX(prices.created_at)
FROM products
LEFT JOIN prices ON prices.product_id = products.id AND prices.created_at >= '2018-08-22 00:00:00'
WHERE shop_id = 3
GROUP BY products.id
但现在我必须指定所有产品列和 Package ANY_VALUE()
围绕着它?以及 MAX()
正在选择最新的 created_at
从价格日期,但我如何选择 price
从同一排?
暂无答案!
目前还没有任何答案,快来回答吧!