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

[原创] 关于等值连接和非等值连接效率解析。。。求解

[复制链接]
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
11#
发表于 2016-11-18 20:11 | 只看该作者
like能用上索引,instr用不上

create table t_user_info(u_mobile varchar2(12));

create table t_mobile_segment(m_segement varchar2(12),m_carrier varchar2(2));

insert into t_user_info select '1'||x from
(select substr(dbms_random.random,1,10)x from dual connect by level<=1e5)
where length(x)=10;



insert into t_mobile_segment select distinct '1'||x ,1 from
(select substr(dbms_random.random,1,6)x from dual connect by level<=1e5)
where length(x)=6;

explain plan for select count(1) from t_user_info t1,t_mobile_segment t2 where instr(t1.u_mobile,t2.m_segement)=1;

explain plan for select count(1) from t_user_info t1,t_mobile_segment t2 where substr(t1.u_mobile,1,7)=t2.m_segement;

explain plan for select count(1) from t_user_info t1,t_mobile_segment t2 where t1.u_mobile like t2.m_segement||'%';

SQL> create index tui on t_user_info(u_mobile);

索引已创建。

已用时间:  00: 00: 00.20
SQL> explain plan for select count(1) from t_user_info t1,t_mobile_segment t2 where instr(t1.u_mobile,t2.m_segement)=1;

已解释。

已用时间:  00: 00: 00.00
SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
Plan hash value: 75945502

----------------------------------------------------------------------------------------
| Id  | Operation           | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                  |     1 |    16 |  5004K  (1)| 16:40:54 |
|   1 |  SORT AGGREGATE     |                  |     1 |    16 |            |          |
|   2 |   NESTED LOOPS      |                  |    63M|   964M|  5004K  (1)| 16:40:54 |
|   3 |    TABLE ACCESS FULL| T_MOBILE_SEGMENT | 74982 |   585K|    69   (2)| 00:00:01 |
|*  4 |    TABLE ACCESS FULL| T_USER_INFO      |   843 |  6744 |    67   (2)| 00:00:01 |
----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - filter(INSTR("T1"."U_MOBILE","T2"."M_SEGEMENT")=1)

Note
-----
   - dynamic sampling used for this statement (level=2)

已选择20行。

已用时间:  00: 00: 00.07
SQL> explain plan for select count(1) from t_user_info t1,t_mobile_segment t2 where t1.u_mobile like t2.m_segement||'%';

已解释。

已用时间:  00: 00: 00.04
SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
Plan hash value: 178076189

----------------------------------------------------------------------------------------
| Id  | Operation           | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                  |     1 |    16 |   225K  (1)| 00:45:07 |
|   1 |  SORT AGGREGATE     |                  |     1 |    16 |            |          |
|   2 |   NESTED LOOPS      |                  |   316M|  4824M|   225K  (1)| 00:45:07 |
|   3 |    TABLE ACCESS FULL| T_MOBILE_SEGMENT | 74982 |   585K|    69   (2)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN | TUI              |  4217 | 33736 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("T1"."U_MOBILE" LIKE "T2"."M_SEGEMENT"||'%')
       filter("T1"."U_MOBILE" LIKE "T2"."M_SEGEMENT"||'%')

Note
-----
   - dynamic sampling used for this statement (level=2)

已选择21行。

已用时间:  00: 00: 00.07
SQL> select count(1) from t_user_info t1,t_mobile_segment t2 where t1.u_mobile like t2.m_segement||'%';

  COUNT(1)
----------
     37176

已用时间:  00: 00: 00.48

SQL> select count(1) from t_user_info t1,t_mobile_segment t2 where substr(t1.u_mobile,1,7)=t2.m_segement;

  COUNT(1)
----------
     37176

已用时间:  00: 00: 00.04

使用道具 举报

回复
论坛徽章:
23
兰博基尼
日期:2015-04-20 18:33:262014年世界杯参赛球队: 瑞士
日期:2015-04-20 18:33:262014年世界杯参赛球队: 洪都拉斯
日期:2015-04-20 18:33:262014年世界杯参赛球队: 阿尔及利亚
日期:2015-04-20 18:33:26马上有钱
日期:2015-04-20 18:33:26马上有对象
日期:2015-04-20 18:33:26沸羊羊
日期:2015-04-20 18:33:26慢羊羊
日期:2015-04-20 18:33:26喜羊羊
日期:2015-04-21 10:00:44itpub13周年纪念徽章
日期:2015-05-07 14:11:42
12#
 楼主| 发表于 2016-11-19 09:23 | 只看该作者
〇〇 发表于 2016-11-18 20:11
like能用上索引,instr用不上

create table t_user_info(u_mobile varchar2(12));

多谢指教,下面这个造数据的语句很受用
  1. insert into t_user_info select '1'||x from
  2. (select substr(dbms_random.random,1,10)x from dual connect by level<=1e5)
  3. where length(x)=10;
复制代码

使用道具 举报

回复

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

本版积分规则 发表回复

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