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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:08:46+00:00 2026-05-11T11:08:46+00:00

We are trying to set up a cursor to run through records generated from

  • 0

We are trying to set up a cursor to run through records generated from a join between two ‘instances’ of the same huge table (more than 150 M records).

The following exception message comes out:

Could not allocate space for object ‘dbo.SORT temporary run storage: 165282123350016’ in database ‘tempdb’ because the ‘PRIMARY’ filegroup is full. Create disk space by deleting unneeded files, dropping objects in the filegroup, adding additional files to the filegroup, or setting autogrowth on for existing files in the filegroup.

Do any of you know the reason for this? Or how to make the query below below more efficient?

I have found that it occurs somewhere between DECLARE CURSOR and the first FETCH NEXT, but I do not know yet if it is between…

  • DECLARE CURSOR and OPEN

or between

  • OPEN and the first FETCH NEXT.

More details: The sql statement looks like:

 DECLARE cData CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR   SELECT ...   FROM HugeTable HT1 JOIN HugeTable HT2 ON ..    JOIN Table3 ON .. JOIN Table4 ON .. JOIN Table5 ON ..   WHERE ...   ORDER BY HT1..., HT1...  INSERT INTO SysLog (Description) VALUES ('A')  OPEN cData BEGIN TRANSACTION ProcessData   -- Currently trying new logging here:   -- INSERT INTO SysLog (Description) VALUES ('B')    FETCH NEXT FROM cData INTO ...   INSERT INTO SysLog (Description) VALUES ('C')   ... etc. 

where the last log message I get is ‘A’ and then one hour later it fails with the message described above, never reaching ‘C’. I am now trying with logging at point ‘B’.


On request I post the exact sql expression:

 DECLARE cSource CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR     SELECT MD.sFieldName,          MD.sFieldValue,          TR.sTargetDataType,         MD2.sFieldValue AS sUniqueID,         TR.sTargetTableName,         TR.sTargetFieldName,         I.iRefCustomerID,          I.iInterfaceID,          IL.iRefInterfaceSessionID     FROM MasterData MD     JOIN MasterData MD2         ON MD.iRowIndex = MD2.iRowIndex         AND MD.iBatchNumber = MD2.iBatchNumber         AND MD.sTableName = MD2.sTableName          AND MD2.sFieldName = 'sUniqueID'     JOIN SourceTargetRelation TR         ON MD.sFieldName = TR.sSourceFieldName         AND MD.sTableName = TR.sSourceTableName     JOIN InterfaceLog IL         ON IL.iInterfaceLogID = MD.iBatchNumber     JOIN Interface I         ON I.iInterfaceID = IL.iRefInterfaceID         AND TR.iRefSystemID = I.iRefSystemID     WHERE         MD.iBatchNumber = @iBatchNumber     ORDER BY MD.sTableName, MD.iRowIndex 

After the updated answer from Quassnoi, I also post the original index on the table:

I have a nonclustered index on this table with the columns iBatchNumber, sFieldName, sTableName, iRowIndex. And that index has sFieldValue as an included column.


As Quassnoi suggested (and I think I understand why now) I have changed the index to have the columns in this order: iBatchNumber, sTableName, iRowIndex, sFieldName. And I use sFieldValue as an included column. The execution plan does not contain any SORT anymore, and the number of steps in the execution plan is less than half of original, which I hope is also faster…

  • 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. 2026-05-11T11:08:47+00:00Added an answer on May 11, 2026 at 11:08 am

    Do any of you know the reason for this? Or how to make the query below below more efficient?

    Your query uses ORDER BY.

    This needs sorting and sorting needs temporary space. You are out of this space.

    To avoid this, create a composite index on your huge table: (col_filter_1, col_filter_2, col_order_1, col_order_2), where col_filter_n are the columns you filter on, and col_order_n are the columns you order by.

    Such an index can be used both for filtering and ordering the filtered results.

    If you post your actual query (that is expressions you filter on and order by), I’ll probably can tell you more exactly how to create such an index.

    Update:

    From your query, I can see that you need an index on (iBatchNumber, sTableName, iRowIndex, sFieldName) (in that order).

    It may also help if you make MD2 leading in the join:

    WHERE     MD2.iBatchNumber = @iBatchNumber ORDER BY     MD2.sTableName, MD2.iRowIndex 

    See the execution plan and make sure that no SORT operation is used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer & is the bitwise logical and operator - It performs… May 11, 2026 at 12:25 pm
  • added an answer If you use the WCF REST starter kit, there's pretty… May 11, 2026 at 12:25 pm
  • added an answer The first one is used on server side for adding… May 11, 2026 at 12:25 pm

Related Questions

We are trying to create a web-service that we plan to pass a variable
We are trying to implement a REST API for an application we have now.
We are trying to duplicate one of our informix database on a test server,
We are trying to use Log4Net to log from our IIS 6-deployed WCF Application.
I have set up a Django application that uses images. I think I have
Does anyone have experience using makefiles for Visual Studio C++ builds (under VS 2005)

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.