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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:43:42+00:00 2026-06-01T04:43:42+00:00

We have an in-house PHP web application that is a financial analysis tool for

  • 0

We have an in-house PHP web application that is a financial analysis tool for our users. We process customers’ financial information to analyze their ability to pay back a loan.

We have built a class that has functions to generate lots of numbers and ratios. We use these to display totals, ratios, etc. throughout the different pages in the application. We have lots of functions that generate totals and lots of functions that use the totals from functions to generate new totals and ratios. Here’s an example:

//Coverage Ratio
public function CoverageRatio($hma) {
    return div_check(($this->NetFarmIncome($hma) + $this->NetNonFarmIncome($hma) - $this->FamilyLivingExpenses() - $this->IncomeTaxes()), ($this->InterestPortionTermDebt($hma) + $this->PrincipalPortionTermDebt($hma)));
}

//Operating Expense Ratio
public function OperatingExpenseRatio($hma) {
    return div_check($this->FarmOperatingExpenses($hma), $this->GrossFarmIncome($hma));
}

//Net Nonfarm Income
public function NetNonfarmIncome($hma =  null) {
    $result = $this->db->query("SELECT ah.id, COALESCE(v1.h1, 0) - COALESCE(v2.h2, 0) AS h, COALESCE(v1.p1, 0) - COALESCE(v2.p2, 0) AS p, COALESCE(v1.a1, 0) - COALESCE(v2.a2, 0) AS a FROM analysis_history ah LEFT JOIN (SELECT analysis_id, ... GROUP BY analysis_id) AS v2 ON v2.analysis_id = ah.id WHERE ah.id = $this->analysisid");
    $row = $result->fetch_assoc();
    return $row['a'];
}

//Net Business Income
public function NetBusinessIncome($hma = null) {
    $result = $this->db->query("SELECT ah.id, COALESCE(v1.h1, 0) - COALESCE(v2.h2, 0) AS h, COALESCE(v1.p1, 0) - COALESCE(v2.p2, 0) AS p, COALESCE(v1.a1, 0) - COALESCE(v2.a2, 0) AS a FROM analysis_history ah LEFT JOIN (SELECT analysis_id, ... GROUP BY analysis_id) AS v2 ON v2.analysis_id = ah.id WHERE ah.id = $this->analysisid");
    $row = $result->fetch_assoc();
    return $row['a'];
}

Over the last year, lots of changes have been made and we’ve had to add lots of new totals and ratios. After modifying our application to generate the page load time and count the number of queries that are executed, we were shocked. Our largest page takes .8 seconds to generate and has 122 queries.

We’ve made some adjustments to decrease the number of queries, but we were only able to shave off about 10 queries total. Our average page has about 35 queries which we feel is just too much.

In our class which holds the analysis logic, should we change the functions that pull data from multiple functions (and make several calls to the database) and just have them perform the single query? In my CoverageRatio example about, every function in it actually queries the database–6 separate queries.

Which is the best way to better optimize our code? Is it fine the way it is–we like it this way just in case we need to make a change–we only change the function that generates that particular total. It would become a nightmare to have to manage 14+ functions that all use a net income calculation–if we ever had to modify how net income is calculated, we would have to change every query in every function that relies on that value. Thanks for your help!

  • 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-01T04:43:43+00:00Added an answer on June 1, 2026 at 4:43 am

    If you’re computing multiple things from the same set of tables, it’d be more efficient to do it with a single SELECT statement that does all the computations in separate columns, rather than a separate SELECT statement for each computation.

    You might also be able to improve performance by using temporary tables to hold intermediate results that are needed by multiple other queries, to avoid recomputing those values in each query that uses them.

    If your queries involve a lot of joins, use EXPLAIN to study how the database is performing those joins, and see if there are any indexes you could add to make the joins more efficient. A few well-chosen indexes may make a big difference.

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

Sidebar

Related Questions

We have an in house winform application that is used by about 20 users
I have contact management / CRM application used in-house by our company, It is
I have a web-form that is getting customer information. The form is processed using
So I'm building a web application and I have an ajax request that pings
I have a prosperity application written in-house in PHP for collaboration (Basecamp knock off).
I've been thinking about the number of projects we have in-house that are still
I have an In-house windows form app that I would like to use Spell
We have a software house developing code for us on a project, .NET Web
I have a PHP site with a mySQL database that I'm working on. I'm
I have a simple php site that I built, but for some reason my

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.