尽管索引MSSQL 2005,但速度非常慢

时间:2013-03-06 14:54:06

标签: sql-server tsql sql-server-2005 indexing

编辑: 我们正在移动服务器,我刚刚在新服务器上测试过它。那里没有性能问题。这似乎取决于功能不足,组织严密的服务器。

我们的一个过程昨晚突然变得非常缓慢。这个缓慢的步骤被追踪到一张桌子上的更新声明,该表格被认为没有太巧妙地编入索引。

所以今天我已经为所有相关表添加了索引,但我的表现仍然很糟糕。 我真的不明白 - 可能我还在做一些不那么聪明的事情。 欢迎任何建议。

更新如下:

update test_HDM_RTT
set patient_district_no = b.legacy_number
from test_HDM_RTT a
inner join PHD.migration.PatScope b
on a.patient_pas_no = b.TrustNumber

patscope是2474147行,test_hdm_rtt是815278

表的定义:

CREATE TABLE [dbo].[test_HDM_RTT](
    [pk_episode_id] [int] NULL,
    [pk_event_id] [int] NOT NULL,
    [activity_date] [datetime] NULL,
    [activity_datetime] [datetime] NULL,
    [activity_subtype1] [nvarchar](50) NULL,
    [activity_subtype1_code] [nvarchar](50) NULL,
    [activity_subtype2] [nvarchar](50) NULL,
    [activity_subtype2_code] [nvarchar](50) NULL,
    [activity_type] [nvarchar](50) NULL,
    [activity_type_code] [nvarchar](50) NULL,
    [clock_start_date] [datetime] NULL,
    [clock_stop_date] [datetime] NULL,
    [dir_code] [nvarchar](10) NULL,
    [div_code] [nvarchar](10) NULL,
    [episode_id_ext] [nvarchar](50) NULL,
    [episode_id_appt] [nvarchar](50) NULL,
    [episode_id_ref] [nvarchar](50) NULL,
    [episode_id_ref_medway] [nvarchar](50) NULL,
    [episode_id_wl] [nvarchar](50) NULL,
    [erod] [datetime] NULL,
    [nhs_number] [nvarchar](20) NULL,
    [patient_id] [int] NULL,
    [patient_district_no] [nvarchar](20) NULL,
    [patient_pas_no] [nvarchar](50) NULL,
    [pathway_id] [nvarchar](50) NULL,
    [pct_code] [nvarchar](10) NULL,
    [ref_source_code] [nvarchar](10) NULL,
    [rtt_episode_id] [nvarchar](50) NULL,
    [rtt_outcome_code] [nvarchar](50) NULL,
    [rtt_outcome_desc] [nvarchar](50) NULL,
    [rtt_start_date] [datetime] NULL,
    [rtt_start_ind] [nvarchar](10) NULL,
    [rtt_stop_date] [datetime] NULL,
    [site_code] [nvarchar](10) NULL,
    [spec_natcode] [nvarchar](10) NULL,
    [spec_pascode] [nvarchar](10) NULL,
    [transfer_text] [nvarchar](100) NULL,
    [op_rtt_count] [int] NULL,
    [app_rec_date] [datetime] NULL,
    [cons_code] [varchar](10) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


USE [PIP]
/****** Object:  Index [pk_event_id_clustered]    Script Date: 03/06/2013 14:46:52 ******/
CREATE CLUSTERED INDEX [pk_event_id_clustered] ON [dbo].[test_HDM_RTT] 
(
    [pk_event_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


USE [PIP]
/****** Object:  Index [idx_episode_id_appt]    Script Date: 03/06/2013 14:46:52 ******/
CREATE NONCLUSTERED INDEX [idx_episode_id_appt] ON [dbo].[test_HDM_RTT] 
(
    [episode_id_appt] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


USE [PIP]
/****** Object:  Index [idx_episode_id_ref]    Script Date: 03/06/2013 14:46:52 ******/
CREATE NONCLUSTERED INDEX [idx_episode_id_ref] ON [dbo].[test_HDM_RTT] 
(
    [episode_id_ref] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


USE [PIP]
/****** Object:  Index [idx_episode_id_wl]    Script Date: 03/06/2013 14:46:52 ******/
CREATE NONCLUSTERED INDEX [idx_episode_id_wl] ON [dbo].[test_HDM_RTT] 
(
    [episode_id_wl] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


USE [PIP]
/****** Object:  Index [patient_pas_no]    Script Date: 03/06/2013 14:46:52 ******/
CREATE NONCLUSTERED INDEX [patient_pas_no] ON [dbo].[test_HDM_RTT] 
(
    [patient_pas_no] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

USE [PHD]
GO

/****** Object:  Table [migration].[PatScope]    Script Date: 03/06/2013 14:47:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [migration].[PatScope](
    [RID] [varchar](7) NOT NULL,
    [Number] [varchar](17) NOT NULL,
    [TrustNumber] [varchar](10) NULL,
    [NumberType] [nvarchar](10) NULL,
    [legacy_number] [varchar](10) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


USE [PHD]
/****** Object:  Index [TrustNoClustered]    Script Date: 03/06/2013 14:47:57 ******/
CREATE CLUSTERED INDEX [TrustNoClustered] ON [migration].[PatScope] 
(
    [TrustNumber] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


USE [PHD]
/****** Object:  Index [TrustNo]    Script Date: 03/06/2013 14:47:57 ******/
CREATE NONCLUSTERED INDEX [TrustNo] ON [migration].[PatScope] 
(
    [TrustNumber] ASC,
    [Number] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


USE [PHD]
/****** Object:  Index [TrustNumber_legacy_lookup]    Script Date: 03/06/2013 14:47:57 ******/
CREATE UNIQUE NONCLUSTERED INDEX [TrustNumber_legacy_lookup] ON [migration].[PatScope] 
(
    [TrustNumber] ASC,
    [legacy_number] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

2 个答案:

答案 0 :(得分:3)

就个人而言,我只会更新该值是否与现有值不同。这应该加快查询速度

update test_HDM_RTT
set patient_district_no = b.legacy_number
from test_HDM_RTT a
 inner join PHD.migration.PatScope b
 on a.patient_pas_no = b.TrustNumber
where a.patient_district_no <> b.legacy_number

我还会检查您的查询可能使用错误索引的EXPLAIN结果(ctrl + l)。

答案 1 :(得分:0)

显示执行计划&amp;看看该计划是否使用索引。如果没有,您可以强制它使用特定索引:

SET ANSI_NULLS OFF
GO

update test_HDM_RTT
set patient_district_no = b.legacy_number
from test_HDM_RTT a
inner join PHD.migration.PatScope b WITH (INDEX(TrustNumber_legacy_lookup))
on a.patient_pas_no = b.TrustNumber
where a.patient_district_no <> b.legacy_number

您需要查看是否需要在test_HDM_RTT强制使用索引,因为看起来它已经开始对TrustNumber_legacy_lookup进行索引扫描以获取其数据。