duangua6912 2013-06-17 20:03
浏览 49
已采纳

与AJAX异步使用MySQL的性能问题

This isn't really a problem per se, but more of a question about efficiency.

Basically, I have an index.php file that handles the main code for the site I'm building. Within that code are several JavaScript/JQuery functions that use $.ajax() to handle data using other php files.

There are two functions I use that interact with a MySQL database (Note these aren't the fully implemented functions).

One for updating the database:

    function updateDB(text) {
        $.ajax({
                url: "update_db.php",
                type: "POST",
                data: {text:text},
        });
    }

and one for getting information from the database:

    function displayText() {
        $.ajax({
                url: "display_text.php",
                type: "POST",
                data: {},
        });
    }

At the top of both files ("update_db.php" and "display_text.php"), I include a .php file that connects to the database using PDO:

<?php require_once('connect_to_db.php'); ?>

However, this seems inefficient because the database is accessed twice when I run my index.php file, once for updating the database and once for displaying the contents of the database.

My question is this: is there any drawback to connecting to the database twice? My assumption is that this would bog down database operations and execute functions slower.

If there are drawbacks, is there a way to only connect to the database once even though the main index.php calls two different files that each have to connect to the database?

I'm mainly interested in learning how to write more efficient code, as well as get opinions on how to make things like file-handling more efficient. Any suggestions are welcome!

  • 写回答

3条回答 默认 最新

  • douou9094747 2013-06-17 20:08
    关注

    There is no real problem with connecting to the database twice (it is a must if you are doing it in two separate requests).

    You might be able to eliminate one of the requests from the page by having the update_db.php return the data that that display_text.php would normally send if update_db.php is always followed by a display_text.php call.

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统