1

这是我的查询,大约需要 1.5 秒。我可以降低这个吗?

SELECT * 
FROM 
    (SELECT 
        ROW_NUMBER() OVER (ORDER BY NAME asc) peta_rn, 
        peta_query.* 
     FROM 
         (SELECT 
              BOOK, PAGETRIMMED, NAME, TYPE, PDF 
         FROM 
              CCWiseDocumentNames2 cdn
         INNER JOIN 
              CCWiseInstr2 cwi ON cwi.ID = cdn.ID) as peta_query) peta_paged 
WHERE 
    peta_rn > 1331900 AND peta_rn <= 1331950

这些是我的表结构:

CREATE TABLE [dbo].[CCWiseDocumentNames2](
    [ID] [int] NULL,
    [BK_PG] [varchar](50) NULL,
    [NAME] [varchar](100) NULL,
    [OTHERNAM] [varchar](100) NULL,
    [TYPE] [varchar](50) NULL,
    [INDEXNAME] [varchar](50) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[CCWiseInstr2](
    [ID] [int] NULL,
    [BK_PG] [varchar](50) NULL,
    [DATE] [datetime] NULL,
    [ITYPE] [varchar](50) NULL,
    [BOOK] [int] NULL,
    [PAGE] [varchar](50) NULL,
    [NOBP] [varchar](50) NULL,
    [DESC] [varchar](240) NULL,
    [TIF] [varchar](50) NULL,
    [INDEXNAME] [varchar](50) NULL,
    [CONFIRM] [varchar](50) NULL,
    [PDF] [varchar](50) NULL,
    [PAGETRIMMED] [varchar](10) NULL,
    [PageINT] [int] NULL,
    [PageCHAR] [varchar](2) NULL,
    [IdAuto] [int] NOT NULL
) ON [PRIMARY]

这是我的执行计划:

在此处输入图像描述

如您所见,它是 97% 的聚集索引搜索和 3% 的索引扫描。有什么办法可以进一步改进这个查询?

4

2 回答 2

4

您不能即时将行号添加到超过一百万行,并期望 where 子句会立即使用新生成的行号识别这些行。

于 2013-09-24T12:22:15.593 回答
0

因为我没有那么大的数据量,只能提供一些选项供大家参考:

将聚集索引专用于 Name 列(ID 除外)在获得 row_number over name 后进行连接。将 CCWiseInstr2 中的三列包含到 ID 列的非聚集索引中。这可以节省一些硬盘主轴的运动。只有通过大量数据才能观察到性能增益。

CREATE NONCLUSTERED INDEX  [idx2_ID_include] ON [dbo].[CCWiseInstr2] ([ID] ASC) INCLUDE ( [BOOK], [PDF], [PAGETRIMMED]) 
GO

With a as (
    Select * 
    from (  SELECT  ROW_NUMBER() OVER (ORDER BY NAME asc) as  peta_rn, ID, 
                    type 
            from    CCWiseDocumentNames2) as Temp 
            where   peta_rn > 1331900 AND peta_rn <= 1331950
) 
select  a.peta_rn, 
        a.type, 
        b.book, 
        b.PAGETRIMMED, 
        b.PDF
from    a 
join    CCWiseInstr2 as b on a.id = b.id
于 2013-09-24T13:17:11.197 回答