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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:47:07+00:00 2026-05-19T12:47:07+00:00

The following query gets the info that I need. However, I noticed that as

  • 0

The following query gets the info that I need. However, I noticed that as the tables grow, my code gets slower and slower. I’m guessing it is this query. Can this written a different way to make it more efficient? I’ve heard a lot about using joins instead of subqueries, however, I don’t “get” how to do it.

  SELECT * FROM

  (SELECT MAX(T.id) AS MAXid
  FROM transactions AS T 
  GROUP BY T.position
  ORDER BY T.position) AS result1,

  (SELECT T.id AS id, T.symbol, T.t_type, T.degree, T.position, T.shares, T.price, T.completed, T.t_date,
  DATEDIFF(CURRENT_DATE, T.t_date) AS days_past, 
  IFNULL(SUM(S.shares), 0) AS subtrans_shares,
  T.shares - IFNULL(SUM(S.shares),0) AS due_shares,

  (SELECT IFNULL(SUM(IF(SO.t_type = 'sell', -SO.shares, SO.shares )), 0) 
  FROM subtransactions AS SO WHERE SO.symbol = T.symbol) AS owned_shares

  FROM transactions AS T
  LEFT OUTER JOIN subtransactions AS S
  ON T.id = S.transid     
  GROUP BY T.id
  ORDER BY T.position) AS result2

  WHERE MAXid = id
  • 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-19T12:47:07+00:00Added an answer on May 19, 2026 at 12:47 pm

    Your code:

      (SELECT MAX(T.id) AS MAXid
      FROM transactions AS T        [<--- here ]
      GROUP BY T.position
      ORDER BY T.position) AS result1,
    
      (SELECT T.id AS id, T.symbol, T.t_type, T.degree, T.position, T.shares, T.price, T.completed, T.t_date,
      DATEDIFF(CURRENT_DATE, T.t_date) AS days_past, 
      IFNULL(SUM(S.shares), 0) AS subtrans_shares,
      T.shares - IFNULL(SUM(S.shares),0) AS due_shares,
    
      (SELECT IFNULL(SUM(IF(SO.t_type = 'sell', -SO.shares, SO.shares )), 0) 
      FROM subtransactions AS SO WHERE SO.symbol = T.symbol) AS owned_shares
    
      FROM transactions AS T     [<--- here ]
    

    Notice the [<---- here ] marks I added to your code.

    The first T is not in any way related to the second T. They have the same correlation alias, they refer to the same table, but they’re entirely independent selects and results.

    So what you’re doing in the first, uncorrelated, subquery is getting the max id for all positions in transactions.

    And then you’re joining all transaction.position.max(id)s to result2 (which result2 happens to be a join of all transaction.positions to subtransactions). (And the internal order by is pointless and costly, too, but that’s not the main problem.)

    You’re joining every transaction.position.max(id) to every (whatever result 2 selects).

    On Edit, after getting home: Ok, you’re not Cartesianing, the “where MAXid = id” does join result1 to result2. But you’re still rolling up all rows of transaction in both queries.

    So you’re getting a Cartesian join — every result1 joined to every result2, unconditionally (nothing tells the database, for example, that they ought to be joined by (max) id or by position).

    So if you have ten unique position.max(id)s in transaction, you’re getting 100 rows. 1000 unique positions, a million rows. Etc.

    When you want to write a complicated query like this, it’s a lot easier if you compose it out of simpler views. in particular, you can test each view on its own, to make sure you’re getting reasonable results, and then just join the views.

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

Sidebar

Related Questions

The following query works as long as I can add DataRelations. However, the relation
I have the following query That gets the city name, city id, the region
the following query is used in my Wordpress blog, it gets the categories that
The following query gets me a column of distances. But what i need is
The following query takes more than 3 minutes to run because tables contain massive
So I have the following query which gets a Duplicate PK error what am
I have the following query that when executed, it just runs for hours and
I'm writing a stored procedure that gets data for a whole screen. How can
I need help with a raw SQL query which gets a value based on
The following query gets all the results from a table called video SELECT video.*

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.