Query Rewrite with Materialized Views 用物化视图重写查询
(page 72)
Query rewrite is a transformation that occurs when a query, or a portion
of a query , has been saved as a materialized view and the transformer
can rewrite the query to use the precomputed materialized view data
instead of executing the current query.
A materialized view is like a
normal view except that the query has been executed and its result set
has been stored in a table. What this does is to precompute the result
of the query and make it available whenever the specific query is
executed. That means that all the work to determine the plan, execute
it, and gather up all the data has already been done. So, when the same
query is executed again, there is no need to go through all that effort
again.
查询重写是一种变换发生在,当一查询,或一部分查询,(执行后)作为物化视图保存且变换器可使用预先计算好的物化视图数据重写查询,而不是执行当前的查询。
除了查询已经执行且它的结果已经存入表中以外,物化视图像普通视图一样。(物化视图所作的是)预先计算查询的结果且在特定查询执行时使其(结果)
有效。这意味着所有确定计划,执行计划,收集全部数据的工作已经完成。这样的话,当相同的查询再次执行时,就不再需要重头到尾费力一番。
The query transformer will match a query with available materialized
views and then rewrite the query to simply select from the materialized
result set.
Listing 2-10 walks through creating a materialized view and
how the transformer would rewrite the query to use the materialized view
result set .
查询变化器将把一个查询和有效的物化视图相匹配,然后重写查询,直接的从物化的结果集中选取(数据)。
列表2-10通过创建一物化视图,展示变换器如何用物化视图的结果集重写查询。
Listing 2-10. Query Rewrite with Materialized Views
SQL > set autotrace traceonly explain
SQL >
SQL > SELECT p.prod_id, p.prod_name, t.time_id, t.week_ending_day,
2 s.channel_id, s.promo_id, s.cust_id, s.amount_sold
3 FROM sales s, products p , times t
4 WHERE s.time_id = t.time_id AND s.prod_id = p.prod_id;
Execution Plan
--------------------------------------------------------------------------
Plan hash value : 1109402314
-----------------------------------------------------------------------------------------------------------------------------------
|
Id | Operation | Name
| Rows | Bytes | Cost (%CPU ) | Pstart | Pstop |
-----------------------------------------------------------------------------------------------------------------------------------------
|
0 | SELECT STATEME N T | | 918
K| 65 M | 485 (17) | | |
|*
1 | HASH JOIN |
| 918 K| 65 M | 485 (17) | | |
|
2 | TABLE ACCESS FULL | TIMES | 1826
| 29216 | 15 (0) | | |
|*
3 | HASH JOIN |
| 918 K| 51 M | 453 (14) | |
|
| 4 | TABLE ACCESS FULL | PRODUCTS |
72 | 2160 | 3 (0) | | |
|
5 | PARTITION RANGE ALL | | 918
K| 25 M | 434 (11) | 1 | 28 |
|
6 | TABLE ACCESS FULL | SALES | 918
K| 25 M | 434 (11) | 1 | 28 |
------------------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
-------------------------------------------------------------------------------
1 - access("S". "TIME_ID" = "T" . "TIME_ID")
3 - access("S". "PROD_ID" = "P"."PROD_ID")
SQL >
SQL > set autotrace off
SQL>
SQL> CREATE MATERIALIZED VIEW sales_time_product_mv
2 ENABLE QUERY REWRITE AS
3 SELECT p.prod_id, p.prod_name, t.time_id, t.week_ending_day,
4 s.channel_id, s.promo_id, s.cust_id, s.amount_sold
5 FROM sales s, products p, times t
6 WHERE s.time_id=t.time_id AND s.prod_id = p.prod_id;
SQL>
SQL> set autotrace traceonly explain
SQL>
SQL> SELECT p.prod_id, p.prod_name, t.time_id, t.week_ending_day,
2 s.channel_id, s.promo_id, s.cust_id, s.amount_sold
3 FROM sales s, products p, times t
4 WHERE s.time_id=t.time_id AND s.prod_id = p.prod_id;
Execution Plan
----------------------------------------------------------
Plan hash value: 1109402314
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Pstart| Pstop |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 918K | 65M | 485 (17) | | |
|* 1 | HASH JOIN | | 918K | 65M | 485 (17) | | |
| 2 | TABLE ACCESS FULL | TIMES | 1826 | 29216 | 15 (0) | | |
|* 3 | HASH JOIN | | 918K | 51M | 453 (14) | | |
| 4 | TABLE ACCESS FULL | PRODUCTS | 72 | 2160 | 3 (0) | | |
| 5 | PARTITION RANGE ALL | | 918K | 25M | 434 (11) | 1 | 28 |
| 6 | TABLE ACCESS FULL | SALES | 918K | 25M | 434 (11) | 1 | 28 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("S"."TIME_ID"="T"."TIME_ID")
3 - access("S"."PROD_ID"="P"."PROD_ID")
Note
-----
- dynamic sampling used for this statement
SQL>
SQL> SELECT /*+ rewrite(sales_time_product_mv) */
2 p.prod_id, p.prod_name, t.time_id, t.week_ending_day,
3 s.channel_id, s.promo_id, s.cust_id, s.amount_sold
4 FROM sales s, products p, times t
5 WHERE s.time_id=t.time_id AND s.prod_id = p.prod_id;
Execution Plan
----------------------------------------------------------
Plan hash value: 663088863
-------------------------------------------------------------------------------------------
|
Id | Operation |
Name | Rows | Bytes | Cost (%CPU)|
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 909K| 95M| 1935 (3)|
| 1 | MAT_VIEW REWRITE ACCESS FULL| SALES_TIME_PRODUCT_MV | 909K| 95M| 1935 (3)|
-------------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
In order to keep the example simple, I used a REWRITE
hint to turn on the query rewrite
transformation. You can enable query rewrite to happen automatically as well. But as you notice in the
example, when the rewrite does occur, the plan simply shows a full access on the materialized view
instead of the entire set of operations required to produce the result set originally.
As you can imagine,
the time savings can be substantial for complicated queries with large results sets, particularly if the
query contains aggregations. For more information on query rewrite and materialized views, refer to
The Oracle Data Warehousing Guide where you’ll find an entire chapter on advanced query rewrite.
为了让例子简单,我使用REWRITE提示打开查询重写变换。你也能让查询重写自动有效。但是如你在例子中所见,当查询重写发生时,计划直接展示在物化视图上的全扫描而不是最初产生结果集所需的整个操作集合。
你能想象,对于复杂的大结果集的查询能实质上的节约时间,特别是如果查询包含聚合操作。更多的信息关
于查询重写和物化视图,参考《Oracle数据仓库指南》,其中你能找到整章的查询重写的高级(论述)。
分享到:
相关推荐
NohBoard Rewrite v1.3.0 是一个更新版本的软件工具,专注于提供键盘管理和定制化功能。这个软件的核心可能是为了提升用户在不同设备或场景下的打字体验,特别是对于那些需要频繁切换输入设备或者有特定输入需求的...
- 使用`DBMS_ADVANCED_REWRITE.REWRITE_QUERY`函数重写SQL语句。 - 使用`DBMS_ADVANCED_REWRITE.CACHE_PLAN`将重写后的计划缓存起来。 - **2.1.15 Histogram Investigation** - 统计直方图是用于提高CBO性能的...
安装只需在您的shell环境中键入: eg plugin install express-gateway-plugin-rewrite快速开始确保该插件在列出。 如果您使用了上面的命令,这将自动为您完成。 将配置密钥添加到。 policies : - rewrite : - ...
"connect-url-rewrite" 就是一个这样的工具,它是一个基于Connect或Express的中间件,专门用于处理URL重写。这个库的核心功能是允许开发者使用正则表达式来实现与Apache服务器的mod_rewrite模块类似的功能,对HTTP...
leetcode下载Leetcode-SQL-rewrite-using-Python- 这是一个由黄磊创建的记录Leetcode SQL实践的仓库。 175. 合并两张表 sql select FirstName, LastName, City, State from Person P left join Address A on P . ...
本文主要讲述 Cost-based Query Optimization 的概念和实现,包括 Apache Phoenix 和 Apache Calcite 的集成,旨在提高查询性能和SQL标准兼容性。 Cost-based Query Optimization 是一种优化查询的方法,通过统计...
2. **易用性**:该工具提供直观的用户界面,方便用户创建和管理重写规则,无需深入理解正则表达式。 3. **兼容性**:IIS URL重写模块与多种应用程序框架兼容,包括ASP.NET、PHP、经典ASP等。 4. **性能优化**:通过...
《URLRewrite:掌握网页地址重写的关键技术》 URLRewrite是一款强大的URL管理工具,尤其在Java Web应用中广泛使用,它可以帮助开发者实现URL的重写和转换,以达到优化URL、隐藏真实路径、提高SEO效果等目的。在本篇...
《SQL Optimizer for Oracle - Basic》的培训课程主要讲解了如何高效优化Oracle数据库中的SQL语句,提升系统性能。课程涵盖了四个核心步骤、两种执行模式以及开发和优化流程。 首先,四个核心步骤是识别、优化、...
"rewrite_x64"暗示这可能与IIS(Internet Information Services)的URL重写模块有关,因为IIS是微软的Web服务器,它有一个名为“URL Rewrite”的功能,用于管理网站的URL规则,优化SEO(搜索引擎优化)并提供更好的...
iis建网站使用的微软URL重写程序,辅助HTTP自动跳转HTTPS,还有一些其他的作用未发掘,我只用它的自动跳转功能
- **ENABLE QUERY REWRITE**:启用查询重写功能,允许Oracle自动使用物化视图代替基表查询,以提高性能。 - **DISABLE QUERY REWRITE**:禁用查询重写功能。 4. **物化视图日志(Materialized View Log)** - 如果...
Oracle 官方数据仓库指南,讲解透彻,知识权威,强烈推荐! Oracle® Database Data Warehousing Guide 10g Release 2 (10.2) Contents: Part I Concepts 1 Data Warehousing Concepts Part II Logical Design 2 ...
rewrite_amd64_zh-CN一些前端架构部署IIS调用不了api接口,使用到的工具
其核心特性在于支持 URL Rewrite,允许开发者在应用程序发布后仍能方便地调整和更新路由规则,这对于迭代更新和维护具有重要意义。 首先,URL 路由是现代移动应用架构中的关键组成部分,它负责根据特定的 URL 地址...
官方离线安装包,亲测可用
### 实战Oracle SQL调优 hint特性详解 #### 一、Oracle SQL Hint概述 **Hint**是Oracle数据库提供的一种特殊语法,允许用户通过在SQL语句中插入特定的指令来直接影响SQL执行的方式。Hint的设计初衷更多是为了帮助...
官方离线安装包,亲测可用
URL Rewrite组件2.0 护卫神优化版,剔除了一些对安装环境的检查,从而支持在Windows Server 2016中安装。 URL 重写模块 2.0 提供基于规则的重写机制,可在 Web 服务器处理请求的 URL 之前对其进行更改,以及在向 ...
Oracle 8i 开始有四种转换技术:视图合并(View Merging)、谓词推进(Predicate Pushing)、非嵌套子查询(Subquery Unnesting)和物化视图的查询重写(Query Rewrite with Materialized Views)。 CBO 优化器可以...