we have two related tables(inventory and inventoryLocalization) on Sql Server 2005.
the first table’s script.
CREATE TABLE [dbo].[inventory](
[code] [varchar](35) NOT NULL,
[salePrice1] [decimal](22, 2) NULL,
CONSTRAINT [PK_inventory] PRIMARY KEY CLUSTERED
(
[code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
and the second is;
CREATE TABLE [dbo].[inventoryLocalization](
[code] [varchar](35) NOT NULL,
[language] [varchar](2) NOT NULL,
[name] [nvarchar](100) NOT NULL,
[description] [nvarchar](max) NULL,
CONSTRAINT [PK_inventoryLocalization] PRIMARY KEY CLUSTERED
(
[code] ASC,
[language] ASC,
[name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
And we have a query depends on these two, as follows:
select i.[code],
iL.[language],
case
when iL.[name] is null then (select iL2.name from dbo.inventoryLocalization iL2 where iL2.[language] = 'de' and iL2.code = i.[code])
else iL.[name]
end as [name]
from dbo.inventory i
left join dbo.inventoryLocalization iL on i.code = iL.code and iL.[language] = 'en';
as you guess, we select all records with [language] = ‘en’ but if there is no record for ‘en’, query takes ‘de'( as default).
But that query takes much longer (approximately 13 seconds).
Do you think any other elegant methods for the same result to reduce the amount of time it takes ?
Thanks in advance for any assistance you can provide.
Probably slow because it has to do a query on demand when the case statement evaluates a null and runs the sub-query.
Maybe this will be faster, always do the join to ‘de’ rather than it doing a query each time a null is found.