楼主: alan_yang

探讨一下这个sql的执行!

[复制链接]
论坛徽章:
27
ITPUB元老
日期:2005-02-28 12:57:002012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:18
11#
发表于 2001-10-10 10:23 | 只看该作者
1. 用等价的exists语句代替

SELECT COUNT(*)
FROM TABLE_A A
WHERE NOT EXISTS
(    SELECT 1 FROM
     FROM TABLE_B B
     WHERE B.DH='XXX'
     AND SUBSTR(A.COL,1,7) = SUBSTR(B.COL, 1, 7)
)

注意在a.col, b.col可能为null时与原语句语义上的差别, 并考虑使用外连接.

2. 建立基于SUBSTR(A.COL, 7, 1), SUBSTR(B.COL,1,7)的function based index会有帮助

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
12#
发表于 2001-10-10 10:28 | 只看该作者
Not in is the slowest
use out join instead
Not exists is the fastest way to deal with your problem

SELECT COUNT(*)
FROM TABLE_A A
WHERE not exists (SELECT 'X'
                    FROM TABLE_B B
                   WHERE B.DH='XXX' and
                         SUBSTR(A.COL,1,7) = SUBSTR(B.COL,1,7) )

create index on table_aa and table_bb but also consider to deal with the substr function since it will prevend u to use the index

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
13#
发表于 2001-10-10 10:44 | 只看该作者
if you still need my help please run following sql and let me know the result and how long it takes

create table x as select substr(a.col,1,7) s ,count(*)  c from a group by s
select count(*), sum(c) from x

使用道具 举报

回复
论坛徽章:
3
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33会员2006贡献徽章
日期:2006-04-17 13:46:34
14#
发表于 2001-10-10 11:11 | 只看该作者
就本例来说,是
tidycc的minues快
还是oldwain的not exists快?

俺想知道 结果的说^^

当然 还可以比较 create function index前后的差别

使用道具 举报

回复
论坛徽章:
41
ITPUB元老
日期:2005-02-28 12:57:002011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:08ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
15#
 楼主| 发表于 2001-10-10 11:53 | 只看该作者
最初由 kezizi 发布
[B]if you still need my help please run following sql and let me know the result and how long it takes

create table x as select substr(a.col,1,7) s ,count(*)  c from a group by s
select count(*), sum(c) from x [/B]

The first sql takes about 1000 seconds
and the second sql return follow results:
COUNT(S)        SUM(C)
15              12126713

And kezizi,  are you have good advices?

BTW ,  谢谢各位大虾的鼎力相助!
如果我的语句还要统计其他项目,如统计其他列的和呢?
例如:
   SELECT COUNT(*),SUM(A.COL2)
      FROM TABLE_A    A
      WHERE  .......(省略)

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
16#
发表于 2001-10-10 12:17 | 只看该作者
count(*) = 15?!!, so small?

1. create index on table_a and/or table_b will not help because of the table size, plus it creates trouble later when data is updated
2. the different between 'not in' and 'exists' is a good point where I didn't pay attention. acturelly Internally in any kind of database, the 'exists' is use the same algorithm (in certain degree) to get data as the 'method 2' i mentioned before. ( use sort join or other join instead of inner loop join as in 'not in')

if 30 minutes is acceptable, then just do following (much better than several hours).  
by the way, There following sql can be optimized as well, but I don't believe any tuning will speed it up for another 10 times. because the following sql perform full table scan for 2 times (on two table) and perform a join on small tables whose time can be ignored. Mathematiclly there are no other significant algorithm better than this one

create table x as select substr(a.col,1,7) s ,count(*) c from a group by s
create table y as select distinct substr(b.col,1,7) s from b where b.dh = 'xxx'
select sum(c) from x where not exists (select 1 from x,y where x.s = y.s)

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
17#
发表于 2001-10-10 12:19 | 只看该作者
by the way the sum(c) is the result you want

使用道具 举报

回复
论坛徽章:
41
ITPUB元老
日期:2005-02-28 12:57:002011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:08ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
18#
 楼主| 发表于 2001-10-10 12:32 | 只看该作者
呵呵,你说15 太少?那是因为我是按substr(a.col1,1,7)
去分组统计的,如果不是那就和后面那个一千多万的数是一样的?

非常感谢!在您的英明指导下问题已经解决!

非常想跟你多学习!因为平时处理大数据量的查询统计工作
比较多。

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
19#
发表于 2001-10-10 12:36 | 只看该作者
I was wrong. create index on b(bh) and/or b(col,1,7) will help on step two. and save you a few minutes.

you can also save several minutes by tuning configuration, fragmentation and refine the sql to speed the query up, but not easy to speed it up for another 10 times.

使用道具 举报

回复
论坛徽章:
41
ITPUB元老
日期:2005-02-28 12:57:002011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:08ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
20#
 楼主| 发表于 2001-10-10 12:41 | 只看该作者
最初由 kezizi 发布
[B]I was wrong. create index on b(bh) and/or b(col,1,7) will help on step two. and save you a few minutes.

you can also save several minutes by tuning configuration, fragmentation and refine the sql to speed the query up, but not easy to speed it up for another 10 times. [/B]

    run the query  takes about 30 minutes !
    Thanks a lot!
    BTW, you may check you message!
    Can we take a secret  talk ?  
    Hope to make friends with you !
    And hope to learn  more from you about the sql tunning !

使用道具 举报

回复

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

本版积分规则 发表回复

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