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 8380919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:34:47+00:00 2026-06-09T16:34:47+00:00

I have the following SP for SQL Server. Strangely the SP has weired behaviour

  • 0

I have the following SP for SQL Server. Strangely the SP has weired behaviour when executing the query

Select @max_backup_session_time = Max(MachineStat.BackupSessionTime) from MachineStat     where MachineStat.MachineID = @machine_id;

It takes 1 second if the MachineStat table has rows pertaining to @machine_id but if there are no rows for a @machine_id then it takes more than half a minute to execute. Can someone please help me understand this.

SET NOCOUNT ON;

DECLARE @MachineStatsMId TABLE (
  MachineId         INT NULL,
  BackupSessiontime BIGINT NULL,
  MachineGroupName  NVARCHAR(128) NULL )
DECLARE @machine_id AS INT;
DECLARE @Machine_group_id AS INT;
DECLARE @machine_group_name AS NVARCHAR(128);
DECLARE @max_backup_session_time AS BIGINT;

SET @machine_id = 0;
SET @Machine_group_id = 0;
SET @machine_group_name = '';

DECLARE MachinesCursor CURSOR FOR
  SELECT m.MachineId,
         m.MachineGroupId,
         mg.MachineGroupName
  FROM   Machines m,
         MachineGroups mg
  WHERE  m.MachineGroupId = mg.MachineGroupId;

OPEN MachinesCursor;

FETCH NEXT FROM MachinesCursor INTO @machine_id, @machine_group_id, @machine_group_name;

WHILE @@FETCH_STATUS = 0
  BEGIN
      SELECT @max_backup_session_time = Max(MachineStat.BackupSessionTime)
      FROM   MachineStat
      WHERE  MachineStat.MachineID = @machine_id;

      INSERT INTO @MachineStatsMId
      VALUES      (@machine_id,
                   @max_backup_session_time,
                   @machine_group_name);

      FETCH NEXT FROM MachinesCursor INTO @machine_id, @machine_group_id, @machine_group_name;
  END;

SELECT *
FROM   @MachineStatsMId;

CLOSE MachinesCursor;

DEALLOCATE MachinesCursor;
GO
  • 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-06-09T16:34:49+00:00Added an answer on June 9, 2026 at 4:34 pm

    Here is an alternate version that avoids a cursor and table variable entirely, uses proper (modern) joins and schema prefixes, and should run a lot quicker than what you have. If it still runs slow in certain scenarios, please post the actual execution plan for that scenario as well as an actual execution plan for the fast scenario.

    ALTER PROCEDURE dbo.procname
    AS
    BEGIN
      SET NOCOUNT ON;
    
      SELECT 
        m.MachineId, 
        BackupSessionTime = MAX(ms.BackupSessionTime), 
        mg.MachineGroupName
      FROM dbo.Machines AS m
      INNER JOIN dbo.MachineGroups AS mg 
        ON m.MachineGroupId = mg.MachineGroupId
      INNER JOIN dbo.MachineStat AS ms -- you may want LEFT OUTER JOIN here, not sure
        ON m.MachineId = ms.MachineID
      GROUP BY m.MachineID, mg.MachineGroupName;
    END
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL Server query: SELECT area FROM places WHERE REPLACE(area,'-',' ')
i have the following SQL Query which runs on SQL Server CE 4 SELECT
I am using SQL Server 2008 and I have the following SQL script: Select
I have the following SQL Server query and I am trying to get the
I have the following SQL Server 2008 query that works for all of my
On SQL Server 2008 R2, I have following T-SQL code: SELECT CAST(GETDATE() AS DATETIMEOFFSET);
I have following SQL query but this is not quite what I want: SELECT
I have the following SQL Server stored procedure : BEGIN TRAN CREATE TABLE #TempTable
I'm using SQL Server 2005 and have the following T-SQL statement: DECLARE @MP VARCHAR(500)
I have following tsql code in sql server 2008: declare @ID INT SET @ID

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.