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

  • Home
  • SEARCH
  • 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 4540882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:08:52+00:00 2026-05-21T15:08:52+00:00

I am wondering which is the best way to query data from a large

  • 0

I am wondering which is the best way to query data from a large database.

Say I have a requirement to get all a list of all users who live in the United States, along with their orders and the products belonging to their orders. For simplicity, we have a user table, which has a CountryId in that table… and then an Order table, with a userId.. then maybe an OrderProduct table to list many products to an order (and many orders can contain the same product).

My question is, would it be better to maybe create a temp table by

SELECT userId FROM dbo.User WHERE countrId = @CountryId

We now have the relevant users in a temp table.

Then, do a

Select p.ProductDescription ... 
From @TempTable tmp 
INNER JOIN Order o 
ON o.UserId = tmp.UserId 
INNER JOIN OrderProduct op
ON op.OrderID = o.OrderId
INNER JOIN Product p
ON p.ProductId = op.ProductId

So, what I am doing is getting the users I need…. and moving that into a temp table, then using that temp table to filter the data for the main query.

Or, is it as efficient, if not more, to just do it all in one…

Select ... from User u
INNER JOIN Order o
....
WHERE u.UserId = @UserId 

?

  • 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-05-21T15:08:53+00:00Added an answer on May 21, 2026 at 3:08 pm

    In general you want to write your entire request in one query, because that gives the database query optimizer the best possible chance of coming up with the most efficient possibility. With a decent database, it will generally do a wonderful job at this, and any effort on your part to help it along is more likely to hurt than help.

    If the database is insufficiently fast, first look into things like whether you have the right indexes, tuning your database, etc. Those are the most common causes of problems and should clear up most remaining problems quite promptly.

    Only after you have given the database every chance to get the right answer in the right way, should you consider trying to use temp tables to force a particular query plan. (There are other reasons to use temp tables. But for getting good query plans, it should be a last resort.)

    There is an old pair of rules about optimization that applies here in spades.

    1. Don’t.
    2. (For experts only.) Not yet.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been wondering which is the best way to load a navigation view.
Just wondering which is best here. I want to output data from a table
I'm wondering which is the best way to communicate between a Service and an
I was wondering the best way to load a big array which stores map
I have a table in a MySQL database from which I want to select
I'm wondering which is the best way to create two lookups table for square
I was wondering which is the best way (the one suggested by Apple guidelines
I was wondering which you think is the best way to implement a "last
I was wondering which of these two scenario's works best for swapping between 2
I'm just getting started/familar with Subversion and was wondering which protocol gives the best

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.