Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6997115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:14:35+00:00 2026-05-27T20:14:35+00:00

I wrote this function that feeds into an XSLT tool for reporting. It’s incredibly

  • 0

I wrote this function that feeds into an XSLT tool for reporting. It’s incredibly slow, taking about 1.5-2 seconds on average to return 100 rows. This gets worse as this query can return up to 2000 rows. Any sort of excess load is causing the query to time out, so now I need to optimize it.

USE [ININ_SID]

GO

/****** Object:  UserDefinedFunction [dbo].[GetProjectData]    Script Date: 12/30/2011 10:27:22 ******/
SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER FUNCTION [dbo].[GetProjectData] (
-- Add the parameters for the function here
@ProjectNo VARCHAR(100))
RETURNS @Final TABLE (
  Customer             VARCHAR(250),
  ProjectNo            VARCHAR(50),
  ProjectName          VARCHAR(512),
  ProjectType          VARCHAR(100),
  PhaseID              INT,
  Phase                VARCHAR(50),
  NameField1           VARCHAR(100),
  IDField1             INT,
  IDField2             INT,
  ItemDescription      VARCHAR(100),
  Health               VARCHAR(6),
  CompletionPercentage INT,
  Status               VARCHAR(16),
  Owner                VARCHAR(64),
  ItemNotes            VARCHAR(140),
  ProjectNotes         VARCHAR(140))
