0
下面是我用我的Postgres的原始查詢 -如何在Amazon Redshift中編寫此postgres查詢,使其與postgres中的優化程序一樣優化?
SELECT a.id,
(SELECT val
FROM database.detail x
WHERE name = 'blablah'
AND x.id = b.id) AS myGroup,
c.username,
a.someCode,
a.timeTaken,
a.date ::timestamp WITH time ZONE AT time ZONE 'PST' AS date,
SUM (CASE WHEN (b.name = 'name1') THEN b.val ::INTEGER ELSE 0 END) AS name11,
SUM (CASE WHEN (b.name = 'name2') THEN b.val ::INTEGER ELSE 0 END) AS name12
FROM
database.myTable a,
database.detail b,
database.client c
WHERE
a.id = b.id
AND a.c_id = c.c_id
AND a.date > current_date - interval '2 weeks'
GROUP BY 1, 2, 3, 4, 5, 6
以下是我如何轉換此查詢到亞馬遜紅移查詢。
SELECT a.id,
b.val AS myGroup,
c.username,
a.someCode,
a.timeTaken,
convert_timezone('PST', a.date) AS date,
SUM (CASE WHEN (b.name = 'name1') THEN b.val ::INTEGER ELSE 0 END) AS name11,
SUM (CASE WHEN (b.name = 'name2') THEN b.val ::INTEGER ELSE 0 END) AS name12
FROM
database.myTable a,
database.detail b,
database.client c
WHERE
a.id = b.id
AND b.name = 'blablah'
AND a.c_id = c.c_id
AND a.date > current_date - interval '2 weeks'
GROUP BY 1, 2, 3, 4, 5, 6 LIMIT 10
CASE語句似乎沒有執行預期的方式,基本上name11和name12的值都是零。我的postgres查詢返回這些值的有效值,但紅移查詢不。
另外,這個查詢非常慢。 Postgres查詢需要大約150毫秒,這個查詢需要2分鐘。
我們該如何做得更好?
我得到一個錯誤 - 字符不存在---爲myGroupSet爲(SELECT b.val AS ....看起來它是不能夠訪問表b? – Tisha
朋友,SQL只是一個基於你的輸入的模擬SQL。請根據你的實際表格和列調整SQL。我分享了一個有關Redshift如何有效的概念以及如何將ANSI SQL轉換爲兼容Redshift SQL。 – kadalamittai