将LIMITed SELECT和总COUNT组合在一起?

时间:2014-07-25 22:38:27

标签: sql postgresql amazon-redshift

我有一个名为profile的表,其中包含用户信息。我需要对此表进行过滤查询并获取:

  1. 与此查询匹配的行数。
  2. 仅限前5000个匹配行的数据。
  3. 我正在寻找一种最佳方法。显然,至少需要进行一次扫描来进行计数,但理想情况下,DB可以在进行计数时获取最高匹配。

    以下查询给出了正确的结果,但看起来有点hacky。我想知道它能否做得更好?

    WITH total AS (
         SELECT COUNT(*) AS total FROM profile 
             WHERE project_id = 1 and some_prop = 100)
    SELECT total.total, full_name, other_prop 
        FROM profile
        INNER JOIN total ON 1 = 1
        WHERE project_id = 1 and some_prop = 100
        ORDER BY full_name ASC
        LIMIT 5000
    

    有更有效的方法吗?

1 个答案:

答案 0 :(得分:2)

您正在扫描同一个表两次以应用过滤器。使用下面的代码,您只需在应用过滤器后扫描表格,然后执行总计并在过滤后的表格中列出两者。

with s as (
    select *
    from profile 
    where project_id = 1 and some_prop = 100
), t as (
    select count(*) as total from s
)
select total, full_name, other_prop 
from s cross join t
order by full_name asc
limit 5000

窗口功能版

select
    count(*) over() as total,
    full_name,
    other_prop 
from profile
where project_id = 1 and some_prop = 100
order by full_name asc
limit 5000