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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:18:10+00:00 2026-05-19T00:18:10+00:00

I am working with a poorly designed database that I am not at liberty

  • 0

I am working with a poorly designed database that I am not at liberty to restructure. In this database, there are three tables (let’s call them ‘companiesA‘, ‘companiesB‘, and ‘items‘) that are involved in a query that I need to optimize. ‘companiesA‘ and ‘companiesB‘ describe companies in the same way in that the column values are the same, but they represent two different groups of companies and have different column names. Essentially, the ID and company name columns are ‘aID‘ and ‘aName‘ in ‘companiesA‘, and ‘idB‘ and ‘nameB‘ in ‘companiesB‘. ‘items‘ contains a column, ‘companyID‘, that contains a foreign key value from one of the two company tables.

The query I need to optimize gets a page’s worth of company IDs and names from the union of the two tables, sorted by the names column, with an added column that states whether the row’s company has any items associated with it. This query can also filter by the company names if the user requests it in the front-end. In its current state, I think it runs in THETA(companies * items) time, which is prohibitively slow:

select
  a.aID as companyID,
  a.aName as companyName,
  (select
     count(companyID)
   from
     items
   where
     companyID = a.aID
  ) as items
from
  companiesA as a
where
  a.aName like '%<string>%'

union

select
  b.idB as companyID,
  b.nameB as companyName,
  (select
     count(companyID)
   from
     items
   where
     companyID = b.idB
  ) as items
from
  companiesB as b
where
  b.nameB like '%<string>%'

order by
  companyName ASC
limit
  [optional_starting_index, ] 50;

It is not important that the items column contain the actual counts as this query returns (it was the only way I could figure out to cleanly return a value regarding the entire ‘items‘ table). I suppose that I can count myself fortunate that with 1500 companies and 9000 items, this algorithm only takes seven seconds.

If I were writing this in another language in which I had access to the tables myself, I could easily write this in O(companies + items) time, but I am finding it difficult to figure out how to do so in MySQL. Is it possible to do this, preferably without stored functions or procedures? I CAN add them if necessary, but I have had a hard time adding them through phpMyAdmin now that the server’s host only allows that interface to access the database by GUI.

  • 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-19T00:18:11+00:00Added an answer on May 19, 2026 at 12:18 am

    In this solution, I took the daring assumption that the company names in each of the tables are unique by using Union All. If they are not, then you can switch back to Union but you’ll get the performance hit of making the list unique. Basically, I’m eliminating your need for correlated subqueries to return the counts by using derived tables.

    Select Companies.CompanyID, Companies.CompanyName
        , Coalesce(ItemTotals.ItemCount,0) As ItemCount
    From    (
            Select a.aID As CompanyID, a.aName As CompanyName
            From companiesA As a
            Where a.aName Like '%<string>%'
            Union All
            Select b.IDB, b.nameB
            From companiesB As b
            Where b.bName Like '%<string>%'
            ) As Companies
        Left Join   (
                    Select companyID, Count(*) As ItemCount
                    From items
                    Group By companyID
                    ) As ItemTotals
                On ItemTotals.companyID = Companies.CompanyID
    Order By Company.CompanyName
    

    Here is another variant. This one is similar to your original except that I replaced the correlated subqueries with two Group By queries. As before, if the names and IDs between the two tables are mutually exclusive, you can use Union All otherwise you will need to use Union.

    Select Z.CompanyId, Z.CompanyName, Z.ItemCount
    From    (
            Select A.companyID, A.aName As CompanyName
                , Count(I.CompanyID) As ItemCount
            From companiesA As A
                Left Join items As I
                    On I.CompanyId = A.CompanyId
            Where A.aName Like '%<string>%'
            Group By A.companyID, A.aName
            Union All
            Select B.companyID, B.bName, Count(I.CompanyID)
            From companiesB As B
                Left Join items As I
                    On I.CompanyId = B.CompanyId
            Where B.bName Like '%<string>%'
            Group By B.companyID, B.bName
            ) As Z
    Order By Z.CompanyName  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.