標準的方式來獲得你的結果牽涉到非相等聯接,這將是一個產品中加入解釋。首先複製20000行,之後進行700萬* 20000比較,並在計數之前設置一個巨大的中間軸。
有基於OLAP的功能的解決方案,通常都是很有效的:
SELECT dt.*,
-- Do a cumulative count of the rows of table #2
-- sorted by size, i.e. count number of rows with a size #2 less size #1
Sum(CASE WHEN NAME = '' THEN 1 ELSE 0 end)
Over (ORDER BY SIZE, NAME DESC ROWS Unbounded Preceding)
FROM
(-- mix the rows of both tables, an empty name indicates rows from table #2
SELECT id, name, size
FROM a
UNION ALL
SELECT id, '', size
FROM b
) AS dt
-- only return the rows of table #1
QUALIFY name <> ''
如果有與表#相同尺寸2你最好的聯盟之前數減少尺寸多行:
SELECT dt.*,
-- Do a cumulative sum of the counts of table #2
-- sorted by size, i.e. count number of rows with a size #2 less size #1
Sum(CASE WHEN NAME ='' THEN id ELSE 0 end)
Over (ORDER BY SIZE, NAME DESC ROWS Unbounded Preceding)
FROM
(-- mix the rows of both tables, an empty name indicates rows from table #2
SELECT id, name, size
FROM a
UNION ALL
SELECT Count(*), '', SIZE
FROM b
GROUP BY SIZE
) AS dt
-- only return the rows of table #1
QUALIFY NAME <> ''
究竟是什麼*相當巨大*?有沒有任何類型的分組,或者它實際上是比較所有行? – dnoeth
是桃子6還是9表a? – Beth
@Beth。應該是9.對於錯字 –