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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:35:01+00:00 2026-06-07T11:35:01+00:00

A cursor is used, but it’s slow and appears to be a big bottleneck

  • 0

A cursor is used, but it’s slow and appears to be a big bottleneck in a SQL job. Basically, this is a cleanup effort to remove all but the top X accessories (ordered by sales rank) from a particular source that’s previously grouped by a product id and account visibility.

The command is basically built in the each iteration of the cursor loop and exec‘ed manually.

The vis column refers to multiple tenants that sort of acts like a bitmask e.g. two tenants could have the same product.

declare @prodid int
declare @cnt    int
declare @vis    bigint
declare @cmd    varchar(600)
declare @clause varchar(600)

-- find records with more than X excess accessories
declare cur cursor for
    select pa.prodid, 'cnt' = count(*), vis from [accessories] pa
    group by prodid, vis
    having count(*) > X -- e.g. 5

Sample output could look like

prodid  cnt     vis
123     6       128
234     8       260
345     10      512

In the case where X=5, the last 1 salesrank item for 123 would be removed, the last 3 for 234 and the last 5 for 345. Can this be done using a DELETE statement while including the groupings in some nested select?

open cur

fetch next from cur into @prodid, @cnt, @vis
while @@fetch_status = 0
begin
    -- a clause that ends up looking like this:
    -- 12345 and vis = 128 -- OR -- 23456 and vis is null   
    set @clause = convert(varchar(14), @prodid) + ' and vis ' + case 
        when @vis is null then ' is null ' 
        else ' = ' + cast(@vis as varchar) end

    -- delete all but the top X from source=2 and that match prodid and vis
    set @cmd = 'delete from [accessories] 
        where source = 2 and prodid=' + @clause +
        ' and access_prodid in (select top ' + convert(varchar(5), @cnt - X) + 
        ' access_prodid from [accessories] where prodid = ' 
        + @clause + ' and source = 2 order by salesrank)'

    exec(@cmd)
    fetch next from cur into @prodid, @cnt, @vis
end 
close cur
deallocate cur
  • 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-07T11:35:02+00:00Added an answer on June 7, 2026 at 11:35 am

    Try this:

    WITH DupData AS
    (
        SELECT  *,
          ROW_NUMBER() 
              OVER(PARTITION BY pa.prodid, pa.vis ORDER BY salesrank) Position
          FROM [accessories] pa
          WHERE pa.source = 2
    )
    DELETE 
        FROM DupData
     WHERE Position > 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used setCursor() method to set a cursor for JTable. But that cursor is
I have used this code but the result is not the information of the
I have a cursor what the size size 128x128, but when i used LoadCursor
As per the title, if we used to call cursor.requery() , but it is
I read lot of about this topic but no solution can be used in
I can use [NSEvent mouseLocation] to get the cursor's location, but this gives me
RESOLVED! The problem is that I used this.findViewById, but I will use dialog.findViewById. TNX
I used Cursor in a class extends BaseAdapter, class NewDefinedAdapter extends BaseAdapter { private
I made a custom cursor using this tutorial . Problem is, as soon as
A strange thing started to happen with our application. The mouse cursor used to

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.