MySQL PHP PDO prepared statements - performance issues vs security

前端 未结 4 1686
一个人的身影
一个人的身影 2021-01-12 14:32

I am thinking of rewriting some open-source application for my purposes to PDO and transactions using InnoDB (mysql_query and MyISAM now).

My question is: Which case

4条回答
  •  有刺的猬
    2021-01-12 15:17

    I think this falls in the "premature optimization" category.

    How significant is the overhead? Have you measured it? Does it affect your server performance at all?

    Odds are it doesn't.


    On the plus side, you have an undeniable gain in terms of security (which should be a major concern for any internet-based shop).

    On the downside, you have the risk that it might affect performance. In the link you provided, it shows that poorly implemented PDO preparation results in slightly lower performance than non prepared statement in some circumstances. Performance difference on 5000 runs is 0.298 seconds.

    Insignificant. Even more so when you realize that the "non prepared" queries are run without the input sanitizing routines that would be required to make them safe in a live environment. If you don't use the prepared queries, you need some form of input sanitizing to prevent SQL attacks, and depending on how it is done, you may need to massage back the result sets.

    Bottom line, there is no significant performance issue, but there is a significant security benefit. Thus the official recommendation of using prepared statements.

    In your question, you speak of "the common eshop". The "common eshop" will never have enough traffic to worry about the performance issue, if there is one. The security issue on the other end...

提交回复
热议问题