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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:20:17+00:00 2026-05-28T04:20:17+00:00

I have two seperate queries: SELECT `ad_general`.`id` FROM (`ad_general`) WHERE `ad_general`.`city` = ‘708’ ORDER

  • 0

I have two seperate queries:

SELECT  `ad_general`.`id`
FROM (`ad_general`)
WHERE `ad_general`.`city` =  '708'
ORDER BY `ad_general`.`id` desc
LIMIT 15 

SELECT  count(`ad_general`.`id`) as count
FROM (`ad_general`)
WHERE `city` =  '708'

I have combined these two queries for avoiding sending multiple requests to mysql results for performance gain.

SELECT  `ad_general`.`id`, (
 SELECT  count(`ad_general`.`id`) as count
 FROM (`ad_general`)
 WHERE `city` = 708 ) AS count,
FROM (`ad_general`)
WHERE `ad_general`.`city` =  '708'
ORDER BY `ad_general`.`id` desc
LIMIT 15 

In first approach the “count” column has only one row and clearly there is only one request for count column.

But in combined query “count” column has 15 same rows.

I was wondering how mysql duplicating that “count” column in that subquery. If it’s sending same count request for each row (15 times in that case) it’s not wise to use a combined query in that case.

  • 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-28T04:20:18+00:00Added an answer on May 28, 2026 at 4:20 am

    In the first query, you are asking the database server for two pieces of information. First, the first 15 rows in the table. Second, the total number of rows in the table.

    In the second query, you are asking it for sixteen. You want the first 15 rows of the table, and for each row, you want the total number of rows.

    The performance impact of this depends on the database particulars. But no, it’s not wise: just make two queries. This is what ORMs such as Django do to handle pagination.

    Premature optimization is generally a bad idea. If you aren’t sure that sending the COUNT queries is hurting you (and it probably isn’t), don’t add complexity to “fix” what you don’t know is a problem. If you are sure it’s hurting you, then measure the performance of both means to see which one is superior. The proper performance optimization (almost certainly not necessary) is to keep a denormalized count of the rows in a ready cache.

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

Sidebar

Related Questions

Hi I have 2 select queries that are returned to two seperate tables. While
I have a group of CFC's that I access from two seperate Applicaiton scopes.
I have two seperate queries for my database which return search results for two
I have three tables and two seperate SQL queries which are working correctly and
I have a simple stored proc with two queries joined with a union: select
I have the following two mysql_queries : 1. $primary_img_query = SELECT imgWidth, imgHeight FROM
I have two heroku accounts with two seperate applications. I am new at this
I have had this problem w/ two seperate WYSWYG editors in my rails application
I have a SQL query Question, I am trying to nest two separate queries
I have two completely separate queries that were transferred over to procedures. When 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.