【问题标题】:Get first 100 records in a table获取表中的前 100 条记录
【发布时间】:2014-08-09 00:23:33
【问题描述】:

我有一个庞大的数据库,其中包含数百万行,我的查询中包含多个表。我想测试我的查询,以便知道我的查询是否正常工作。

如果我运行我的查询,它需要几个小时才能给出查询的输出,并且在阅读了 oracle 中的 Rownum 之后我尝试了,但 rownum 仅在查询执行后执行。

有什么快速的方法来测试我的查询,以便我可以显示前 100 行。

select 
p.attr_value product,
m.attr_value model,
u.attr_value usage,
l.attr_value location
    from table1 t1 join table2 t2 on t1.e_subid = t2.e_subid
                   join table4 t4 on t4.loc_id = t1.loc_id
                   join table3 p  on t2.e_cid = p.e_cid 
                   join table3 m  on t2.e_cid = m.e_cid 
                   join table3 u  on t2.e_cid = u.e_cid 
  Where
      t4.attr_name = 'SiteName' 
      and p.attr_name  = 'Product'
      and m.attr_name  = 'Model'
      and u.attr_name  = 'Usage'
      order by product,location;

attempt1:获取前100名的查询结果

select 
p.attr_value product,
m.attr_value model,
u.attr_value usage,
l.attr_value location
    from table1 t1 join table2 t2 on t1.e_subid = t2.e_subid
                   join table4 t4 on t4.loc_id = t1.loc_id
                   join table3 p  on t2.e_cid = p.e_cid 
                   join table3 m  on t2.e_cid = m.e_cid 
                   join table3 u  on t2.e_cid = u.e_cid 
  Where
      ROWNUM <= 100 
      and t4.attr_name = 'SiteName' 
      and p.attr_name  = 'Product'
      and m.attr_name  = 'Model'
      and u.attr_name  = 'Usage'
      order by product,location;

我确实尝试了上述方法,并在几分钟内得到了一些结果,但不确定这是否是正确的方法......你觉得呢?

【问题讨论】:

  • +1 感谢您的尝试。
  • @WillMarcouiller 谢谢

标签: oracle rownum


【解决方案1】:

试试这个来获取 100 条记录:

  select 
p.attr_value product,
m.attr_value model,
u.attr_value usage,
l.attr_value location
    from table1 t1 join table2 t2 on t1.e_subid = t2.e_subid
                   join table4 t4 on t4.loc_id = t1.loc_id
                   join table3 p  on t2.e_cid = p.e_cid 
                   join table3 m  on t2.e_cid = m.e_cid 
                   join table3 u  on t2.e_cid = u.e_cid 
  Where
      t4.attr_name = 'SiteName' 
      and p.attr_name  = 'Product'
      and m.attr_name  = 'Model'
      and u.attr_name  = 'Usage'
      and ROWNUM <= 100
      order by product,location;

另请注意,Oracle 在返回结果后会将 rownum 应用于结果。

但是,您可以尝试使用以下方法检查表中是否存在该值:

select case 
            when exists (select 1
        from table1 t1 join table2 t2 on t1.e_subid = t2.e_subid
                       join table4 t4 on t4.loc_id = t1.loc_id
                       join table3 p  on t2.e_cid = p.e_cid 
                       join table3 m  on t2.e_cid = m.e_cid 
                       join table3 u  on t2.e_cid = u.e_cid 
      Where
          t4.attr_name = 'SiteName' 
          and p.attr_name  = 'Product'
          and m.attr_name  = 'Model'
          and u.attr_name  = 'Usage'
          order by product,location;
) 
    then 'Y' 
            else 'N' 
        end as rec_exists
from dual;

【讨论】:

  • 我已经尝试过了,它会等待整个内部选择查询执行,然后它只会给出前 100 个......所以最终我还要等待几个小时......
  • @user3384231:- 我认为这是获得它的唯一方法。如果您只想检查该值是否存在,那么您可以尝试使用 EXISTS
  • @user3384231:- 更新了我的答案。检查是否有帮助!
  • 我已经更新了我尝试过的答案...请查看是否正确
  • @user3384231:- 是的,没关系。那是我第二次编辑的! :)
猜你喜欢
  • 2013-08-29
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
相关资源
最近更新 更多