Organisations and orders share a pivot table, that shows the organisations
that are aware of the order.
The following select statement results in table scans for Organisations and
OrgLink
select code, textname from organisations where pk_organisations in
(select fk_organisations from orglink where orglinktype = 'ESP' and
fk_orders in
(select pk_orders from orders where ordnumber like '%"%'))
Is there any way to make the query use index scans?
I have added the BrettTmp directory which gets rid of one table scan (on
Orders)
(I have chopped out the irelevant parts from the script)
CREATE TABLE [dbo].[Organisations] (
[PK_Organisations] [PRIMEKEY] NOT NULL ,
[OR_TSTAMP] [timestamp] NOT NULL ,
[espcode] [char] (8) NOT NULL ,
[code] [char] (10) NOT NULL ,
[cocode] [varchar] (20) NOT NULL ,
[textname] [varchar] (35) NOT NULL ,
) ON [PRIMARY]
GO
CREATE UNIQUE INDEX [UniqueKey] ON [dbo].[Organisations]([espcode],
[code]) ON [PRIMARY]
GO
CREATE INDEX [ESPCode] ON [dbo].[Organisations]([espcode]) ON [PRIMARY]
GO
CREATE INDEX [PK_OrganisationsESPCode] ON
[dbo].[Organisations]([PK_Organisations], [espcode]) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Orders] (
[PK_Orders] [PRIMEKEY] NOT NULL ,
[OR_TSTAMP] [timestamp] NOT NULL ,
[ordnumber] [varchar] (20) NOT NULL ,
) ON [PRIMARY]
GO
CREATE INDEX [Shipment] ON [dbo].[Orders]([FK_Shipment]) ON [PRIMARY]
GO
CREATE INDEX [OrdNumber] ON [dbo].[Orders]([ordnumber]) ON [PRIMARY]
GO
CREATE INDEX [ATA] ON [dbo].[Orders]([ata]) ON [PRIMARY]
GO
CREATE INDEX [BrettTemp] ON [dbo].[Orders]([PK_Orders], [ordnumber]) ON
[PRIMARY]
GO
CREATE TABLE [dbo].[OrgLink] (
[PK_OrgLink] [PRIMEKEY] NOT NULL ,
[OL_TSTAMP] [timestamp] NOT NULL ,
[FK_Organisations] [FOREIGN_KEY] NULL ,
[OrgLinkType] [char] (3) NOT NULL ,
[FK_Shipment] [FOREIGN_KEY] NULL ,
[FK_Orders] [FOREIGN_KEY] NULL
) ON [PRIMARY]
GO
CREATE INDEX [Organisations] ON [dbo].[OrgLink]([FK_Organisations]) ON
[PRIMARY]
GO
CREATE INDEX [Shipment] ON [dbo].[OrgLink]([FK_Shipment]) ON [PRIMARY]
GO
CREATE UNIQUE INDEX [UniqueKey] ON [dbo].[OrgLink]([FK_Organisations],
[OrgLinkType], [FK_Shipment], [FK_Orders]) ON [PRIMARY]
GO
CREATE INDEX [Orders] ON [dbo].[OrgLink]([FK_Orders]) ON [PRIMARY]
GO