moses3017 发表于 2010-5-23 23:36:49

Sybase,A left join B 与 B left join A 速度相差太大,并且执行计划也不一样,求解

Sybase数据库,A表数据量122806,B表数据量198161,通过N_STORAGE_ID字段做left join,此字段在A、B两张表中均不是主键,A、B表均有主键,没有索引。
1) SELECT count(*) FROM B b LEFT JOIN A a ON a.N_STORAGE_ID = b.N_STORAGE_ID 执行时间2s
2) SELECT count(*) FROM A a LEFT JOIN B b ON a.N_STORAGE_ID = b.N_STORAGE_ID 执行时间:半个小时搞不定
执行计划:
Sql代码
第一种情况 1)
QUERY PLAN FOR STATEMENT 1 (at line 1).




    STEP 1
      The type of query is EXECUTE.
      Executing a previously cached statement.


Parse and Compile Time 0.
SQL Server cpu time: 0 ms.


QUERY PLAN FOR STATEMENT 1 (at line 1).
    Executed in parallel by coordinating process and 6 worker processes.




    STEP 1
      The type of query is INSERT.
      The update mode is direct.
      Executed in parallel by coordinating process and 6 worker processes.
      Worktable1 created for REFORMATTING.


      FROM TABLE
            XZ_GLY_LDJLB
            a
      Nested iteration.
      Table Scan.
      Forward scan.
      Positioning at start of table.
      Executed in parallel with a 6-way hash scan.
      Using I/O Size 128 Kbytes for data pages.
      With LRU Buffer Replacement Strategy for data pages.
      TO TABLE
            Worktable1.


    STEP 2
      The type of query is SELECT.
      Evaluate Ungrouped COUNT AGGREGATE.
      Executed in parallel by coordinating process and 6 worker processes.


      FROM TABLE
            IDLE_DEVICE
            b
      Nested iteration.
      Table Scan.
      Forward scan.
      Positioning at start of table.
      Executed in parallel with a 6-way hash scan.
      Using I/O Size 128 Kbytes for data pages.
      With LRU Buffer Replacement Strategy for data pages.


      FROM TABLE
            Worktable1.
      Nested iteration.
      Using Clustered Index.
      Forward scan.
      Positioning by key.
      Using I/O Size 128 Kbytes for data pages.
      With LRU Buffer Replacement Strategy for data pages.


      Parallel result buffer merge.


    STEP 3
      The type of query is SELECT.
      Executed by coordinating process.


Parse and Compile Time 0.
SQL Server cpu time: 0 ms.

Execution Time 0.
SQL Server cpu time: 0 ms.SQL Server elapsed time: 0 ms.Sql代码第二种情况 2)
2)QUERY PLAN FOR STATEMENT 1 (at line 1).




    STEP 1
      The type of query is EXECUTE.
      Executing a newly cached statement.


Parse and Compile Time 0.
SQL Server cpu time: 0 ms.


QUERY PLAN FOR STATEMENT 1 (at line 1).
    Executed in parallel by coordinating process and 6 worker processes.




    STEP 1
      The type of query is SELECT.
      Evaluate Ungrouped COUNT AGGREGATE.
      Executed in parallel by coordinating process and 6 worker processes.


      FROM TABLE
            XZ_GLY_LDJLB
            a
      Nested iteration.
      Table Scan.
      Forward scan.
      Positioning at start of table.
      Executed in parallel with a 6-way hash scan.
      Using I/O Size 128 Kbytes for data pages.
      With LRU Buffer Replacement Strategy for data pages.


      FROM TABLE
            IDLE_DEVICE
            b
      Nested iteration.
      Table Scan.
      Forward scan.
      Positioning at start of table.
      Using I/O Size 128 Kbytes for data pages.
      With LRU Buffer Replacement Strategy for data pages.


      Parallel result buffer merge.


    STEP 2
      The type of query is SELECT.
      Executed by coordinating process.


Parse and Compile Time 0.
SQL Server cpu time: 0 ms.

Execution Time 0.
SQL Server cpu time: 0 ms.SQL Server elapsed time: 0 ms.
这让我苦思不得其解,大家帮忙啊

shakeone 发表于 2010-5-27 21:27:58

第一个plan:
step1: 为A建了个worktable,这个worktable有个clustered index
step2: nested loop join, inner table是这个work table的clustered index, outer table是B,所以这样的join是很快的,因为inner table很小,可以一直cache in memory
step3: 输出结果
第二个plan:
没有使用刚才建立的worktable的index,同样的nested loop join,inner table为B,outer table为A,所以性能很差,因为B不能被cache in memory,I/O很多次。
所以关键还是execution/optimizer做的太滥,给你个work arround,就是第一个执行完毕后,drop table XZ_GLY_LDJLB

shakeone 发表于 2010-5-27 21:28:55

你用的估计是15x,我都觉得15x的qp做得烂了
页: [1]
查看完整版本: Sybase,A left join B 与 B left join A 速度相差太大,并且执行计划也不一样,求解

免责声明:
本站所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:Admin@SybaseBbs.com