2011-05-04 32 views
4

我想加快查询,我想我对索引感到困惑。我将如何添加到此表中以及将添加哪个索引。该ID是唯一的,这是否会成为主要索引?如何在此临时表上创建索引?

CREATE TABLE #OSP 
    (
     [Id] UniqueIdentifier, 
     [YearMonth] int, 
     [Expenditure] decimal (7,2), 
     [Permit] decimal (7,2) 
    ); 
+0

如果您是通过Id查询的,那么是的,它会是主键/索引。如果你不使用它来查询,索引不会帮助... – forsvarir 2011-05-04 20:39:30

+0

那么是的,这是正确的主索引/键创建 – forsvarir 2011-05-04 20:43:05

回答

1

如果你加入id,那么创建一个索引将有所帮助。

,我认为这会工作:

CREATE TABLE #OSP 
    (
     [Id] UniqueIdentifier, 
     [YearMonth] int, 
     [Expenditure] decimal (7,2), 
     [Permit] decimal (7,2) 
    ); 


CREATE UNIQUE CLUSTERED INDEX [idx_id] ON #Osp ([Id] ASC) 
3

您可以在create table语句中指定的primary key

CREATE TABLE #OSP 
    (
     [Id] UniqueIdentifier primary key, 
     [YearMonth] int, 
     [Expenditure] decimal (7,2), 
     [Permit] decimal (7,2) 
    ); 
+2

+1这是首选,因为它使'#temp'表可以缓存[和可以减少'#temp表的创建次数](http://www.sqlservercentral.com/Forums/Topic510942-360-1.aspx) – 2011-05-04 20:56:15