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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:39:22+00:00 2026-05-23T03:39:22+00:00

I know ahead of time that I’m eventually going to need a bunch of

  • 0

I know ahead of time that I’m eventually going to need a bunch of rows from a certain table, but I don’t know in what order I’ll need each one.

In general, from a performance and best practices point of view, is it a better idea to:

  1. Do a single general query that returns all the rows I know I’ll eventually need, and then use PHP (or whatever) to pick out each one as I need it; or,

  2. Do lots of queries using WHERE to pick out only each row as I need it.

The number of rows I’ll need is something like 300-600, and the page shouldn’t be getting loaded more than a few times per minute (although I’d like to pick a solution that will cope under heavier loads than this).

Edit for more info: I’m using PostgreSQL. The table is going to get quite large over time, probably with many thousands of rows. I can determine before I do any queries exactly which rows I will eventually need (as I said, n ~ 500), but what I don’t know is what order I’ll need them in. So it’s either 1 query and n PHP searches for specific values in an array, or n queries with a different WHERE clause each time. I’m leaning towards the former.

  • 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-23T03:39:23+00:00Added an answer on May 23, 2026 at 3:39 am

    It’s nearly always faster to do one query instead of n queries when possible. If your result set is in an associative array, individual rows won’t be indexed in the array. But, if you iterate over the result array and key the array elements by an row ID, they will become faster for random access in PHP (rather than having to traverse the whole array each time to find the row you need). Of course, doing so will use a bit more memory to copy the array elements.

    Either way, this is much faster than returning to the database each time, especially if you will hit it hundreds of times.

    The best option is to pull your records from the RDBMS in the order which you’ll need them with ORDER BY. Then random access isn’t an issue.

    In any case, 600 rows doesn’t sound like that much. Make sure you have indexes on appropriate columns so your query is optimized and you’ll be fine.

    EDIT When fetching rows, create an array index. Now you don’t need to search the array, as you can just access it by index.

    $rowset = array();
    while ($row = pg_fetch_assoc($result)) {
      // Append rows to $rowset indexed by the 'id' column
      // Use whatever column you'll need to be searching with PHP
      $rowset[$row['id']] = $row;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following set of sets. I don't know ahead of time how
I want to call a function that generates a function, but I don't know
PROBLEM I have a nested PHP array that I need to populate from flat
I have a program that segfaults from pointer arithmetic sometimes. I know this happens,
before I go ahead and attempt to create a website, I wanted to know
I know how to do a HEAD request with httplib, but I have to
Know of an OCAML/CAML IDE? Especially one that runs on Linux?
Know this might be rather basic, but I been trying to figure out how
I know you can minify/condense Javascript or CSS code, but can you do this
Anyone know what type of JSON (if even that!) the following code is? I'm

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.