简体   繁体   中英

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.

  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.

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. 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.

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]
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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