简体   繁体   English

在视图或表上显示索引?

[英]INDEX on View or Table?

Can anyone help me with this one. 谁能帮我这个忙。

Where should I put the index for this one since it is a view? 既然是视图,我应该在哪里放置该索引? Does indexing the sub query (table) help optimize this, or should I just put an index on the view instead? 为子查询(表)建立索引是否有助于优化此操作,还是应该只在视图上放置索引?

Thank you very much. 非常感谢你。

  1. INDEX on vw_fact_test ? vw_fact_test INDEX吗?
  2. INDEX on A1LE_Project ? A1LE_Project INDEX吗?

Code: 码:

ALTER VIEW [dbo].[vw_fact_test]
AS
  SELECT     
      A.Column1
      A.Column2, 
      A.Column3, 
      CASE 
         WHEN Column4 IS NULL OR Column4 = '' OR Column4 = 'NULL' 
           THEN 'unknown' 
           ELSE Column4 
      END AS Column4, 
      CASE 
        WHEN Column5 IS NULL OR Column5 = '' OR Column5 = 'NULL' 
          THEN 'unknown' 
          ELSE Column5 
      END AS Column5, 
      A.Column6, 
      dbo.M3Ofc_Lookups.Column7
FROM         
    (SELECT     
        Column1, 
        Column2, 
        Column3, 
        Column4, 
        Column5, 
        Column6, 
    FROM 
        dbo.A1LE_Project) AS A 
INNER JOIN 
    dbo.M3Ofc_Lookups ON A.[Office Code] = dbo.M3Ofc_Lookups.[Office Code] 
WHERE 
    (A.Column2 <> 0) OR
    (A.Column3 <> 0) OR
    (A.Column4 <> 0)

Firstly you can simplify your select statement, which makes it easier to read and think about the indexing. 首先,您可以简化您的select语句,这使阅读和考虑索引变得更加容易。 You don't need a subquery. 您不需要子查询。

SELECT A.Column1, A.Column2, A.Column3, 
        CASE 
            WHEN Column4 IS NULL OR Column4 = '' THEN 'unknown' 
            ELSE Column4 
        END AS Column4, 
        CASE 
            WHEN Column5 IS NULL OR Column5 = '' THEN 'unknown' 
            ELSE Column5 
        END AS Column5, 
        A.Column6, B.Column7
    FROM dbo.A1LE_Project AS A 
        INNER JOIN dbo.M3Ofc_Lookups AS B
            ON A.[Office Code] = B.[Office Code] 
    WHERE (A.Column2 <> 0) OR (A.Column3 <> 0) OR (A.Column4 <> 0);

You need to consider the sizes of the tables and where you want to improve the performance. 您需要考虑表的大小以及要提高性能的位置。 You also need to examine the execution plan. 您还需要检查执行计划。

If table B is a lookup table, it is likely to be fairly small. 如果表B是查找表,则它可能很小。 So the query processor could do a table scan with little cost. 因此查询处理器可以以很少的成本进行表扫描。 If it is sizeable, making [Office Code] the clustered primary key would help. 如果相当大,则使[Office Code]成为群集的主键将有所帮助。 Or possibly putting an index on [Office Code] and Column7. 或者可能在[Office Code]和Column7上添加索引。

How many records are in table A, and what are the types and sizes of columns? 表A中有多少条记录,列的类型和大小是什么?

If table A has a large number of records, and the select statement will yield a small proportion, then putting indexes on Column2, Column3, and Column4 might help. 如果表A具有大量记录,并且select语句将产生很小的比例,则将索引放在Column2,Column3和Column4上可能会有所帮助。 The query processor would be able to quickly select early on just the records it needs. 查询处理器将能够迅速根据其需要的记录进行早期选择。 You would need to test this, and see the effect on the execution plan. 您将需要对此进行测试,并查看其对执行计划的影响。

If columns 1 to 6 are small, and the table contains a lot of other columns, then adding a covering index could help. 如果第1列到第6列很小,并且该表包含许多其他列,那么添加覆盖索引可能会有所帮助。

Before adding any indexes you need to consider how the database is being updated and the impact on performance. 在添加任何索引之前,您需要考虑如何更新数据库以及对性能的影响。 Is the database only being used for reporting, or is it being updated as well? 该数据库仅用于报告,还是正在更新?

Adding an index on the view itself could have a severe impact on update performance. 在视图本身上添加索引可能会对更新性能产生严重影响。 It's almost like creating an additional table which is kept automatically in step. 这几乎就像创建一个额外的表,该表会自动保持在步骤中。 See SQL Server Books Online > Create Indexed Views. 请参阅SQL Server联机丛书>创建索引视图。 "The first index created on a view must be a unique clustered index. After the unique clustered index has been created, you can create additional nonclustered indexes. Creating a unique clustered index on a view improves query performance because the view is stored in the database in the same way a table with a clustered index is stored." “在视图上创建的第一个索引必须是唯一的聚集索引。创建唯一的聚集索引后,您可以创建其他非聚集索引。在视图上创建唯一的聚集索引可以提高查询性能,因为视图存储在数据库中以相同的方式存储具有聚簇索引的表。”

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

相关问题 表索引和视图索引有什么区别? - What is the difference between an table index and a view index? 命中索引表的视图中的其他索引 - Additional index in a view which hits an indexed table 表和视图索引比较和 CTE 迭代 - Table and view index comparison and CTE iteration 从基础视图制作数据透视表使用索引 - Making pivot use index from underlying table of view 具有在单个SP中创建表/视图/索引和选择的存储过程范围 - Stored Proc scope with Create table/view/index & Select in single SP 无法在视图'View_Table_Name'上创建索引,因为视图不是模式绑定的 - Cannot create index on view 'View_Table_Name' because the view is not schema bound 具有唯一聚集索引和索引视图的表以相同的方式存储。 那么,索引视图的好处是什么? - Table with Unique Clustered Index and an Indexed View are stored in same way. What is the benefit of Indexed View then? 繁忙的表,索引或不索引 - Busy table, to index or not to index 如果我在sql server中为表创建索引,那么看起来索引表的视图会受到影响吗? - If I create an index to a table in sql server, a view which looks indexed table will be affected? SQL Server:无法在视图上创建索引,因为它引用了派生表。 - SQL Server : cannot create index on view because it references derived table.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM