zoukankan      html  css  js  c++  java
  • 在PostgreSQL中,如何模拟Oracle的hint效果

    Oracle 的SQL文,可以强制指定各种 hint。

    但是在PostgreSQL中是不支持的。

    其wiki 是这样说的:

    http://wiki.postgresql.org/wiki/OptimizerHintsDiscussion

    We are not interested in implementing hints in the exact ways they are commonly implemented on other databases. Proposals based on "because they've got them" will not be welcomed. If you have an idea that avoids the problems that have been observed with other hint systems, that could lead to valuable discussion.

    但是可以通过如下的postgresql.conf参数来调节:

    # - Planner Method Configuration -
    
    #enable_bitmapscan = on
    #enable_hashagg = on
    enable_hashjoin = on
    #enable_indexscan = on
    #enable_indexonlyscan = on
    #enable_material = on
    enable_mergejoin = on
    enable_nestloop = on
    #enable_seqscan = on
    #enable_sort = on
    #enable_tidscan = on

    对于我的查询:

    explain select  dept.no_emps,emp.age from dept,emp where emp.name = dept.mgr and dept.dept_name = 'shoe';

    如果 enable_hashjoin = on,其他也为on,则执行计划是:

    postgres=# explain select dept.no_emps,emp.age from dept,emp where emp.name = dept.mgr and dept.dept_name = 'shoe';
                                QUERY PLAN                            
    ------------------------------------------------------------------
     Hash Join  (cost=19.30..45.07 rows=23 width=8)
       Hash Cond: ((emp.name)::text = (dept.mgr)::text)
       ->  Seq Scan on emp  (cost=0.00..21.30 rows=1130 width=42)
       ->  Hash  (cost=19.25..19.25 rows=4 width=42)
             ->  Seq Scan on dept  (cost=0.00..19.25 rows=4 width=42)
                   Filter: ((dept_name)::text = 'shoe'::text)
    (6 rows)
    
    postgres=#

    如果 enable_hashjoin=off,其他为on,则执行计划是:

    postgres=# explain select dept.no_emps,emp.age from dept,emp where emp.name = dept.mgr and dept.dept_name = 'shoe';
                                 QUERY PLAN                             
    --------------------------------------------------------------------
     Merge Join  (cost=97.89..103.79 rows=23 width=8)
       Merge Cond: ((dept.mgr)::text = (emp.name)::text)
       ->  Sort  (cost=19.29..19.30 rows=4 width=42)
             Sort Key: dept.mgr
             ->  Seq Scan on dept  (cost=0.00..19.25 rows=4 width=42)
                   Filter: ((dept_name)::text = 'shoe'::text)
       ->  Sort  (cost=78.60..81.43 rows=1130 width=42)
             Sort Key: emp.name
             ->  Seq Scan on emp  (cost=0.00..21.30 rows=1130 width=42)
    (9 rows)
    
    postgres=#

    如果enable_hashjoin 为 off,而 enable_mergejoin也为 off,则执行计划为:

    postgres=# explain select dept.no_emps,emp.age from dept,emp where emp.name = dept.mgr and dept.dept_name = 'shoe';
                                QUERY PLAN                            
    ------------------------------------------------------------------
     Nested Loop  (cost=0.00..108.36 rows=23 width=8)
       Join Filter: ((dept.mgr)::text = (emp.name)::text)
       ->  Seq Scan on emp  (cost=0.00..21.30 rows=1130 width=42)
       ->  Materialize  (cost=0.00..19.27 rows=4 width=42)
             ->  Seq Scan on dept  (cost=0.00..19.25 rows=4 width=42)
                   Filter: ((dept_name)::text = 'shoe'::text)
    (6 rows)
    
    postgres=#
  • 相关阅读:
    java 中文排序 中文拼音排序 pinyin4j (怡,阿等) 拂晓风起
    jQuery 和 json 简单例子(注意callback函数的处理!!) (servlet返回json,jquery更新,java json) 拂晓风起
    推荐一个免费在线制作Banner的好地方
    Jquery焦点图/幻灯片效果 插件 KinSlideshow
    C#关于伪静态页面的两种实现方法
    cu3er 3D幻灯切换效果 div被遮住的解决方法
    推荐一个亲子教学网站,悟空学字
    怎么通过小米账号查出买家的手机号?
    添加网页桌面快捷方式的代码
    卖小米资格号怎么才不会受骗,怎么才不会淘宝退款?
  • 原文地址:https://www.cnblogs.com/gaojian/p/3130852.html
Copyright © 2011-2022 走看看