我想加快查詢,我想我對索引感到困惑。我將如何添加到此表中以及將添加哪個索引。該ID是唯一的,這是否會成爲主要索引?如何在此臨時表上創建索引?
CREATE TABLE #OSP
(
[Id] UniqueIdentifier,
[YearMonth] int,
[Expenditure] decimal (7,2),
[Permit] decimal (7,2)
);
我想加快查詢,我想我對索引感到困惑。我將如何添加到此表中以及將添加哪個索引。該ID是唯一的,這是否會成爲主要索引?如何在此臨時表上創建索引?
CREATE TABLE #OSP
(
[Id] UniqueIdentifier,
[YearMonth] int,
[Expenditure] decimal (7,2),
[Permit] decimal (7,2)
);
如果你加入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)
您可以在create table語句中指定的primary key。
CREATE TABLE #OSP
(
[Id] UniqueIdentifier primary key,
[YearMonth] int,
[Expenditure] decimal (7,2),
[Permit] decimal (7,2)
);
+1這是首選,因爲它使'#temp'表可以緩存[和可以減少'#temp表的創建次數](http://www.sqlservercentral.com/Forums/Topic510942-360-1.aspx) – 2011-05-04 20:56:15
如果您是通過Id查詢的,那麼是的,它會是主鍵/索引。如果你不使用它來查詢,索引不會幫助... – forsvarir 2011-05-04 20:39:30
那麼是的,這是正確的主索引/鍵創建 – forsvarir 2011-05-04 20:43:05