in reply to Re^3: Dynamic SQL
in thread Dynamic SQL

prepared statements with placeholders will likely be faster than constructing queries on the fly

I don't think rather doubt this is generally true. It would be nice to see it demonstrated.

Replies are listed 'Best First'.
Re^5: Dynamic SQL (prepare)
by tye (Sage) on Apr 07, 2015 at 03:47 UTC
    prepared statements with placeholders will likely be faster than constructing queries on the fly
    I rather doubt this is generally true. It would be nice to see it demonstrated.

    Running a ton of nearly identical queries can get a rather slight performance improvement from only having to prepare the statement once. But this is not particularly significant in the most extreme case and is of no significance at all if your queries are not rather trivial.

    On the other hand, in all 3 of my most recent jobs I've had to basically disable the "prepare" step as the poor query optimization that is done in the absence of the actual values is often horrible and can result in queries taking several orders of magnitude longer.

    So I've heard that newer versions of databases that I haven't used recently end up doing query planning twice, once at "prepare" time and then again once the values are known. I don't know how much that impacts the oft-touted "efficiency" of separate prepare/execute, but it surely means it has become even more insignificant.

    But the documentation says that it is faster so everybody repeats that, usually with much more emphasis than is warranted (and completely ignoring the much, much worse performance problems that can result).

    - tye        

Re^5: Dynamic SQL
by bitingduck (Deacon) on Apr 06, 2015 at 17:16 UTC

    If each prepared statement is used only once then, no, it won't be true. If each prepared statement is used a number of times then it will save the parsing time of the SQL queries each time a prepared statement is reused. The OP has given very limited examples and no sense of the number of possible unique (modulo the values that would go in the placeholders) queries

      If each prepared statement is used only once then, no, it won't be true.

      for instance.

      And then there is the fickleness of planning.

      But I can see it would be hard to set up a broad, convincing case.

        This is generally the case with any caching or memoizing strategy: if the cached results are never re-used, then caching is an overhead. But still, there are many many cases where caching really make sense, it can sometimes transform a quadratic (and sometimes even exponential) algorithm into a linear one. Sometimes it is easy enough to analyze to figure out; in other cases, only benchmark can tell.

        Je suis Charlie.