12
返回列表 发新帖
楼主: Kenniu

[原创] 一个隐形的笛卡尔乘积的SQL语句

[复制链接]
论坛徽章:
46
托尼托尼·乔巴
日期:2017-01-03 11:47:42喜羊羊
日期:2015-03-10 14:01:432015年新春福章
日期:2015-03-06 11:57:31沸羊羊
日期:2015-03-04 14:43:43马上有房
日期:2014-12-29 13:45:35马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11夏利
日期:2014-01-28 09:42:56雪铁龙
日期:2013-10-09 13:33:15秀才
日期:2016-01-21 13:37:04
11#
 楼主| 发表于 2016-6-14 12:42 | 只看该作者
bell6248 发表于 2016-6-14 11:56
你把2中不同语句执行的方式的执行计划贴一下, 比较一下即可

XD,查询计划很漂亮呀....

使用道具 举报

回复
论坛徽章:
46
托尼托尼·乔巴
日期:2017-01-03 11:47:42喜羊羊
日期:2015-03-10 14:01:432015年新春福章
日期:2015-03-06 11:57:31沸羊羊
日期:2015-03-04 14:43:43马上有房
日期:2014-12-29 13:45:35马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11夏利
日期:2014-01-28 09:42:56雪铁龙
日期:2013-10-09 13:33:15秀才
日期:2016-01-21 13:37:04
12#
 楼主| 发表于 2016-6-14 12:42 | 只看该作者
zergduan 发表于 2016-6-14 11:56
你确定你在sqlplus中运行的时候使用的T0 T1过滤条件和存储过程中的过滤条件一样?(变量)

完全一样....

使用道具 举报

回复
论坛徽章:
46
托尼托尼·乔巴
日期:2017-01-03 11:47:42喜羊羊
日期:2015-03-10 14:01:432015年新春福章
日期:2015-03-06 11:57:31沸羊羊
日期:2015-03-04 14:43:43马上有房
日期:2014-12-29 13:45:35马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11夏利
日期:2014-01-28 09:42:56雪铁龙
日期:2013-10-09 13:33:15秀才
日期:2016-01-21 13:37:04
13#
 楼主| 发表于 2016-6-14 14:16 | 只看该作者
我不是讲一个问题,我是在讲应该避开的一个陷阱...有人会踩雷吗?

使用道具 举报

回复
论坛徽章:
47
蒙奇·D·路飞
日期:2017-03-27 08:04:23马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11一汽
日期:2013-09-01 20:46:27复活蛋
日期:2013-03-13 07:55:232013年新春福章
日期:2013-02-25 14:51:24ITPUB 11周年纪念徽章
日期:2012-10-09 18:03:322012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20
14#
发表于 2016-6-15 00:01 | 只看该作者
If the actual SQL cannot be posted here, change or mask sensitive table or column names and values in the SQL and then post it.

The example SQL you posted here will clearly use a cartesian join:

Test in 11.2.0.3 (not sure about your version):

SQL> create table t_test (a int, b varchar2(10), c int);

Table created.

SQL> exec dbms_stats.gather_table_stats(user, 'T_TEST')

PL/SQL procedure successfully completed.

SQL> explain plan for
  2  Select * from T_TEST T0,T_TEST T1
  3  where  T0.b='' and T1.b='';

Explained.

SQL> @?/rdbms/admin/utlxpls

Plan Table
------------------------------------------------------------------------------------------
| Operation                 |  Name              |  Rows | Bytes|  Cost  | Pstart| Pstop |
------------------------------------------------------------------------------------------
| SELECT STATEMENT          |                    |     1 |   66 |      0 |       |       |
|  FILTER                   |                    |       |      |        |       |       |
|   MERGE JOIN CARTESIAN    |                    |     1 |   66 |      4 |       |       |
|    TABLE ACCESS FULL      |T_TEST              |     1 |   33 |      2 |       |       |
|    BUFFER SORT            |                    |     1 |   33 |      2 |       |       |
|     TABLE ACCESS FULL     |T_TEST              |     1 |   33 |      2 |       |       |
------------------------------------------------------------------------------------------

If a SQL takes more than 5 seconds to run, you may find its actual runtime stats with

set pages 1000 linesize 1000 long 10000 longc 1000
select dbms_sqltune.report_sql_monitor('&sql_id') from dual;

Please post the output here.

Another possibility is that the database completes execution just as fast as in Sqlplus. But a lot of time is wasted somewhere else, including the app server.

使用道具 举报

回复
论坛徽章:
6
2014年新春福章
日期:2014-02-18 16:49:31马上有钱
日期:2014-02-18 16:49:31优秀写手
日期:2014-02-21 06:00:13马上有车
日期:2014-10-09 10:14:53技术图书徽章
日期:2017-07-12 17:18:46妮可·罗宾
日期:2017-08-10 12:13:56
15#
发表于 2016-9-4 11:20 | 只看该作者
去掉符号*,避免笛卡尔积

使用道具 举报

回复
论坛徽章:
46
托尼托尼·乔巴
日期:2017-01-03 11:47:42喜羊羊
日期:2015-03-10 14:01:432015年新春福章
日期:2015-03-06 11:57:31沸羊羊
日期:2015-03-04 14:43:43马上有房
日期:2014-12-29 13:45:35马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11夏利
日期:2014-01-28 09:42:56雪铁龙
日期:2013-10-09 13:33:15秀才
日期:2016-01-21 13:37:04
16#
 楼主| 发表于 2016-9-5 10:15 | 只看该作者
260600sz 发表于 2016-9-4 11:20
去掉符号*,避免笛卡尔积

这个想法好...

使用道具 举报

回复
论坛徽章:
47
蒙奇·D·路飞
日期:2017-03-27 08:04:23马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11一汽
日期:2013-09-01 20:46:27复活蛋
日期:2013-03-13 07:55:232013年新春福章
日期:2013-02-25 14:51:24ITPUB 11周年纪念徽章
日期:2012-10-09 18:03:322012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20
17#
发表于 2016-9-7 00:38 | 只看该作者
> 去掉符号*,避免笛卡尔积

Do you mean changing
select * from ...
to something like
select 1 from ...?

That won't make any difference in the execution plan in this case. Try my code in message #14. You'll still see "MERGE JOIN CARTESIAN" in the plan.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表