Gilles Roy
|
Re: QDB - Precompiled vs. Not-Precompiled Database Queries
|
Gilles Roy
02/12/2010 10:53 AM
post47227
|
Re: QDB - Precompiled vs. Not-Precompiled Database Queries
> - How much faster is a precompiled database query?
I think it depends a lot on the complexity of the query. I'm sure a prepared statement is always faster, however note
that if you were just to run it once you would have to do multiple API calls, one to prepare it, one to execute it and
one to free it.
> - How much memory does the QDB need to hold one precompiled query?
It depends on the complexity of the statement. I think it can hold several kilobytes of memory for some statements.
> - What is the overall limit of precompiled queries?
I don't think there is any limit above how much memory they would consume.
> - Would you recommend to free not reused precompiled database queries manually or not?
In the MME, we create a wrapper function which we pass the prepared statement ID into. If there is no prepared statement
for this ID, it goes ahead and creates one (it will be reused next time the same ID is passed in). If we don't need to
hold on the ID, we pass in a special ID called FREE_AFTER_USE and the wrapper function frees the prepared statement once
it has the result.
In general, I'd say always use prepared statements. We use them for almost everything in the MME, even sometimes
statements we execute only once. The main benefit is that it also prevents issues with strings not being escaped
properly (i.e. when using normal statements you have to take care to use %q and %Q properly, in some cases a %s is a
mistake).
qdb_statement() is more useful if you are building dynamic statements that are always different and that you run
frequently. In those cases you might not want the overhead of preparing/executing/freeing a normal prepared statement.
|
|
|