结合SQL语句进行优化

时间:2014-01-08 20:27:17

标签: sql postgresql query-optimization aggregate amazon-redshift

我在同一张桌子上有两个不同的查询。第一个是形式 -

SELECT MIN(A) AS MIN_A, MAX(B) AS MAX_B
FROM myTable
WHERE timeStamp > ?
GROUP BY type;

另一个:

SELECT SUM (CASE WHEN io > 0 THEN 1 ELSE 0 END) as io_cnt
FROM (
     SELECT
     (CASE WHEN SUM(io_ops) > 0 THEN 1 ELSE 0 END) as io
     FROM myTable
     WHERE timestamp > ? AND type = ?
     GROUP BY id
) t;

表有列--A,B,id,时间戳,类型。 现在我从java调用第一个查询,获取结果集输出并循环遍历它,为第一个查询中返回的每个类型调用第二个查询。

我需要来自第一次查询的MIN(A),MAX(B)和来自第二次查询的io计数。是否可以在一个查询中执行此操作?我使用Amazon Redshift作为我的数据库。

1 个答案:

答案 0 :(得分:1)

Redshift is pretty limited。它基于PostgreSQL 8.0.2,不支持许多新功能。这应该工作(未经测试):

SELECT t.type, min(min_a) AS min_a, max(max_b) AS max_b
      ,count(io > 0 OR NULL) AS io_cnt
FROM  (
   SELECT type, min(a) as min_a, max(b) as max_b
         ,sum(io_ops) AS io
   FROM   myTable
   WHERE  timestamp > ?
   GROUP  BY type, id
   ) t
GROUP  BY t.type;

根据数据分布,这可能更快或更快:

SELECT t.type, m.min_a, m.max_b, count(io > 0 OR NULL) AS io_cnt
FROM  (
   SELECT type, sum(io_ops) AS io
   FROM   myTable
   WHERE  timestamp > ?
   GROUP  BY type, id
   ) t
JOIN  (
   SELECT type, min(a) as min_a, max(b) as max_b
   FROM   myTable
   WHERE  timeStamp > ?
   GROUP  BY type
   ) m USING (type)
GROUP  BY  t.type, m.min_a, m.max_b;