简体   繁体   中英

Are date calculations faster in PHP or MySQL?

A while back a database administrator mentioned to me that some server-side programmers don't utilize SQL as often as they should. For instance, when it comes making time-based calculations, he claims that SQL is better suited.

I didn't give that much consideration since it didn't really affect what I was doing. However, now I am making considerable time-based calculations. Typically, I have used PHP for this in the past. For the sake of performance, I am curious as to whether SQL would be more efficient.

For example, these are some of the tasks I have been doing:

        $todaysDate = date("d-m-Y");
        $todayStamp = strtotime($todaysDate); //Convert date to unix timestamp for comparison
        $verifyStamp = strtotime($verifyDate); //Convert submitted date to unix timestamp for comparison

        //The date comparison
        if((strtotime($lbp) <= $verifyStamp) && ($verifyStamp <= $todayStamp)){
            return true;
        }
        else {
            $invalid = "$verifyDate is outside the valid date range: $lbp - $todaysDate.";
            return $invalid;            
        }

The variables aren't that important - it's just to illustrate that I am making comparisons, adding time to current dates, etc.

Would it be beneficial if I were to translate some or all of these tasks to SQL ? Note that my connection to my database is via PDO and that I usually have to create a new connection. Also, my date calculations typically will be inserted into a database. So when I say that I'm making comparisons or adding time to a current date, I mean that I'm making these calculation before adding whatever results from them to a query:

ie $result = something...INSERT INTO table VALUE = $result

The calculations could just as easily be INSERT INTO table VALUE = DATE_ADD(...

Any input is appreciated.

The overhead of talking to the database would negate any and all advantages it may or may not have. It's simple: if you're in PHP anyway, do the calculations in PHP. If the data you want to do calculations on is in the database, do it in the database. Don't transition between systems just because unless you can really proof that it saves you a ton of time to do so (most likely it doesn't). What you're showing is child's play in either system, it hardly gets any faster as it is.

Well when you consider SQL with any of the programming language, then using SQL is more preferable for calculations than any other language.

If you consider Php and SQL then I would like to tell you what I have realized from my analysis..

The PHP architecture is a client-server architecture, that is Client sends a HTTP-Request to the Server and the server responds back to the client with HTTP-Response

One the backside of the server, the server generates a simple HTML Format page which is static that page is generated using the dynamic codes of PHP on the server.

Now the total time is:

HTTP-Request + SQL-Query + Fetching data from SQL Query + Data Manipulation of SQL Data + Php-to-HTMLGeneration + HTTP-Response

But if in case you use the calculations to be done within the SQL Query itself then the time for Data Manipulation of SQL in php would be saved. As the Php would have to deal with the datas explicitly.

So the total time would be:

HTTP-Request + SQL-Query + Fetching data from SQL Query + Php-to-HTMLGeneration + HTTP-Response

This may look almost equal if you are dealing with less amount of data. But for an instance if you are dealing with 1000 of rows in one query then a loop in php which would run 1000 time would be more time consuming than running a single query which would calculate the complete 1000 row in just one command.

One thing to consider is how many date calculations you are performing and where in the query your conversion is taking place. If you are searching a DB of 10 million records and you are converting a DateTime field into a Unix Timestamp inside of a WHERE clause for every single record and only ending up with 100 records in the query result it would be less efficient to use SQL to perform that conversion on 10 million records than it would be to use PHP to convert the DateTime object into a Timestamp on only the resulting 100 records.

Granted, only the result of 100 records would be converted anyway if you put the conversion in the select statement so it would be pretty much the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM