|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Dec 03, 2012 4:38 am Post subject: PDO (PHP Data Objects) - caching prepared statements |
|
|
It's not a Smarty trick, but I think it's still worth sharing.
When you work with database, the performance increase from using prepared statements could be from mere 2% to 25% and more. But you often run into an issue of using statement call inside a function, making reuse of a prepared statement something of a problem.
Well, I've solved it the most straightforward way:
Code: | function get_page_details($id)
{
if(isset($GLOBALS['_pdo_query'][__FUNCTION__]))
{
$_query = $GLOBALS['_pdo_query'][__FUNCTION__];
}
else
{
$_query = $GLOBALS['_pdo']->prepare('SELECT s.*, t.tname, t.alias
FROM structure s, templates t
WHERE t.id = s.template_id
AND s.id = ?
LIMIT 1');
$GLOBALS['_pdo_query'][__FUNCTION__] = $_query;
}
if($_query->execute(array((int)$id)))
{
$result = $_query->fetch();
return $result;
}
return false;
} |
All prepared statememnts are stored in global scope, each associated with it's function name.
Functions, that use multiple statements (while they are few and far between, sometimes you need a few additional pieces from database before you can save a new row, or you need to alter 2 or more tables for one function call (a very bad practice - it makes tracking changes harder, but still happens sometimes)), save them with some __tail, clarifying the statement meaning. |
|
Back to top |
|
tron2k Guest
|
Posted: Wed Jan 16, 2013 2:26 am Post subject: |
|
|
How about putting the function into an object? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Apr 22, 2013 12:30 pm Post subject: |
|
|
tron2k wrote: | How about putting the function into an object? |
What for? Just because? Why everyone think that objects are catch-all solution to life, universe and all that shit? |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|