AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 11366
Accepted
Bob Jansen
Bob Jansen
Asked: 2012-01-24 01:20:12 +0800 CST2012-01-24 01:20:12 +0800 CST 2012-01-24 01:20:12 +0800 CST

有效地找到不同的值

  • 772

我有许多带有主键(月、年、数字)的表,并且不同的基数有所不同。对于元组(月、年),历史不会追溯到很久以前,从长远来看,这可能不会超过 50。对于每个(月、年)元组,唯一数字不超过 200 万个。我想知道哪些月份和年份的组合可用。我使用此查询执行此操作:

select month, year from table group by month, year

这会返回正确的结果,但似乎效率不高。获得此结果的有效方法是什么(利用唯一索引)?

调优顾问建议为这个查询添加一个月年索引,但这似乎很浪费,因为已经有一个更大的索引可用。

oracle
  • 2 2 个回答
  • 2293 Views

2 个回答

  • Voted
  1. Best Answer
    Jack Douglas
    2012-01-24T02:20:46+08:002012-01-24T02:20:46+08:00

    您可以使用以下技术的变体 - 强制重复“最小/最大”范围扫描:

    假设

    1. 您可以生成所有可能的年/月组合的列表
    2. number不为空(它不能像在 PK 中那样,但我提到它是因为如果允许空值,有一种解决方法)

    试验台:

    create table foo(month, year, num, primary key(month, year, num)) as
    with m as ( select extract(month from d) as month, extract(year from d) as year
                from (select add_months(sysdate,1-level) as d from dual connect by level<50) )
    select month, year, num
    from m cross join 
         (select level as num from dual connect by level<100000 order by dbms_random.random());
    

    正常查询:

    select distinct month, year from foo;
    --gets=11656
    

    最小/最大技术:

    with m as ( select extract(month from d) as month, extract(year from d) as year
                from (select add_months(sysdate,1-level) as d from dual connect by level<50) )
    select month, year, decode(( select min(num)
                                 from foo
                                 where month=m.month and year=m.year )
                               ,null, 'N', 'Y') as has_data_yn
    from m;
    --gets=294
    

    回应评论的一些解释:

    在每种情况下(测试台和最小/最大查询),子查询因式分解子句只生成一个(年、月)元组列表:

    with m as ( select extract(month from d) as month, extract(year from d) as year
                from (select add_months(sysdate,1-level) as d from dual connect by level<50) )
    select * from m;
    /*
    MONTH                  YEAR                   
    ---------------------- ---------------------- 
    1                      2012                   
    12                     2011                   
    11                     2011                   
    10                     2011           
    ...
    ...
    */
    

    然后,该技术在select子句中使用子查询来检查(月,年)是否存在任何行——这个子查询必须最多只能产生 1 行:

    select min(num)
    from foo
    where month=m.month and year=m.year;
    

    这非常快,因为它利用了 PK 的有序性质 - 但是它需要每个月执行一次 - 如果每个月有数百万行是有意义的,但如果有足够少的行适合少数块。

    • 5
  2. Leigh Riffel
    2012-01-25T06:42:15+08:002012-01-25T06:42:15+08:00

    这是一个使用与 Jack Douglas (+1) 相同的技术的解决方案。它使用他的测试平台产生相同数量的一致获取,但是否更容易理解将在旁观者的眼中。

    SELECT extract(month from d) m, extract(year from d) y 
    FROM (SELECT add_months(sysdate,1-level) d FROM dual CONNECT BY level < 50)
    WHERE EXISTS (
     SELECT 1 FROM foo WHERE month=extract(month from d) AND year=extract(year from d)
    );
    

    此选项使用 select from dual 来驱动查询,而 select from foo 仅用于决定保留哪些日期。

    同样的查询也可以这样写:

    SELECT * FROM (
       SELECT extract(month from add_months(sysdate,1-level)) m 
            , extract(year from add_months(sysdate,1-level)) y 
          FROM dual CONNECT BY level < 50)
    WHERE EXISTS (
       SELECT 1 FROM foo WHERE month=m AND year=y
    );
    
    • 3

相关问题

  • Oracle 中的数据库备份 - 导出数据库还是使用其他工具?

  • ORDER BY 使用文本列的自定义优先级

  • 舒服的sqlplus界面?[关闭]

  • 如何在数据库中找到最新的 SQL 语句?

  • 如何使用正则表达式查询名称?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何查看 Oracle 中的数据库列表?

    • 8 个回答
  • Marko Smith

    mysql innodb_buffer_pool_size 应该有多大?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    从 .frm 和 .ibd 文件恢复表?

    • 10 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    如何选择每组的第一行?

    • 6 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    bernd_k 什么时候应该使用唯一约束而不是唯一索引? 2011-01-05 02:32:27 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve