This is a production machine supplied by a vendor. We just replaced it. The Old one had the issue with SQL Studio, but executed perfectly (in a second) in Ignition. I have no control on that machine except for a read only user to query views. Can't do indexes or anything (Not if I could if I could anyway...) He're the query if you can find any better fix
Sorry my comments are in French 
The goal is to get my closed bundles ([AutViewPackageHeader]) from a Shift with the Sum of BoardFeet in [AutViewTallyInventory] by Product Code (ScoopCode).
I'm trying a different approach now, by dumping bundle by bundle with it's Tally info a temp table then do the sum on that table and the string formatting.
/*Dans mon script en python, j'execute chaque requĂȘte dans des transactions sĂ©parĂ©es et met la valeur dans
une variable pour l'utiliser dans les autres requĂȘtes d'aprĂšs, au lieu tout faire dans une requĂȘte comme ici.
Ătrangement dans mon script en python, qui utilise un driver SQL en Java, tout le script s'exĂ©cute en moins
de 5 seconds... Celui si dans Management Studio, il prend 3 min Ă 4 mins...
*/
DECLARE @Faction varchar(25),
@Equipe varchar(25),
@Debut datetime,
@Fin datetime,
@Shift int,
@Entrepot int
SELECT @Shift=4200
SELECT @Faction=(SELECT(REPLICATE(' ',25-LEN(RTRIM([Shift])))+ [Shift])
FROM [AutologACCTrimmerHardWood].[dbo].[AutViewShiftHeader]
WHERE IDDetail = @Shift
)
SELECT @Equipe=(SELECT(REPLICATE(' ',25-LEN(RTRIM([Crew])))+ [Crew])
FROM [AutologACCTrimmerHardWood].[dbo].[AutViewShiftHeader]
WHERE IDDetail = @Shift
)
SELECT @Debut=(SELECT [StartDate]
FROM [AutologACCTrimmerHardWood].[dbo].[AutViewShiftHeader]
WHERE IDDetail = @Shift
)
SELECT @Fin=(SELECT [EndDate]
FROM [AutologACCTrimmerHardWood].[dbo].[AutViewShiftHeader]
WHERE IDDetail = @Shift
)
/Changer Ă 210,300 ou 400/
SELECT @Entrepot=210
/*Dans mon script j'execute une requĂȘte qui me donne les entrepots dans un array et je for loop par entrepot.
Je ne pouvais pas faire pareille ici, alors il faut executĂ© la requĂȘte par entrepot manuellement.
*/
SELECT 'QUE' AS Localisation,
(SELECT([ScoopCode] + REPLICATE(' ',25-LEN(RTRIM([ScoopCode]))))
)AS CodeProduit,
(SELECT
(SELECT([Specie] + REPLICATE(' ',25-LEN(RTRIM([Specie]))))
) AS Specie
FROM [AutologACCTrimmerHardWood].[dbo].AutViewSpecieHeader
WHERE IDDetail = IDDetailSpecie
)AS Specie,
(SELECT [SortTbl]+(REPLICATE(' ',25-LEN(RTRIM([SortTbl]))))
)AS Classement,
(SELECT(
REPLICATE('0',25-LEN(RTRIM(CONVERT(varchar(10),CONVERT(DECIMAL(20,4),SUM([BoardFeetOut]))))))
+CONVERT(varchar(10),CONVERT(DECIMAL(20,4),SUM([BoardFeetOut])))
)
)AS PMP,
@Faction as Faction,
@Equipe as Equipe,
(SELECT CONVERT(varchar, @Debut, 101)
)AS DateDebutFaction,
(SELECT LEFT(CONVERT(varchar, @Fin, 108), 5)
)AS HeureDebutFaction,
CASE
WHEN Warehouse is NULL
THEN 210
ELSE Warehouse
END as Entrepot
FROM [AutologACCTrimmerHardWood].[dbo].[AutViewTallyInventory] AS TALLY
WHERE [Pile] = 1 AND Warehouse = @Entrepot
AND IDDetailPackage IN
(SELECT [IDDetail]
FROM [AutologACCTrimmerHardWood].[dbo].[AutViewPackageHeader]
WHERE EndDate BETWEEN @Debut AND @Fin
)
GROUP BY [ScoopCode],IDDetailSpecie,[SortTbl], Warehouse
ORDER BY ScoopCode
Result looks like:
Localisation CodeProduit Specie Classement PMP Faction Equipe DateDebutFaction HeureDebutFaction Entrepot
QUE TLVE1U0440RW070 Tremble Tremble cour 00000000000000001289.0000 Jours # 211 08/11/2017 17:15 210
QUE TLVE1U0440RW080 Tremble Tremble cour 00000000000000000945.0000 Jours # 211 08/11/2017 17:15 210
QUE TLVESU0440RW070 Tremble Tremble cour 00000000000000002252.0000 Jours # 211 08/11/2017 17:15 210
QUE TLVESU0440RW080 Tremble Tremble cour 00000000000000001908.0000 Jours # 211 08/11/2017 17:15 210