Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Creating appropriate index Expand / Collapse
Author
Message
Posted Sunday, March 16, 2014 10:44 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, March 16, 2014 12:02 PM
Points: 33, Visits: 58
I have a transaction table with columns as below:

(1) Tran_Id int P.K [Identity]
(2) Location_Id smallint
(3) Vertical_Id smallint
(4) Customer_Id smallint
(5) M_ID int
(6) Condition_ID tinyint
(7) T_ID smallint
(8) Amount decimal(10,2)
(9) Transaction_Date DateTime

When user selects Date, Location, Vertical and Customer, a set of combination is returned (M_ID, Condition_ID and T_Id) for that date.

I want to create an index and want to know which columns to include.

I am thinking something like:

CREATE INDEX IDX_DETAILS
ON Transaction_Date
INCLUDE (Location_ID, Vertical_ID, Customer_ID, M_ID, Condition_ID, T_ID)


I want to do this because all the columns in the INCLUDE clause are linked to Transaction Date.

Whenever I want to pick any set of M_ID, Condition_ID and T_ID, I am first going to select Date, Location, Vertical and Customer.

I ran the Actual Execution Plan but it is not suggesting any missing indexes because the data, as of now, is low.

What type of index would be suitable in this case?





"Here is a test to find out whether your mission in life is complete. If you're alive, it isn't. "

Richard Bach
Post #1551572
Posted Sunday, March 16, 2014 11:40 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 2:47 PM
Points: 2,929, Visits: 2,944
Hi,
You should always keep the indexing key as narrower as possible. However, sometimes you could use one index to cover more queries. In your case you plan to use only the Transaction_Date column in the key list, and include the other columns. If the execution plan does not detect missing indexes, then it'll keep on that way regardless of the table size. It will be better if you can overview some other queries to possibly extend the key and the include list, but always try to keep narrower the key list for faster navigation. The extension of the include list increases the index's size and does not have impact on the index's seeking speed.

Regards,
Igor




Igor Micev,
SQL Server developer at Seavus
www.seavus.com
Post #1551577
Posted Sunday, March 16, 2014 11:53 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, March 16, 2014 12:02 PM
Points: 33, Visits: 58
What if I create two indexes as below:

CREATE INDEX IDX_DATE
ON Transaction_Date


CREATE INDEX IDX_LOCATION
ON Location_ID
INCLUDE (Vertical_ID, Customer_Id)


"Here is a test to find out whether your mission in life is complete. If you're alive, it isn't. "

Richard Bach
Post #1551580
Posted Sunday, March 16, 2014 11:57 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 2:47 PM
Points: 2,929, Visits: 2,944
@RDBMS (3/16/2014)
What if I create two indexes as below:

CREATE INDEX IDX_DATE
ON Transaction_Date


CREATE INDEX IDX_LOCATION
ON Location_ID
INCLUDE (Vertical_ID, Customer_Id)


You'd better go with one index having Location_ID and Transaction_Date in the key list. The combination of an int and date column has proved as a good indexing strategy. In general you should avoid more than necessary indexes in a table.




Igor Micev,
SQL Server developer at Seavus
www.seavus.com
Post #1551581
Posted Sunday, March 16, 2014 12:05 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, March 16, 2014 12:02 PM
Points: 33, Visits: 58
So according to your suggestion, the index would be:

CREATE INDEX IDX_LOCATION_DATE
ON Location_Id, Transaction_Date
INCLUDE (Vertical_ID, Customer_Id)


After creating this index, how can I determine whether it is performing well or not?


"Here is a test to find out whether your mission in life is complete. If you're alive, it isn't. "

Richard Bach
Post #1551582
Posted Sunday, March 16, 2014 12:13 PM This worked for the OP Answer marked as solution


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 2:47 PM
Points: 2,929, Visits: 2,944
@RDBMS (3/16/2014)
So according to your suggestion, the index would be:

CREATE INDEX IDX_LOCATION_DATE
ON Location_Id, Transaction_Date
INCLUDE (Vertical_ID, Customer_Id)


After creating this index, how can I determine whether it is performing well or not?


You can use the logical and physical reads for a query run.
Use
set statistics io on
and use
dbcc dropcleanbuffers
to clear the memory buffer, during your test.




Igor Micev,
SQL Server developer at Seavus
www.seavus.com
Post #1551585
Posted Sunday, March 16, 2014 12:16 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, March 16, 2014 12:02 PM
Points: 33, Visits: 58
Thanks Igor.

"Here is a test to find out whether your mission in life is complete. If you're alive, it isn't. "

Richard Bach
Post #1551586
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse