I've already asked a similar question yesterday. (Question)
But after customizing the query - the performance is really poor.
Situation
Table Position (Count: 800'000)
Id Number Leafnode From To VersionId
-- --------- -------- ---- ---- ---------
1 100200300 0 NULL NULL 33
2 100200301 1 NULL NULL 34
3 100200302 1 5 10 34
...
Table Variable (Count: 1'300'000)
Id Number PositionId
-- ------ ----------
1 01 2
2 01 3
3 02 3
4 03 3
....
Table VariableText (Count: 1'300'000)
Id Language Text VariableId
-- -------- ---------- ----------
1 1 Hello 1
2 2 Hallo 1
3 3 Salut 1
4 1 Bye 2
5 2 Tschau 2
...
I'm looking for a good performing query (View, Stored Procedures, User Function). From my application I'd like to use a query like:
SELECT Id, Number, Text, Variable
FROM <whatever>
WHERE Language = 2 AND Version = 34 AND Number IN (100200301, 100200305)
And the result shoul be:
Id Number Text Variable
-- --------- ------ --------
2 100200301 Hallo 01
3 100200305 Tschau 01
...
UPDATE I've uploaded the database backup which contains these three tables described above. (Backup)
I used the following query to get just the 'number' and 'id' back from the position table. No joins and no other things. And this query takes about 8 minutes.
WITH C AS
(
SELECT T.Id, CAST(Number AS int) AS Nr, Version
FROM Position AS T
WHERE Leafnode = 1
UNION ALL
SELECT T.Id, Nr + 1 AS Expr1, T.[Version]
FROM dbo.Position AS T
INNER JOIN C ON C.Id = T.Id AND T.[To] > CAST(STUFF(Nr, 1, 6, '') AS int)
)
SELECT Id, Nr, [Version]
FROM C
WHERE Version = 34 AND Nr = '241521123'
OPTION (maxrecursion 0)