dscdttg4389 2015-07-24 10:52
浏览 36
已采纳

连接数据库的功能的性能

I have a function to check wether a function/module is disabled by an admin or not.

The function looks like this (in functions.php):

function function_enabled($function_module) {
    include("dbconnect.php");
    if ($function_module == "changelogs" OR $function_module == "news" OR $function_module == "calendar" OR $function_module == "groups" OR $function_module == "settings" OR $function_module == "eat" OR $function_module == "weather" OR $function_module == "usertext" OR $function_module == "login") {
        $sql = $db->query("SELECT enabled FROM functions WHERE function_name = '$function_module'");
        $row = $sql->fetch_object();
        $return = $row->enabled;
        return $return;
    }
}

And this is how I use the function (in any other php file):

include("functions.php");
if(function_enabled("weather") == 1) {
    //do the weather stuff here
} else {
    echo"Function disabled";
}

Now my question: How is the performance of this function? Can this cause relative high performance hits? Is there a better/faster way to check wether a function is enabled or not or is this script okay?

UPDATE 1

Now, I combined these multiple queries into one so that all values are in an array.

function compare_id($a, $b) {
    if ($a['id'] == $b['id']) return 0;
    return ($a['id'] < $b['id']) ? -1 : 1;
}   

$sql = $db->query("SELECT function_name, enabled, id FROM functions");

$function_array = array();
while ($row = mysqli_fetch_assoc($sql)) {
    $function_array[] = $row;
}
usort($function_array, compare_id);

if($function_array[0]['enabled'] == 1) {
    echo"changelogs enabled<br>";
} else {
    echo"changelogs disabled<br>";
}

if($function_array[2]['enabled'] == 1) {
    echo"calendar enabled<br>";
} else {
    echo"calendar disabled<br>";
}

if($function_array[7]['enabled'] == 1) {
    echo"usertext enabled<br>";
} else {
    echo"usertext disabled<br>";
}

if($function_array[8]['enabled'] == 1) {
    echo"login enabled<br>";
} else {
    echo"login disabled<br>";
}

if($function_array[4]['enabled'] == 1) {
    echo"settings enabled<br>";
} else {
    echo"settings disabled<br>";
}

Is this method faster?

  • 写回答

2条回答 默认 最新

  • doudou1897 2015-07-24 11:19
    关注

    It looks OK. Is it giving you issues? There could be performance issues in the DB query from the table or index.

    Generally you shouldn't build for performance from the start. Rather build something that functions, then go back an optmise where the choke points are. There are high load webservices which use simple frameworks (Ruby on Rails, Laravel) They only have to optimise 2-5% of the code where the load is. When you know where the choke points are and why its choking you can make an educated choice on how to optmise. Should it be optimised, or should I rewite this in another language.

    Does your functions table change fequently? If not you could possibly export it to file. Then PHP could read it once and store it in Memory as an array. Referencing an array would be a lot faster functions['weather'] == 1 with no DB throughput issues.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持