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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:32:21+00:00 2026-06-17T19:32:21+00:00

I am running a script on our production database reffering two tables : our

  • 0

I am running a script on our production database reffering two tables : our table of users (3700 of them) and the table of quotes that they have made (280000 of them). Quote is the main object in our application, a very large object, for whom many data tables are created and filled. My goal is to clean database from all quotes but those made of a small group of users.

I first create a temp table containing ids of those users (it is used else in the script also) and then a cursor that runs through the main table for the quotes, where they are listed, and for those quotes created from the user group does the necessary cleansing.

I see that this script is going to be executed for 26 hours approximately, which I consider peculiar since I need about 15 minutes for the database restoring in general, and I guess the heaviest sql is executed there. The db, though, weighs more than 100GB.

Is there some part of the script that I am making terribly non-optimal, or you have some suggestion how this could be done with much shorter execution.

We are running SQL Server 2008 R2.

Here’s the sketch of the script.

CREATE table #UsersIdsToStay(user_id int)
INSERT INTO #UsersIdsToStay
select user_id 
from users 
where user_name like '%SOMESTRING '
-----
declare @QuoteId int
declare @UserId int

declare QuoteCursor cursor for 
select DISTINCT QuoteId, UserId
from QuotesTable
where UserId not in 
    (
        select * from #UsersIdsToStay
    )

open QuoteCursor
while 1=1
begin
    fetch QuoteCursor into @QuoteId, @UserId
    if @@fetch_status != 0 break

    -- all the deletions from related tables are executed here using @QuoteId and @UserId
    exec('delete from QuoteHistory where QuoteId = ' + @QuoteId + ' and UserId = ' + @UserId )
    exec('delete from QuoteRevisions where QuoteId = ' + @QuoteId + ' and UserId = ' + @UserId )
    exec('delete from QuoteItems where QuoteId = ' + @QuoteId + ' and UserId = ' + @UserId )
    ....

end
close QuoteCursor;
deallocate QuoteCursor
  • 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-17T19:32:22+00:00Added an answer on June 17, 2026 at 7:32 pm

    The cursor restricts you to only delete a single User_Id/Quote_Id combination at a time on each related table. By using joins you will be able to delete in mass.

    You could also switch out the temp table with a Common Table Expression(CTE). If this is a one off script the temp table should be ok, but for production code I would create a CTE.

        if OBJECT_ID('tempdb..#quotesToDelete') is not null
                drop table #quotesToDelete
    
    
        select distinct 
                    ut.user_id, 
                    qt.quote_id
            into #quotesToDelete
        from    dbo.QuotesTable qt (nolock)
            inner join dbo.UsersTable ut (nolock)
                on qt.user_id = ut.user_id
        where ut.user_name not like '%SOMESTRING '
    
        -- all the deletions from related tables are executed here using @QuoteId and @UserId
    
        -- relatedtableA
        delete a
        from relatedtableA a
            inner join #quotesToDelete b
            on a.user_id = b.user_id
            and a.quote_id = b.quote_id
    
        -- relatedtableB
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written a PHP script that takes in two CSV files, processes them and
Running my script through Devel::NYTProf showed that the following portion of code took up
I am running a script that animates a plot (simulation of a water flow).
I have a script running a batch of very similar queries. All of them,
I have a PHP script running on my LAMP server that requires certain files
I have a python script running on a small server that is called in
I have a PHP script running on XAMPP in Windows XP that will open
We have two zope servers running our company's internal site. One is the live
Our team (QA) is facing the following problem: We have a database that is
For my current project, I am creating a php script that allows our Instructors

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.