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 154985

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:04:32+00:00 2026-05-11T10:04:32+00:00

I have a sql query with 50 parameters, such as this one. DECLARE @p0

  • 0

I have a sql query with 50 parameters, such as this one.

DECLARE   @p0 int, @p1 int, @p2 int, (text omitted), @p49 int  SELECT   @p0=111227, @p1=146599, @p2=98917, (text omitted), @p49=125319  -- SELECT   [t0].[CustomerID], [t0].[Amount],   [t0].[OrderID], [t0].[InvoiceNumber] FROM [dbo].[Orders] AS [t0] WHERE ([t0].[CustomerID]) IN   (@p0, @p1, @p2, (text omitted), @p49) 

The estimated execution plan shows that the database will collect these parameters, order them, and then read the index Orders.CustomerID from the smallest parameter to the largest, then do a bookmark lookup for the rest of the record.

The problem is that there the smallest and largest parameter could be quite far apart and this will lead to reading possibly the entire index.

Since this is being done in a loop from the client side (50 params sent each time, for 1000 iterations), this is a bad situation. How can I formulate the query/client side code to get my data without repetitive index scanning while keeping the number of round trips down?


I thought about ordering the 50k parameters such that smaller readings of the index would occur. There is a wierd mitigating circumstance that prevents this – I can’t use this solution. To model this circumstance, just assume that I only have 50 id’s available at any time and can’t control their relative position in the global list.

  • 0 0 Answers
  • 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-11T10:04:33+00:00Added an answer on May 11, 2026 at 10:04 am

    Insert the parameters into a temporary table, then join it with your table:

    DECLARE @params AS TABLE(param INT);  INSERT INTO    @params VALUES  (@p1) ... INSERT INTO    @params VALUES  (@p49)  SELECT   [t0].[CustomerID], [t0].[Amount],   [t0].[OrderID], [t0].[InvoiceNumber] FROM @params, [dbo].[Orders] AS [t0] WHERE ([t0].[CustomerID]) = @params.param 

    This will most probably use NESTED LOOPS with a INDEX SEEK over CustomerID on each loop.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • 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
  • Editorial Team
    Editorial Team added an answer ok, i have very superficial knowledge about C#, and none… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer I would create a Two-Way data binding with that Check… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer Make sure you call VirtualProtectEx to set the correct protection… May 11, 2026 at 11:55 pm

Related Questions

I have the following Query and i need the query to fetch data from
I'm in the process of speeding up Linq to SQL queries in a web
I am trying to create an SSIS package that queries data from a table,
I've searched and am now at a loss as to the cause of my

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.