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条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序