AS
  BEGIN
      DECLARE @Health TABLE (
        ProjectID    INT,
        ProjectNotes VARCHAR(140))
      DECLARE @Projects TABLE (
        ProjectID     INT PRIMARY KEY,
        ProjectSiteId INT,
        ProjectNo     VARCHAR(50),
        ProjectType   VARCHAR(100),
        ProjectName   VARCHAR(512),
        Customer      VARCHAR(250),
        UNIQUE (ProjectID))

      INSERT INTO @Projects
      SELECT PSO_Projects.ProjectID,
             ProjectSiteID,
             ProjectNo,
             ProjectType,
             ProjectName,
             Customers.Name AS Customer
      FROM   PSO_Projects
             INNER JOIN Customers
               ON Customers.CustomerID = PSO_Projects.CustomerID
      WHERE  PSO_Projects.ProjectNo = @ProjectNo
             AND CompletionDate IS NULL

      -- Get the latest health/notes for the projects
      -- This will be used in an inner join later
      INSERT INTO @Health
      SELECT ProjectID,
             ProjectNotes
      FROM   (SELECT ProjectID,
                     Notes                                                              AS ProjectNotes,
                     Rank() OVER (Partition BY ProjectID ORDER BY LastUpdatedDate DESC) AS Rank
              FROM   PSO_ProjectHealthEntries) tmp
      WHERE  Rank = 1
             AND ProjectID IN (SELECT ProjectID
                               FROM   @Projects)

      -- PROJECT DELIVERABLES
      INSERT INTO @Final
                  (Customer,
                   ProjectNo,
                   ProjectName,
                   ProjectType,
                   PhaseID,
                   Phase,
                   NameField1,
                   IDField1,
                   IDField2,
                   ItemDescription,
                   Health,
                   CompletionPercentage,
                   Status,
                   Owner,
                   ItemNotes,
                   ProjectNotes)
      SELECT Customer,
             ProjectNo,
             ProjectName,
             ProjectType,
             PSO_phases.PhaseID,
             PSO_Phases.Name,
             PSO_Phases.PhaseType,
             PSO_Deliverables.DeliverableID,
             PSO_Deliverables.MasterID,
             PSO_Deliverables.Name,
             PSO_Deliverables.Health,
             PSO_Deliverables.CompletionPercentage,
             pso_deliverables.Status,
             DeliverableContacts.Name,
             PSO_Deliverables.Notes,
             Health.ProjectNotes
      FROM   @Projects Projects
             INNER JOIN @Health Health
               ON Health.ProjectID = Projects.ProjectID
             INNER JOIN PSO_Phases
               ON PSO_Phases.ProjectSiteID = Projects.ProjectSiteID
             LEFT JOIN PSO_Deliverables
               ON PSO_Deliverables.PhaseID = PSO_Phases.PhaseID
             LEFT JOIN PSO_DeliverableAssociations
               ON PSO_DeliverableAssociations.DeliverableID = PSO_Deliverables.DeliverableID
                  AND PSO_DeliverableAssociations.Active = 1
             LEFT JOIN PSO_ProjectContacts DeliverableContacts
               ON DeliverableContacts.ContactID = PSO_DeliverableAssociations.ContactID
      WHERE  PSO_Phases.Name  'System Development & Deployment'
             AND PSO_Deliverables.Type NOT IN (SELECT DISTINCT ManagementGroup
                                               FROM   PSO_DeliverableOwnership)
             AND Projects.ProjectID IN (SELECT ProjectID
                                        FROM   @Projects)

      -- ADDON DELIVERABLES
      INSERT INTO @Final
                  (Customer,
                   ProjectNo,
                   ProjectName,
                   ProjectType,
                   PhaseID,
                   Phase,
                   NameField1,
                   IDField1,
                   IDField2,
                   ItemDescription,
                   Health,
                   CompletionPercentage,
                   Status,
                   Owner,
                   ItemNotes,
                   ProjectNotes)
      SELECT Customer,
             ProjectNo,
             ProjectName,
             ProjectType,
             PSO_Phases.PhaseID,
             'Add-On Deliverables',
             PSO_Deliverables.SubType,
             PSO_Deliverables.DeliverableID,
             PSO_Deliverables.MasterID,
             PSO_Deliverables.Name + ' (' + PSO_Deliverables.SubType + ')',
             PSO_Deliverables.Health,
             PSO_Deliverables.CompletionPercentage,
             PSO_Deliverables.Status,
             PSO_ProjectContacts.Name,
             PSO_Deliverables.Notes,
             Health.ProjectNotes
      FROM   @Projects Projects
             INNER JOIN @Health Health
               ON Health.ProjectID = Projects.ProjectID
             INNER JOIN PSO_Phases
               ON PSO_Phases.ProjectSiteID = Projects.ProjectSiteID
             INNER JOIN PSO_Deliverables
               ON PSO_Deliverables.PhaseID = PSO_Phases.PhaseID
             LEFT JOIN PSO_DeliverableAssociations
               ON PSO_DeliverableAssociations.DeliverableID = PSO_Deliverables.DeliverableID
                  AND PSO_DeliverableAssociations.Active = 1
             LEFT JOIN PSO_ProjectContacts
               ON PSO_ProjectContacts.ContactID = PSO_DeliverableAssociations.ContactID
      WHERE  Projects.ProjectID IN (SELECT ProjectID
                                    FROM   @Projects)
             AND ( PSO_Deliverables.Type IN (SELECT DISTINCT ManagementGroup
                                             FROM   PSO_DeliverableOwnership) )

      -- PROJECT  SITE NAMES ONLY
      INSERT INTO @Final
                  (Customer,
                   ProjectNo,
                   ProjectName,
                   ProjectType,
                   PhaseID,
                   Phase,
                   NameField1,
                   IDField1,
                   IDField2,
                   ItemDescription,
                   Health,
                   CompletionPercentage,
                   Status,
                   Owner,
                   ItemNotes,
                   ProjectNotes)
      SELECT Customer,
             ProjectNo,
             ProjectName,
             ProjectType,
             0,
             'Systems Development and Deployment',
             PSO_Sites.Name,
             PSO_Sites.SiteID,
             PSO_Sites.SiteID,
             PSO_Sites.Name,
             '' AS Health,
             '' AS CompletionPercentage,
             '' AS Status,
             '' AS Owner,
             '' AS Notes,
             Health.ProjectNotes
      FROM   @Projects Projects
             INNER JOIN @Health Health
               ON Health.ProjectID = Projects.ProjectID
             INNER JOIN PSO_Sites
               ON PSO_Sites.ProjectID = Projects.ProjectID
      WHERE  Projects.ProjectID IN (SELECT ProjectID
                                    FROM   @Projects)

      RETURN
  END 

And on top of this, order is extremely important. This is how it’s called:


SELECT Data.*
FROM   PSO_Projects
       INNER JOIN PSO_ProjectAssociations
         ON PSO_ProjectAssociations.ProjectID = PSO_Projects.ProjectID
            AND PSO_ProjectAssociations.Active = 1
       INNER JOIN PSO_ProjectContacts
         ON PSO_ProjectContacts.ContactID = PSO_ProjectAssociations.ContactID
       CROSS apply Getprojectdata(ProjectNo) Data
WHERE  PSO_ProjectContacts.Name = 'John.Smith'
       AND PSO_Projects.CompletionDate IS NULL
ORDER  BY Customer,
          ProjectNo,
          CASE
            WHEN Phase = 'Initiation' THEN PhaseID
            WHEN Phase = 'Planning' THEN PhaseID + 2000
            WHEN Phase = 'Requirements & Design' THEN PhaseID + 4000
            WHEN Phase = 'Systems Development and Deployment' THEN PhaseID + 6000
            WHEN Phase = 'Add-On Deliverables' THEN PhaseID + 8000
            WHEN Phase = 'Closing' THEN PhaseID + 10000
            ELSE PhaseID
          END,
          IDField2,
          IDField1 

