外连接在两个带有顺序guid档位的表上

时间:2012-03-05 19:59:34

标签: sql-server sql-server-2008

我正在尝试在两个不相关的表上执行完全外连接。每个表都有一个location_id,最终形成主/外键关系(一旦我弄清楚这个性能问题)。执行外连接时,它只是时钟。对每个表执行的查询和触发器在不到一秒的时间内完成。

此表有21000条记录:

CREATE TABLE [dbo].[TBL_LOCATIONS](
    [OBJECTID] [int] NOT NULL,
    [Loc_Name] [nvarchar](100) NULL,
    [Location_ID] [uniqueidentifier] NULL,
    [SHAPE] [geometry] NULL,

 CONSTRAINT [R33_pk] PRIMARY KEY CLUSTERED 
(
    [OBJECTID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 75) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TBL_LOCATIONS]  WITH CHECK ADD  CONSTRAINT [g17_ck] CHECK  (([SHAPE].[STSrid]=(26917)))
GO
ALTER TABLE [dbo].[TBL_LOCATIONS] ADD  CONSTRAINT [DF_TBL_LOCATIONS_Location_ID]  DEFAULT (newsequentialid()) FOR [Location_ID]
GO

CREATE SPATIAL INDEX [S17_idx] ON [dbo].[TBL_LOCATIONS] 
(
    [SHAPE]
)USING  GEOMETRY_GRID 
WITH (
BOUNDING_BOX =(224827, 3923750, 323464, 3967780), GRIDS =(LEVEL_1 = HIGH,LEVEL_2 = HIGH,LEVEL_3 = HIGH,LEVEL_4 = HIGH), 
CELLS_PER_OBJECT = 16, PAD_INDEX  = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

CREATE UNIQUE NONCLUSTERED INDEX [UUID_OID_33] ON [dbo].[TBL_LOCATIONS] 
(
    [Location_ID] ASC,
    [OBJECTID] 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, FILLFACTOR = 75) ON [PRIMARY]
GO

此表有53000条记录

CREATE TABLE [dbo].[TBL_EVENTS](
    [OBJECTID] [int] NOT NULL,
    [Event_ID] [uniqueidentifier] NULL,
    [Location_ID] [uniqueidentifier] NULL,
 CONSTRAINT [PK_TBL_EVENTS] PRIMARY KEY CLUSTERED 
(
    [OBJECTID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TBL_EVENTS] ADD  CONSTRAINT [DF_TBL_EVENTS_Event_ID]  DEFAULT (newsequentialid()) FOR [Event_ID]
GO
ALTER TABLE [dbo].[TBL_EVENTS] ADD  CONSTRAINT [DF_TBL_EVENTS_Event_ID]  DEFAULT (newsequentialid()) FOR [Event_ID]
GO

CREATE UNIQUE NONCLUSTERED INDEX [R36_SDE_ROWID_UK] ON [dbo].[TBL_EVENTS] 
(
    [OBJECTID] 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, FILLFACTOR = 75) ON [PRIMARY]
GO

这是正在运行的查询....并且正在运行... 1小时且没有结果。

SELECT     
    TBL_LOCATIONS.Loc_Name, 
    TBL_LOCATIONS.Location_ID, 
    TBL_LOCATIONS.SHAPE, 
    TBL_EVENTS.Event_ID
FROM         
    TBL_EVENTS 
FULL OUTER JOIN
    TBL_LOCATIONS ON TBL_EVENTS.Location_ID = TBL_LOCATIONS.Location_ID

我已尝试在两个表上进行属性索引的每个排列,重建和重新组织它们,不会影响性能。应用程序强制使用ObjectID作为PK,orderGUID也是如此。我不认为这些是因素,因为这两个表在这个查询之外表现出色。 RAID 10/48 GB RAM上的SQL Server 2008 SP1 64BIT。

3 个答案:

答案 0 :(得分:2)

也许您需要考虑关闭事务记录,同时完成所有这些。

答案 1 :(得分:2)

当用于链接表的列中的数据是唯一的时,

FULL JOIN可以正常工作。

对于包含重复数据的行FULL JOIN,行为与CROSS JOIN相似,可能会导致性能问题。

因此,可能瓶颈来自LOCATION_ID列中的重复项。

答案 2 :(得分:1)

如果链接的字段值不是唯一的(位置),则查询大小可能接近相当大的数量。

在一个极端的例子中,如果位置只有" 1"在两个表中,总行数将接近交叉连接大小,大约1,113,000,000行(21,000 * 53,000)。查询此大小(超过十亿行)将需要很长时间才能运行。

编辑 - 更新错误的陈述,如评论中所述