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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:19:18+00:00 2026-06-11T18:19:18+00:00

I am currently trying to optimize a report using mysql and openreports for accounting

  • 0

I am currently trying to optimize a report using mysql and openreports for accounting use. Although I’m sure there are better tools for the job, these are what I have to work with at the moment. I am doing the following calculated fields:

client,
number of orders,
client cost,
client adjustments,
adjusted client price (client cost – client adjustments),
Vendor cost,
vendor adjustments,
adjusted vendor price (vendor cost – vendor adjustments),
margin ((client billed – vendor billed)/client billed),
adj margin ((adj client price-adj vendor price)/adj client)

The tables are quite large and I have to join 4 tables for each inner join. The report that was in place before has a union and about 20 nested selects in each side of the union all containing at least four joins and counts. Since I’m doing calculations based on previous fields, each nested select gets progressively larger containing all of the nested selects, counts, and calculations from the prior calculations. The query is now 400 lines and very expensive and taxing to the system especially on larger clients.

I realize there are better tools for the job but I was wondering what the ideal fix would be for the situation. Could I create a user defined variable within the query and use it later? Would this work if I had multiple queries per client? I can’t include the entire 400 line query in this post but I can supply any additional details that wold be helpful. Any insight would be greatly appreciated.

select office_1.name as 'Client'
,count(distinct(property_1.id)) as 'Total Billed Orders',
(select format(coalesce(sum(serviceprice_2.amount),0),2)
    from cap.service_price serviceprice_2
    inner join cap.service service_2 on service_2.id = serviceprice_2.service_id
    inner join cap.service_area servicearea_2 on servicearea_2.id = service_2.service_area_id
    inner join cap.property property_2 on property_2.id = servicearea_2.property_id
    inner join cap.office office_2 on office_2.id = property_2.client_id
    where serviceprice_2.price_context = 'CLIENT'
    and serviceprice_2.price_type_id = 236
    && service_2.date_client_billed between '2012-09-01' and now()
    and service_2.gl_code_ap='4700-70-000'
    and service_2.date_cancelled is null
    and office_2.id = office_1.id) as 'Client Price'
/* other calculations between here */
from cap.service service_1
inner join cap.service_area servicearea_1 on servicearea_1.id = service_1.service_area_id
inner join cap.property property_1 on property_1.id = servicearea_1.property_id
inner join cap.office office_1 on office_1.id = property_1.client_id
inner join cap.office vendor_1 on vendor_1.id = service_1.vendor_id
where 
service_1.date_client_billed between '2012-09-01' and now()
and service_1.date_cancelled is null
and office_1.id = 26377 
and service_1.gl_code_ap='4700-70-000'
group by office_1.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-06-11T18:19:20+00:00Added an answer on June 11, 2026 at 6:19 pm

    Using views can simplify the query although there is no effect on performance.

    create view view1 as
    select *
    from 
        cap.service_price serviceprice_2
        inner join cap.service service_2 on service_2.id = serviceprice_2.service_id
        inner join cap.service_area servicearea_2 on servicearea_2.id = service_2.service_area_id
        inner join cap.property property_2 on property_2.id = servicearea_2.property_id
        inner join cap.office office_2 on office_2.id = property_2.client_id
    ;
    create view view2 as
    select *
    from 
        cap.service service_1
        inner join cap.service_area servicearea_1 on servicearea_1.id = service_1.service_area_id
        inner join cap.property property_1 on property_1.id = servicearea_1.property_id
        inner join cap.office office_1 on office_1.id = property_1.client_id
        inner join cap.office vendor_1 on vendor_1.id = service_1.vendor_id
    

    Now the query is more manageable:

    select @client_price := format(coalesce(sum(serviceprice_2.amount),0),2)
    from view1
    where serviceprice_2.price_context = 'CLIENT'
        and serviceprice_2.price_type_id = 236
        && service_2.date_client_billed between '2012-09-01' and now()
        and service_2.gl_code_ap='4700-70-000'
        and service_2.date_cancelled is null
        and office_2.id = office_1.id
    ;
    select 
        office_1.name as 'Client'
        ,count(distinct(property_1.id)) as 'Total Billed Orders',
        @client_price
    /* other calculations between here */
    from view2
    where 
        service_1.date_client_billed between '2012-09-01' and now()
        and service_1.date_cancelled is null
        and office_1.id = 26377 
        and service_1.gl_code_ap='4700-70-000'
    group by office_1.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to optimize my program. I have a large database which consists
I currently have a website that I am trying to optimize in terms of
I'm currently trying this: using DocumentFormat.OpenXml.Packaging; using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileNameDocx as string, true))
I'm currently trying to optimize the sluggish process of retrieving a page of log
I have been trying to optimize some code which handles raw pixel data. Currently
I am currently trying to optimize some bobj reports where our backend is Teradata.
I am currently trying to optimize some DSP related code with Shark and found
We are currently trying to optimize the performance of an ASP.NET based website which
I am currently trying to learn how to use premake 4 in order to
I've got some OpenGL drawing code that I'm trying to optimize. It's currently testing

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.