Is there anything I can do in the query to optimize this? The only other real option I have is to find a way to refactor the report to be able to handle fewer columns (such as returning the project name/customer in a single row instead of including it with every row) but I’m not sure how feasible this is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:14:35+00:00Added an answer on May 27, 2026 at 8:14 pm

    As a quick hit, just take out your not in and use not exists, and all of your in statements checking the ProjectID are useless. Make the insert into @Final one unioned statement, like so:

    SELECT 
        Customer,
        ProjectNo,
        ProjectName,
        ProjectType,
        PSO_phases.PhaseID,
        PSO_Phases.Name,
        PSO_Phases.PhaseType,
        PSO_Deliverables.DeliverableID,
        PSO_Deliverables.MasterID,
        PSO_Deliverables.Name,
        PSO_Deliverables.Health,
        PSO_Deliverables.CompletionPercentage,
        pso_deliverables.Status,
        DeliverableContacts.Name,
        PSO_Deliverables.Notes,
        Health.ProjectNotes
    FROM   
        @Projects Projects
         INNER JOIN @Health Health
           ON Health.ProjectID = Projects.ProjectID
         INNER JOIN PSO_Phases
           ON PSO_Phases.ProjectSiteID = Projects.ProjectSiteID
         LEFT JOIN PSO_Deliverables
           ON PSO_Deliverables.PhaseID = PSO_Phases.PhaseID
         LEFT JOIN PSO_DeliverableAssociations
           ON PSO_DeliverableAssociations.DeliverableID = PSO_Deliverables.DeliverableID
              AND PSO_DeliverableAssociations.Active = 1
         LEFT JOIN PSO_ProjectContacts DeliverableContacts
           ON DeliverableContacts.ContactID = PSO_DeliverableAssociations.ContactID
    WHERE  
        PSO_Phases.Name = 'System Development & Deployment'
        AND NOT EXISTS (
            SELECT
                1
            FROM
                PSO_DeliverableOwnership
            where
                PSO_Deliverables.Type = ManagementGroup
        )
    union all
    SELECT Customer,
         ProjectNo,
         ProjectName,
         ProjectType,
         PSO_Phases.PhaseID,
         'Add-On Deliverables',
         PSO_Deliverables.SubType,
         PSO_Deliverables.DeliverableID,
         PSO_Deliverables.MasterID,
         PSO_Deliverables.Name + ' (' + PSO_Deliverables.SubType + ')',
         PSO_Deliverables.Health,
         PSO_Deliverables.CompletionPercentage,
         PSO_Deliverables.Status,
         PSO_ProjectContacts.Name,
         PSO_Deliverables.Notes,
         Health.ProjectNotes
    FROM   @Projects Projects
         INNER JOIN @Health Health
           ON Health.ProjectID = Projects.ProjectID
         INNER JOIN PSO_Phases
           ON PSO_Phases.ProjectSiteID = Projects.ProjectSiteID
         INNER JOIN PSO_Deliverables
           ON PSO_Deliverables.PhaseID = PSO_Phases.PhaseID
         LEFT JOIN PSO_DeliverableAssociations
           ON PSO_DeliverableAssociations.DeliverableID = PSO_Deliverables.DeliverableID
              AND PSO_DeliverableAssociations.Active = 1
         LEFT JOIN PSO_ProjectContacts
           ON PSO_ProjectContacts.ContactID = PSO_DeliverableAssociations.ContactID
    WHERE
        NOT EXISTS (
            SELECT
                1
            FROM
                PSO_DeliverableOwnership
            where
                PSO_Deliverables.Type = ManagementGroup
        )
    union all
    SELECT Customer,
         ProjectNo,
         ProjectName,
         ProjectType,
         0,
         'Systems Development and Deployment',
         PSO_Sites.Name,
         PSO_Sites.SiteID,
         PSO_Sites.SiteID,
         PSO_Sites.Name,
         '' AS Health,
         '' AS CompletionPercentage,
         '' AS Status,
         '' AS Owner,
         '' AS Notes,
         Health.ProjectNotes
    FROM   @Projects Projects
         INNER JOIN @Health Health
           ON Health.ProjectID = Projects.ProjectID
         INNER JOIN PSO_Sites
           ON PSO_Sites.ProjectID = Projects.ProjectID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .
I have this Array i wrote a function MostFreq that takes an array of
I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote this function to get the unread count of google reader items. function
I wrote this function to get a pseudo random float between 0 .. 1
I want to convert function object to function. I wrote this code, but it
this is somewhat of an odd question. I wrote a C function. Its 'like'
$(function(){ $('a').each(function(){ var x=this.href; this.href=www.somesitename.com/filter+this.href; }); }); i wrote the above jQuery script to
I know i could write a custom function that does just this, but it
I wrote a function that builds an XML string based on the data saved

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.