I've got a table which can potentially contain several versions of each record in different languages. I need to be able to retrieve the chosen record in the preferred language
if it exists falling back on the English record if it's not there.
e.g.
Code:
Id | LanguageCode | Value
1 | ENG | Hello
1 | FRA | Bonjour
2 | ENG | Thanks
2 | DEU | Danke
If I want the table in french I should get;
Code:
Id | LanguageCode | Value
1 | FRA | Bonjour
2 | ENG | Thanks
I'm after opinions on which is the best/fastest approach to get the data.
The platform is SQL Server 2008 accessed from .NET, I expect up to about 50k records in the table when populated.
I've written a table value function like this
Code:
CREATE FUNCTION [dbo].[MyLocalisedTable](@LanguageCode varchar(3))
RETURNS TABLE
AS
RETURN
(
SELECT Id, LanguageCode, Value
FROM (SELECT Id, LanguageCode, Value, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY case when LanguageCode=@LanguageCode THEN 0 ELSE 1 END, case when LanguageCode='ENG' THEN 0 ELSE 1 END) as rowno FROM MyTable) as MyTable
WHERE rowno=1
)
Which I can use very simply as
Code:
SELECT * FROM dbo.MyLocalisedTable('FRA') WHERE Id=1
Now that seems to work but I dont have enough experience in high-traffic systems to know if it's efficient enough. There's also a complicating factor that I want to start including record versioning by dates to maintain an archive, so the table could wind up with a couple of date fields as well.
Code:
Id | LanguageCode | FromDate | ToDate | Value
1 | ENG | 01/01/2010 | 01/04/2010 | Hello
1 | ENG | 01/04/2010 | NULL | Hi
1 | FRA | 01/01/2010 | NULL | Bonjour
2 | ENG | 01/01/2010 | 01/04/2010 | Thank you
2 | ENG | 01/04/2010 | NULL | Thanks
2 | DEU | 01/01/2010 | NULL | Danke
In a situation like that what would be the best primary key and index choices? I'm assuming the first 4 fields as a composite pk, but I'm not sure if I should be adding another unique id field instead.
Any hints would be much appreciated