简体   繁体   English

如何通过索引来提高我的查询性能

[英]How to improve my query performance by indexing

i just want to know how will i index the this table for optimal performance? 我只是想知道如何将此表索引以获得最佳性能? This will potentially hold around 20M rows. 这可能会容纳大约20M行。

  CREATE TABLE [dbo].[Table1]( 
  [ID] [bigint] NOT NULL,
  [Col1] [varchar](100) NULL,
  [Col2] [varchar](100) NULL,
  [Description] [varchar](100) NULL
  ) ON [PRIMARY]

Basically, this table will be queried ONLY in this manner. 基本上,这个表格只会以这种方式查询。

    SELECT ID FROM Table1
    WHERE Col1 = 'exactVal1' AND Col2 = 'exactVal2' AND [Description] = 'exactDesc'

This is what i did: 这就是我做的:

    CREATE NONCLUSTERED INDEX IX_ID
    ON Table1(ID)
    GO 

    CREATE NONCLUSTERED INDEX IX_Col1
    ON Table1(Col1)
    GO 

    CREATE NONCLUSTERED INDEX IX_Col2
    ON Table1(Col2)
    GO 

    CREATE NONCLUSTERED INDEX IX_ValueDescription
    ON Table1(ValueDescription)
    GO 

Am i right to index all these columns? 我是否正确索引所有这些列? Not really that confident yet. 还没那么自信。 Just new to SQL stuff, please let me know if im on the right track. 刚接触SQL的东西,如果我在正确的轨道上,请告诉我。

Again, a lot of data will be put on this table. 同样,很多数据都会放在这张桌子上。 Unfortunately, i cannot test the performance yet since there are no available data. 不幸的是,由于没有可用的数据,我无法测试性能。 But I will soon be generating some dummy data to test the performance. 但我很快就会生成一些虚拟数据来测试性能。 But it would be great if there is already another option(suggestion) available that i can compare the results with. 但如果已经有另外一个选项(建议)我可以将结果与之比较,那将会很棒。

Thanks, jack 谢谢,杰克

I would combine these indexes into one index, instead of having three separate indexes. 我会将这些索引合并到一个索引中,而不是具有三个单独的索引。 For example: 例如:

CREATE INDEX ix_cols ON dbo.Table1 (Col1, Col2, Description)

If this combination of columns is unique within the table, then you should add the UNIQUE keyword to make the index unique. 如果列的这种组合在表中是唯一的,那么您应该添加UNIQUE关键字以使索引唯一。 This is for performance reasons, but, also, more importantly, to enforce uniqueness. 这是出于性能原因,但更重要的是,要强制执行唯一性。 It may also be created as a primary key if that is appropriate. 如果合适,也可以将其创建为主键。

Placing all of the columns into one index will give better performance because it will not be necessary for SQL Server to use multiple passes to find the row you are seeking. 将所有列放入一个索引将提供更好的性能,因为SQL Server不需要使用多个传递来查找您正在寻找的行。

Try this - 尝试这个 -

CREATE TABLE dbo.Table1 
(
      ID BIGINT NOT NULL
    , Col1 VARCHAR(100) NULL
    , Col2 VARCHAR(100) NULL
    , [Description] VARCHAR(100) NULL
)
GO

CREATE CLUSTERED INDEX IX_Table1 ON dbo.Table1 
(
      Col1
    , Col2
    , [Description]
)

Or this - 或这个 -

CREATE TABLE dbo.Table1 
(
      ID BIGINT PRIMARY KEY NOT NULL
    , Col1 VARCHAR(100) NULL
    , Col2 VARCHAR(100) NULL
    , [Description] VARCHAR(100) NULL
)
GO

CREATE UNIQUE NONCLUSTERED INDEX IX_Table1 ON dbo.Table1 
(
      Col1
    , Col2
    , [Description]
)

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

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