简体   繁体   English

带有索引的派生表

[英]Derived table with an index

Please see the TSQL below: 请参见下面的TSQL:

DECLARE @TestTable table (reference int identity, 
                          TestField varchar(10), 
                          primary key (reference))
INSERT INTO @TestTable VALUES ('Ian')

select * from @TestTable as TestTable
INNER JOIN LiveTable on LiveTable.Reference=TestTable.Reference

Is it possible to create an index on @Test.TestField ? 是否可以在@Test.TestField上创建索引? The following webpage suggests it is not. 以下网页提示并非如此。 However, I read on another webpage that it is possible. 但是,我在另一个网页上看到了可能的情况。

I know I could create a physical table instead (for @TestTable). 我知道我可以代替创建一个物理表(用于@TestTable)。 However, I want to see if I can do this with a derived table first. 但是,我想看看是否可以先对派生表执行此操作。

You can create an index on a table variable as described in the top voted answer on this question: 您可以按照此问题的最高投票答案中所述在表变量上创建索引:

SQL Server : Creating an index on a table variable SQL Server:在表变量上创建索引

Sample syntax from that post: 该帖子的示例语法:

DECLARE @TEMPTABLE TABLE (
  [ID]   [INT] NOT NULL PRIMARY KEY,
  [Name] [NVARCHAR] (255) COLLATE DATABASE_DEFAULT NULL,
  UNIQUE NONCLUSTERED ([Name], [ID]) 
  ) 

Alternately, you may want to consider using a temp table, which will persist during the scope of the current operation, ie during execution of a stored procedure exactly like table variables. 或者,您可能要考虑使用临时表,该临时表将在当前操作的范围内(即在执行存储过程的过程中,与表变量完全一样)保持不变。 Temp tables will be structured and optimized just like regular tables, but they will be stored in tempDb , therefore they can be indexed in the same way as regular table. 临时表将像常规表一样进行结构化和优化,但是它们将存储在tempDb ,因此它们的索引方式与常规表相同。

Temp tables will generally offer better performance than table variables, but it's worth testing with your dataset. 临时表通常比表变量提供更好的性能,但是值得对数据集进行测试。

More in depth details can be found here: 更深入的细节可以在这里找到:

When should I use a table variable vs temporary table in sql server? 什么时候应该在SQL Server中使用表变量与临时表?

You can see a sample of creating a temp table with an index from: 您可以看到一个从以下目录创建带有索引的临时表的示例:

SQL Server Planet - Create Index on Temp Table SQL Server Planet-在临时表上创建索引

One of the most valuable assets of a temp table (#temp) is the ability to add either a clustered or non clustered index. 临时表(#temp)最有价值的资产之一就是能够添加聚簇索引或非聚簇索引。 Additionally, #temp tables allow for the auto-generated statistics to be created against them. 此外,#temp表允许针对它们创建自动生成的统计信息。 This can help the optimizer when determining cardinality. 这在确定基数时可以帮助优化器。 Below is an example of creating both a clustered and non-clustered index on a temp table. 下面是在临时表上创建聚集索引和非聚集索引的示例。

Sample code from site: 来自站点的示例代码:

CREATE TABLE #Users
(
    ID          INT IDENTITY(1,1),
    UserID      INT,
    UserName    VARCHAR(50)
)

INSERT INTO #Users
(
    UserID,
    UserName
)   
SELECT 
     UserID     = u.UserID
    ,UserName   = u.UserName
FROM dbo.Users u

CREATE CLUSTERED INDEX IDX_C_Users_UserID ON #Users(UserID)

CREATE INDEX IDX_Users_UserName ON #Users(UserName)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM