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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:08:49+00:00 2026-05-21T12:08:49+00:00

I have two queries that each, on their own, run pretty quickly (less than

  • 0

I have two queries that each, on their own, run pretty quickly (less than 2 seconds). However, when I try to join them as subqueries, it runs ridiculously slowly. The last time I ran it it took about 68 seconds. Here’s the full query:

SELECT t.count,
       t.total
  FROM (SELECT t.account_number,
               COUNT(t.id) count,
               SUM(t.amount) total,
               ib.id import_bundle_id
          FROM import_bundle ib
          JOIN generic_import gi ON gi.import_bundle_id = ib.id
          JOIN transaction_import ti ON ti.generic_import_id = gi.id
          JOIN account_transaction t ON t.transaction_import_id = ti.id
          JOIN transaction_code tc ON t.transaction_code_id = tc.id
         WHERE tc.code IN (0, 20, 40)
      GROUP BY t.account_number) t
  JOIN (SELECT a.account_number,
               np.code
          FROM import_bundle ib
          JOIN generic_import gi ON gi.import_bundle_id = ib.id
          JOIN account_import ai ON ai.generic_import_id = gi.id
          JOIN account a ON a.account_import_id = ai.id
          JOIN account_northway_product anp ON anp.account_id = a.id
          JOIN northway_product np ON anp.northway_product_id = np.id
         WHERE np.code != 'O1') a ON t.account_number = a.account_number

That this query should run slowly is not a total surprise. If these were two separate tables and not subqueries, I would put indexes on their account_number columns. However, it’s obviously not possible to put indexes on query results, so I can’t do that. I suspect that’s part of the problem.

Aside from that, I don’t understand why the query is slow and I don’t have any ideas on how to speed it up, other than adding two summary tables, which I don’t want to do if I don’t have to.

By the way, this query in English might be, “Get all the POS transactions (codes 0, 20 and 40) for accounts that aren’t overdraft protection accounts (code O1).”

  • 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-21T12:08:50+00:00Added an answer on May 21, 2026 at 12:08 pm

    Put the check into the main query:

    SELECT  t.account_number,
            COUNT(t.id) count,
            SUM(t.amount) total,
            ib.id import_bundle_id
    FROM    transaction_code tc
    JOIN    account_transaction t
    ON      t.transaction_code_id = tc.id
    JOIN    transaction_import ti
    ON      ti.id = t.transaction_import_id
    JOIN    generic_import gi
    ON      gi.id = ti.generic_import_id
    JOIN    import_bundle ib
    ON      ib.id = gi.import_bundle_id
    WHERE   tc.code IN (0, 20, 40)
            AND t.account_number NOT IN
            (
            SELECT  anp.id
            FROM    account_northway_product anp
            JOIN    northway_product np
            ON      np.id = anp.northway_product_id
            WHERE   np.code = '01'
            )
    GROUP BY
            t.account_number
    

    Create the following indexes:

    transaction_code (code)
    account_transaction (transaction_code_id)
    account_transaction (account_number)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two SQL queries that I'm running from a C# winform, and I
I have a couple of queries that I need to run one to a
I am trying to create a query that will show the details for each
I have two tables, jos_eimcart_customers_addresses and jos_eimcart_customers. I want to pull all records from
My two questions are: Can I use clustered indexes to speed up bulk inserts
I have an app where users get points for actions they perform - either
I have a Car entity with a many-to-one relationship with an entity Owner .
Back-Story On a current project, I am using MySQL and SQLite in combination with
In a typical many-many arrangement like this... Movies Actors Movies_Actors ------ ------ ------------- movie_ID
I am using SQL Server 2005. SEE END OF THIS POST REGARDING BAD TABLE

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.