与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!

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.

If you are using persistent connection, I don't think there will be an issue.

If you want to write more efficient code, try handling it all in 1 call at the end of the page with a single ajax call. Get then update (or whatever you need to do).

You can use persistent connection but it`s really bad idea you can check more details here: What are the disadvantages of using persistent connection in PDO

and if you think about performance you should think about lot of things like you should use include and not include_once or ... check here http://seeit.org/2010/06/11/php-the-include-include_once-performance-debate/

and for your problem i think, you should choose one of these solutions

  1. Use separate requests for using asynchronous advantages (for this solution i offer to use InnoDb engine that use row blocking and not use table blocking like MyIsam)
  2. Use single request for get minimum Mysql usage and for more information, connecting to mysql and select database in my server is take 50-300 miliseconds and you can save this time with single request.