I’m using SQL Server 2008.
I have two tables like this:
OrderItems
OrderItemID
InventoryItemID
and :
InventoryItems
InventoryItemID
ItemCode
My query plan shows a lot of time getting sucked into joining the InventoryItemIDs to get the ItemCode for every order item.
I already have a nonclustered index that includes both columns on both tables, but would like to make it even faster — can I “import” the relational ItemCode into the OrderItems table’s index?
No you cannot INCLUDE columns from another table. However, one, ‘outside the box’ suggestion would be to create an Indexed View that joins OrderItems and InventoryItems. You can setup your clustered index on the view in such a way to achieve fastest performance and it would appear that both orders and inventory items are in the same “table”, without the need to do a join. The result would be a somewhat denormalized view of the data.
Of course, there are a number of restrictions on an Indexed View, but an inner join is permitted. I have used Index Views quite frequently and they can be tremendously useful. There is a insert / update / delete performance consideration here, because each of the operations will require an update to the view. But, it might be worth it.