zoukankan      html  css  js  c++  java
  • 大表查询只查一次之WITH函数 老猫

    前言:关于oracle sql查询的优化规则,一般建议大表最好只查询一次。那么,怎么样就能做到前一次的大表查询结果能被下一个查询用到呢?我认为WITH很不错。

     

    with a as (select dummy from dual),
    b as (select dummy from dual)
    select a.dummy,b.dummy from a,b
    where a.dummy = b.dummy

    /********************************/
    Starting in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH clause”, a tool for materializing subqueries to save Oracle from having to re-compute them multiple times.

    The SQL “WITH clause” is very similar to the use of Global temporary tables (GTT), a technique that is often used to improve query speed for complex subqueries. Here are some important notes about the Oracle “WITH clause”:

    • The SQL “WITH clause” only works on Oracle 9i release 2 and beyond.
    • Formally, the “WITH clause” is called subquery factoring
    • The SQL “WITH clause” is used when a subquery is executed multiple times
    • Also useful for recursive queries (SQL-99, but not Oracle SQL)

    To keep it simple, the following example only references the aggregations once, where the SQL “WITH clause” is normally used when an aggregation is referenced multiple times in a query.
    We can also use the SQL-99 “WITH clause” instead of temporary tables. The Oracle SQL “WITH clause” will compute the aggregation once, give it a name, and allow us to reference it (maybe multiple times), later in the query.

    The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

    WITH
    subquery_name
    AS
    (the aggregation SQL statement)
    SELECT
    (query naming subquery_name);


    Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH clause”:

    WITH
    sum_sales AS
    select /*+ materialize */
    sum(quantity) all_sales from stores
    number_stores AS
    select /*+ materialize */
    count(*) nbr_stores from stores
    sales_by_store AS
    select /*+ materialize */
    store_name, sum(quantity) store_sales from
    store natural join sales
    SELECT
    store_name
    FROM
    store,
    sum_sales,
    number_stores,
    sales_by_store
    where
    store_sales > (all_sales / nbr_stores)

    ;

    Note the use of the Oracle undocumented “materialize” hint in the “WITH clause”. The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary tables that are created inside the “WITH” clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.

    It should be noted that the “WITH clause” does not yet fully-functional within Oracle SQL and it does not yet support the use of “WITH clause” replacement for “CONNECT BY” when performing recursive queries.

    To see how the “WITH clause” is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick’s great work “Understanding the WITH Clause” showing the use of the SQL-99 “WITH clause” to traverse a recursive bill-of-materials hierarchy

    The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

    WITH
    subquery_name
    AS
    (the aggregation SQL statement)
    SELECT
    (query naming subquery_name);


    Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH” clause”:
  • 相关阅读:
    实现镜面模糊效果
    在网页中单行以及多行内容超出之后隐藏
    利用xhsell登录到远程腾讯云服务器
    highcharts饼状图使用案例
    在利用xampp开发时候为apache设置多个项目目录
    Linux查找命令
    数字签名与数字证书
    数据库之闭包,范式
    利用PHP绘图函数实现简单验证码功能
    IC基础(二):设计中常见的时序问题
  • 原文地址:https://www.cnblogs.com/oldcat/p/2164593.html
Copyright © 2011-2022 走看看