How to optimize this query?

  • Hi All,

    I have a simple query as below

    Select A,B,C from Table1

    How to create a good index on that ?

    At the moment , the table has 2 indexes :

    1.

    CREATE NONCLUSTERED INDEX [IX_A] ON [dbo].[Table1]

    (

    A ASC,

    B ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    GO

    2.

    ALTER TABLE [dbo].[Table1 ] ADD CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED

    (

    [Id] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    GO

    The execution plan shows :

    100% of Clustered iNdex scan

    and I modified the nonclusteredindex become :

    CREATE NONCLUSTERED INDEX [IX_Table1] ON [dbo].[Table1]

    (

    A ASC

    )

    INCLUDE ( ,[C]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    GO

    the result in the execution plan becomes :

    100% of Index Scan ( using the new nonclustered index that i modified)

    My question , is it better in term of performance ? or any idea so it can use INDEX SEEK?

    Thanks heaps

  • Your query mandates a table/index scan. There is no filter/predicate/where clause on the query so it will return all records from the table. For the query, an index scan is the most efficient means of getting the data for this query.

    If you want it to perform a seek, add a where clause to the query.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • SQLRNNR (7/28/2014)


    Your query mandates a table/index scan. There is no filter/predicate/where clause on the query so it will return all records from the table. For the query, an index scan is the most efficient means of getting the data for this query.

    If you want it to perform a seek, add a where clause to the query.

    thanks for your response. I Appreciate it !

    Do you mean index scan equal with table scan ? so it means even though that table has no index , it will still produce the same performance ?

  • murnilim9 (7/28/2014)


    SQLRNNR (7/28/2014)


    Your query mandates a table/index scan. There is no filter/predicate/where clause on the query so it will return all records from the table. For the query, an index scan is the most efficient means of getting the data for this query.

    If you want it to perform a seek, add a where clause to the query.

    thanks for your response. I Appreciate it !

    Do you mean index scan equal with table scan ? so it means even though that table has no index , it will still produce the same performance ?

    Sometimes an Index Scan is the same as a Table Scan. Sometimes one will perform better than the other. In my last post I was really meaning index or table scan when i put index/table. But if you are getting clustered index scan - that is a table scan.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • SQLRNNR (7/29/2014)


    murnilim9 (7/28/2014)


    SQLRNNR (7/28/2014)


    Your query mandates a table/index scan. There is no filter/predicate/where clause on the query so it will return all records from the table. For the query, an index scan is the most efficient means of getting the data for this query.

    If you want it to perform a seek, add a where clause to the query.

    thanks for your response. I Appreciate it !

    Do you mean index scan equal with table scan ? so it means even though that table has no index , it will still produce the same performance ?

    Sometimes an Index Scan is the same as a Table Scan. Sometimes one will perform better than the other. In my last post I was really meaning index or table scan when i put index/table. But if you are getting clustered index scan - that is a table scan.

    thanks !!!

    cheers

  • You're welcome.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply