Dev & Prod Of Azure SQL Datababase

I have a azure sql database dev and production environment when I run the dimension of standard bom query that combine multiple source system , the query runs within 2 minutes pulling 30k rows,

When i try to run the same query in my production, it is running more than 2 hours but did not bring full result yet.

both my sql database have 250gb storage where dev used space is 180gb, allocated space is (190gb) but in production i have only 88.48gb(used space) 88.52gb(allocated space)

        SELECT 
         0 AS TreeLevel,
         TRIM(PartCode) As Ancestor,
         TRIM(BOM.PartCode) AS Parent,
         TRIM(BOM.SubPartCode) AS Child,
         PartTraversepath = CAST(TRIM(PartCode) AS Varchar(8000)) + "https://stackoverflow.com/" + CAST(TRIM(SubPartCode) AS VArchar(8000)),
         BOM.RevisionDate AS StartEff,
         ActiveToDate AS EndEff,
         BOM.Qty AS Qty,
         BOM.Description AS PartDescription,
         BOM.OrdCode,
         BOM.LevelOffPhantomInd,
         BOM.RECORDSOURCE
      FROM stg.BillOfMat AS BOM
      WHERE BOM.PartCode="PA000010"

      UNION ALL

      SELECT 
         CTE.TreeLevel + 1,
         CTE.Ancestor As Ancestor,
         TRIM(BOM.PartCode) AS Parent,
         TRIM(BOM.SubPartCode) AS Child,
         PartTraversePath=PartTraversePath + "https://stackoverflow.com/"+CAST(TRIM(BOM.SubPartCode) AS VARCHAR(8000)),
         BOM.RevisionDate AS StartEff,
         NULL AS EndEff,
         BOM.Qty AS Qty,
         BOM.Description AS PartDescription,
         BOM.OrdCode,
         BOM.LevelOffPhantomInd,
         CTE.RECORDSOURCE
      FROM BOM_Expanded AS CTE
      INNER JOIN stg.BillOfMat AS BOM ON CTE.Child = BOM.PartCode
      AND CTE.RECORDSOURCE=BOM.RECORDSOURCE
       WHERE CTE.TreeLevel <= 20
       AND (BOM.OrdCode <> 7 OR BOM.LevelOffPhantomInd <> 1) -- Exclude if OrdCode is 7 and LevelOffPhantomInd is 1
   )

what am i doing wrong in production?

Read more here: Source link