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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:35:52+00:00 2026-05-19T23:35:52+00:00

I’m currently trying to optimize a bottleneck function that is called really ofen in

  • 0

I’m currently trying to optimize a “bottleneck” function that is called really ofen in an application.

In the application in question, options can be selected. But some options can be restricted by other. For example, when option “A” is selected, it restricts the selection of option “B”.

These restriction links are saved to a table which have this structure:

option_restrictions
    option_code_1    varchar(20)
    option_code_2    varchar(20)

Also some options are options packages. For exemple, option “Fruit” consist of the package of option “Apple”, “Orange”, “Kiwi”.

Those are defined this way in the table:

option_packages
    option_code        varchar(20)
    option_included    varchar(20)

Currently the “bottleneck” function do this:

  1. Check if 2 specified options restricts each other.
  2. If not, it checks if option 1 is an option package and if so, retreive all of it’s options and check if any is restricted by option 2.
  3. If not, it checks if option 2 is an option package and if so, retreive all of it’s options and check if any is restricted by option 1.
  4. If not it returns false (no restriction).

Here is the actual code I’m trying to optimize:

//Returns true if options restricts each other, false otherwise
function restriction($option_1, $option_2) {
    global $conn;

    //Pass 1 check is each option restrict themselves
    $sql_restriction = "select * from option_restrictions where (option_code_1 = '".$option_1."' or option_code_1 = '".$option_2."') and (option_code_2 = '".$option_1."' or option_code_2 = '".$option_2."')";
    $res_restriction = mysql_query($sql_restriction, $conn);
    if (mysql_num_rows($res_restriction)>0) {
        log_action('End restriction(' . $option_1 . ', ' . $option_2 . ')');
        return true;
    } else {
        //Pass 2 check if option 1 is a package and if so test it's options against option 2
        $sql_option_1_composante = "select * from option_packages where option_code = '".$option_1."'";
        $res_option_1_composante = mysql_query($sql_option_1_composante, $conn);
        if (mysql_num_rows($res_option_1_composante)>0) {
            while ($option_1_composante = mysql_fetch_array($res_option_1_composante)) {
                if (restriction($option_1_composante["option_included"], $option_2)) {
                    return true;
                }
            }
        }
        //Pass 3 check if option 2 is a package and if so test it's options against option 1
        $sql_option_2_composante = "select * from option_packages where option_code = '".$option_2."'";
        $res_option_2_composante = mysql_query($sql_option_2_composante, $conn);
        if (mysql_num_rows($res_option_2_composante)>0) {
            while ($option_2_composante = mysql_fetch_array($res_option_2_composante)) {
                if (restriction($option_2_composante["option_included"], $option_1)) {
                    return true;
                }
            }
        }
    }

    return false;
}

The recursive calls/loops are killing the system ATM… For a single page load this function can be called over 15,000 times which lead to page load of 30secs/4mins!

I’m trying to optimize as much as I can. I’ve tryed to merge pass 2 and 3 together and to remove the recursive call… Any ideas welcome!

FYI, I know this is maybe not the best design but this function is part of a leviathan system I have no control over nor the ressources/time to change ATM.

  • 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-19T23:35:53+00:00Added an answer on May 19, 2026 at 11:35 pm

    I can’t tell for sure without more detail, but to me this really sounds like a database design problem.

    If it really is a database design problem,

    • you’re probably not going to get very
      far optimizing the application code
    • but changing the structure could drop the load time dramatically, maybe even a couple of orders of magnitude

    This is the kind of problem (legacy system, not allowed to change it) that I’ve successfully attacked with a proof-of-concept test. I set up a server on my own computer. (Oracle XE, SQL Server Express, and PostgreSQL are all free nowadays.) Then I’d build tables and views that did all the grunt work.

    In your case, if you’re not a database designer, find one that’s willing to get involved in a skunk works project.

    Good luck. I mean that.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.