I am trying to rewrite the following query without using sub-query since I cannot afford hash join.
SELECT
COUNT(wins.bid) as wins,
to_char((TIMESTAMP 'epoch' + bidRequestTime * INTERVAL '1 Second'), 'DD') as date,
COUNT(impression.bid) as impression,
lid,
COUNT(b.bid) as requests,
ROUND(SUM(spendCalc),2) as spend
FROM (
SELECT lid, bids.bid,bidRequestTime,price::float*COUNT(impression.bid)/1000::float/.80::float as spendCalc
FROM bids
LEFT OUTER JOIN wins on wins.bid = bids.bid
LEFT OUTER JOIN impressions impression ON impression.bid = bids.bid
WHERE bidRequestTime BETWEEN 1470614400 AND 1471219200
GROUP BY price, bids.bid, bidRequestTime, lid
) b
LEFT OUTER JOIN wins on wins.bid = b.bid
LEFT OUTER JOIN impressions impression ON impression.bid = b.bid
WHERE bidRequestTime BETWEEN 1470614400 AND 1471219200
GROUP BY lid, date
Any suggestions? Thanks in Advance.
Hard to tell exactly what your problem is... but here are some tricks removing the outer joins and using a CTE to reduce the search space.... may or may not work, would need to see more details about your model to be sure.
WITH mybid as
(
SELECT bid, bidRequestTime
FROM bids
WHERE bidRequestTime BETWEEN 1470614400 AND 1471219200
)
SELECT
COUNT(wins_bid) as wins,
to_char((TIMESTAMP 'epoch' + bidRequestTime * INTERVAL '1 Second'), 'DD') as date,
COUNT(imp_bid) as impression,
lid,
COUNT(b.bid) as requests,
ROUND(SUM(spendCalc),2) as spend
FROM (
SELECT wins.bid as wins_bid, lid, bids.bid, bidRequestTime, impression.bid as imp_bid, price::float*COUNT(impression.bid)/1000::float/.80::float as spendCalc
FROM mybid as bids
LEFT OUTER JOIN wins on wins.bid = bids.bid
LEFT OUTER JOIN impressions impression ON impression.bid = bids.bid
GROUP BY price, bids.bid, bidRequestTime, lid
) b
GROUP BY lid, date
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.