[20150715]Wrong Results.txt

本文详细分析了一次SQL查询中,使用物化视图导致输出结果异常的问题,通过深入理解查询过程和物化视图的工作原理,揭示了问题的根本原因并提供了解决方案。

[20150715]Wrong Results.txt

http://afatkulin.blogspot.com/2015/07/wrong-results.html

--重复测试:

SCOTT@test> @ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.3.0     Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production


create table t as select mod(level, 10) n, level m from dual connect by level <= 1000;
create materialized view mv_t enable query rewrite as select n, sum(m), grouping_id(n) gid from t group by rollup(n);

with v as ( select 20 n from dual) select distinct v.n from v, t where v.n=t.n(+);

SCOTT@test> with v as ( select 20 n from dual) select distinct v.n from v, t where v.n=t.n(+);
no rows selected


SCOTT@test> @dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  4nhtt9tat8cws, child number 0
-------------------------------------
with v as ( select 20 n from dual) select distinct v.n from v, t where v.n=t.n(+)
Plan hash value: 29516041
-----------------------------------------------------------------------------------------
| Id  | Operation                       | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                |      |        |       |     6 (100)|          |
|   1 |  SORT UNIQUE NOSORT             |      |      1 |    28 |     6  (17)| 00:00:01 |
|*  2 |   FILTER                        |      |        |       |            |          |
|   3 |    NESTED LOOPS OUTER           |      |      1 |    28 |     5   (0)| 00:00:01 |
|   4 |     TABLE ACCESS FULL           | DUAL |      1 |     2 |     2   (0)| 00:00:01 |
|*  5 |     MAT_VIEW REWRITE ACCESS FULL| MV_T |      1 |    26 |     3   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$9EB47AD3
   4 - SEL$9EB47AD3 / DUAL@SEL$1
   5 - SEL$9EB47AD3 / MV_T@SEL$EE2C87C0
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("MV_T"."GID"=0)
   5 - filter("MV_T"."N"=CASE  WHEN (ROWID IS NOT NULL) THEN 20 ELSE 20 END )

--仔细观察可以发现2 - filter("MV_T"."GID"=0),导致没有结果输出。

SCOTT@test> select * from MV_T;
         N     SUM(M)        GID
---------- ---------- ----------
         0      50500          0
         1      49600          0
         2      49700          0
         3      49800          0
         4      49900          0
         5      50000          0
         6      50100          0
         7      50200          0
         8      50300          0
         9      50400          0
               500500          1
11 rows selected.

--查询使用物化视图,导致结果出现异常。

SCOTT@test> drop materialized view mv_t;
Materialized view dropped.

SCOTT@test> with v as ( select 20 n from dual) select distinct v.n from v, t where v.n=t.n(+);
         N
----------
        20

function [T,Xinit] = tucker_sym(S,R,varargin) %TUCKER_SYM Symmetric Tucker approximation. % % T = TUCKER_SYM(S,R) computes the best rank-(R,R,...,R) approximation of % the symmetric tensor S, according to the specified dimension R. The % result returned in T is a ttensor (with all factors equal), i.e., % T = G x_1 X x_2 X ... x_N X where X is the optimal factor matrix and G % is the corresponding core. % % T = TUCKER_SYM(S,R,'param',value,...) specifies optional parameters and % values. Valid parameters and their default values are: % 'tol' - Tolerance on difference in X {1.0e-10} % 'maxiters' - Maximum number of iterations {1000} % 'init' - Initial guess [{'random'}|'nvecs'|cell array] % 'printitn' - Print fit every n iterations {1} % 'return' - First return argument is T or X [{'ttensor'},'matrix'] % % [T,X0] = TUCKER_SYM(...) also returns the initial guess. % % See also TUCKER_SYM. % % Reference: Phillip A. Regalia, Monotonically Convergent Algorithms for % Symmetric Tensor Approximation, Linear Algebra and its Applications % 438(2):875-890, 2013, http://dx.doi.org/10.1016/j.laa.2011.10.033. % %MATLAB Tensor Toolbox. %Copyright 2015, Sandia Corporation. % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. % http://www.sandia.gov/~tgkolda/TensorToolbox. % Copyright (2015) Sandia Corporation. Under the terms of Contract % DE-AC04-94AL85000, there is a non-exclusive license for use of this % work by or on behalf of the U.S. Government. Export of this data may % require a license from the United States Government. % The full license terms can be found in the file LICENSE.txt %% Input checking if ~issymmetric(S) error('S must be symmetric'); end if numel(R) ~= 1 error('R must be a scalar'); end N = ndims(S); D = size(S,1); %% Set algorithm parameters from input or by using defaults params = inputParser; params.addParameter('tol',1e-10,@isscalar); params.addParameter('maxiters',1000,@(x) isscalar(x) & x > 0); params.addParameter('init', 'random'); params.addParameter('printitn',1,@isscalar); params.addParameter('return','ttensor'); params.parse(varargin{:}); %% Copy from params object tol = params.Results.tol; maxiters = params.Results.maxiters; init = params.Results.init; printitn = params.Results.printitn; %% Error checking % Error checking on maxiters if maxiters < 0 error('OPTS.maxiters must be positive'); end %% Set up and error checking on initial guess for U. if isnumeric(init) Xinit = init; if ~isequal(size(Xinit),[D R]) error('OPTS.init is the wrong size'); end else if strcmp(init,'random') Xinit = rand(D,R); elseif strcmp(init,'nvecs') || strcmp(init,'eigs') % Compute an orthonormal basis for the dominant % Rn-dimensional left singular subspace of % X_(n) (1 <= n <= N). fprintf('Computing %d leading e-vectors.\n',R); Xinit = nvecs(S,1,R); else error('The selected initialization method is not supported'); end end %% Set up for iterations - we ensure that X is orthogonal X = Xinit; [X,~] = qr(X,0); % Roughly the same tolerance as is used by pinv svdtol = D^(N-1) * norm(S) * eps(1.0); if printitn > 0 fprintf('\nSymmetric Tucker:\n'); end %% Main Loop: Iterate until convergence Xcell = cell(N,1); for iter = 1:maxiters Xold = X; % For the remainder tensor [Xcell{:}] = deal(X); Rem = ttm(S, Xcell, -1, 't'); Rem = double(tenmat(Rem, 1)); % Form gradient % NOTE: We use the SVD directly rather than PINV, which could be % invoked by the line: X = 2*N*Rem*pinv(Rem)*X; [UU,SS,~] = svd(Rem,0); ii = find(diag(SS) > svdtol, 1, 'last'); UU = UU(:,1:ii); X = 2*N*UU*(UU'*X); % Update X [X,~] = qr(X,0); % Check for convergence fit = norm(X-Xold)/norm(X); % Check for convergence if (fit < tol) break; end % Print results if mod(iter,printitn)==0 fprintf(' Iter %2d: rel. change in X = %e\n', iter, fit); end end % Print final result if (printitn > 0) && (iter < maxiters) fprintf(' Iter %2d: rel. change in X = %e\n', iter, fit); end % Do they want just the matrix or the full tensor back? if strcmpi(params.Results.return,'matrix') T = X; else [Xcell{:}] = deal(X); core = ttm(S, Xcell, 1:N, 't'); T = ttensor(core, Xcell); end写一个调用函数的matlab的代码
最新发布
11-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值