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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:36:31+00:00 2026-06-08T07:36:31+00:00

So there are two queries. Why first is running much faster than second? How

  • 0

So there are two queries. Why first is running much faster than second? How can I optimize my inner join to be as fast as the first one ?

Total records more than 4 000 000

idTerminals is indexed


select (select name 
          from terminals 
          where terminals.idTerminals = AnalyzeProfitResults.idTerminals),
        currency, 
        count(*) 
from AnalyzeProfitResults
group by AnalyzeProfitResults.idTerminals,currency;


select name,
        currency, 
        count(*) 
from AnalyzeProfitResults
inner join terminals on terminals.idTerminals = AnalyzeProfitResults.idTerminals
group by AnalyzeProfitResults.idTerminals,currency;

    

the results are same:


name,currency,count(*)
"Buy or sell",EURUSD,235105
"Buy or sell",GBPUSD,14515
"Buy or sell",USDJPY,235298
"very strict",AUDJPY,234787
"very strict",AUDUSD,235298
"very strict",CHFJPY,235298
"very strict",EURJPY,235298
"very strict",EURUSD,235298
"very strict",GBPJPY,235298
"very strict",GBPUSD,235298
"very strict",NZDUSD,235298
"very strict",USDCHF,235298
"very strict",USDJPY,235298
",normal",AUDJPY,235298
",normal",AUDUSD,36391
",normal",EURUSD,234985
",normal",GBPJPY,117649
",normal",NZDUSD,198552
",normal",USDCHF,235298
",normal",USDJPY,235298
 
  • 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-06-08T07:36:33+00:00Added an answer on June 8, 2026 at 7:36 am

    I suppose in the latter query, MySQL joins AnalyzeProfitResults and terminals which yields size(AnalyzeProfitResults) * size(terminals) rows for the group by operation. With 4 million rows in AnalyzeProfitResults, that’s a lot of rows even if terminals contains just a handful of records. In the former case, MySQL performs the grouping on 4 million rows which yields the 20ish rows you gave as result, and only then performs a subquery for each of the 20 rows (which might even be rewritten to a join internally, that would be even faster).

    edit:
    Here’s the first query with an explicit join:

    select terminals.name, APR_aggregated.*
    from terminals join (
        select APR.currency, count(*)
        from AnalyzeProfitResults as APR
        group by APR.idTerminals, APR.currency
    ) as APR_aggregated on terminals.idTerminals = APR_aggregated.idTerminals
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following two queries, which one would be faster (in theory) if there
In my .hbm.xml there are two queries. The first one retrieves number of records
I'm looking to optimize two queries into one, if possible. My first query searches
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Hey how can I combine the two queries below into one?? so I get
I have two queries and i'm using the result of the first one in
I'm working on code that does nearest-neighbor queries. There are two simple ideas that
Is there anyway to batch two different select queries to either SQLite or SQLCE
I want to combine these two queries into one: mysql_query(INSERT INTO categories (name, parent)
I am writing my first little Access 2003 application. I have two queries that

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.