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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:20:18+00:00 2026-05-25T12:20:18+00:00

I have a single table that contains two types of items under the same

  • 0

I have a single table that contains two types of items under the same class/category. I want to use php to output two lists, each containing each type of item. Would it be generally faster and more acceptable to sort them by using two queries or just query them all and then just PHP to sort them using if($type == 'blah') statements?

Which is generally more recommended/acceptable?

I am aware of benchmarking but I would like a general opinion/standard, as benchmarking can usually be very dependant on other factors.

Sample result set

entry_id   user_id  element_id  saved_date  saved_type
    65     1         20         1315473732  listing
    66     1         22         1315473735  listing
    49     1         5          1315429549  user
    62     1         1          1315470500  user
    70     1         15         1315473763  listing
    72     1         2          1315476924  listing

In general, results shouldn’t exceed more than 100 records in total. Example output is basically this result set separated into two tables “Saved Listings” and “Saved Users”.

  • 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-25T12:20:19+00:00Added an answer on May 25, 2026 at 12:20 pm

    Ease of understanding
    I think it’s a bad pattern if you write complicated code in php, that can be trivally coded in SQL, see @Col Shrapnel’s answer.

    The top SQL statement

    $type1 = $db->getArr("SELECT * FROM table WHERE type=1");
    

    is instantly clear: get all items with type = 1.

    The bottom one requires scanning. Also note that @Col shrapnel’s top and bottom code samples are not 100% equivalent! This is a minor oversight I’m sure, but sometimes it can be really hard to write equivalent code to SQL in php, SQL differs in subtle ways from procedural logic that can be very hard to grok.
    SQL wins

    Network load
    Running tiome is one time thing, but you also need to take network load into account.
    If you do a select * and then soft out the results in php, that means data (may) need to cross the network, a very slow medium.
    This is a waste of resources and the network time can easily outweigh all other time factors.
    SQL wins

    Pure running time
    This is a hard one too call, SQL has indexes on the data that it can (re)use to sort the data, however sometimes it does too much work and php will be a faster choose.
    I would contest though that because SQL has more information available (in the form of indexes) to handle the data fast.
    There are also (many) tricks you can use e.g. SQL_SMALL_RESULT to force MySQL to keep the data in memory and not use disk for temp-storage.
    To close too call

    Architecturally
    If you design an application, and the underlying database changes, which option would make be easier to redesign, which is more robust.
    If you do all the work in SQL and always present standardized results to php, you can seperate the php code from the database.
    If you do half your database processing in php, you lose that advantage.
    This kind of tight coupling between the raw database and php is a clear fail in my opinion and a well known anti-pattern.
    SQL wins

    Closing argument
    I can go on, but I would like to rest my case and declare SQL the clear winner.
    php can maybe score a few points on a minor issue, but I doubt the few cases where it is faster are sufficiently important to overcome the disadvantages you are heaping on your project.
    I think you are also underestimating the many ways in which SQL can be optimized.

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

Sidebar

Related Questions

I have dbml with single table users i want add partial class for User
I have a form that searches all rows in a single table (TServices) that
I'm working on a project that will have a single table holding lots and
Background I have a dimension table that has a single record for each day.
Here's the caveat... If you have a table that has a single column and
Say I have a table called xml that stores XML files in a single
I have two tables that are considered a single entity in my domain model.
I have a huge access mdb file which contains a single table with 20-30
using mysql and php, I have two tables, I'm french so the table names
I have a database with a table that contains Name and CompanyName . They

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.