Jump to content

Recommended Posts

Really? It's an awful lot slower for me, locally. To be fair, I'm not using objects at all, I'm just doing SQL queries and then formatting or doing whatever I do with the results after. Clearly I should have chosen MySQLi instead, but I wanted to teach myself some things about PDO for future-proofing.

Link to comment
https://forums.phpfreaks.com/topic/278216-pdo-isnt-it-slow/#findComment-1431227
Share on other sites

I read before lots of posts of different forums on the web, where their "experts" said that PDO and MySQLi drivers are mutch more slower than MySQL one - it's not really true.

 

As kicken said - you're doing somethig wrong in your script.

Edited by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/278216-pdo-isnt-it-slow/#findComment-1431282
Share on other sites

You "close" the connection after each query? No no. Leave it open and let PHP close it for you. It's quite possible that PDO has coding where garbage collection frees the underlying connection while mysql's coding doesn't (especially considering how it's not object-oriented).

Link to comment
https://forums.phpfreaks.com/topic/278216-pdo-isnt-it-slow/#findComment-1431313
Share on other sites

based on your other recent thread about including a file containing your connection, you are apparently now calling a 'connect_db' function inside of each of your database related functions to open a connection and based on post #8 in this thread, closing that connection after you use it each time.

 

that's most definitely NOT how to manage a database connection in your application. creating a database connection takes a significant amount of time, so of course your code is noticeably slow.

 

the correct method is to create one database connection in your application code and pass it into each function that uses it. you would normally pass this in as a call time parameter, in the same way that the procedural mysqli_ functions accept the mysqli connection as the first call-time parameter in any function that needs it. there are other methods to pass a database connection object into functions, but they are not general purpose.

Link to comment
https://forums.phpfreaks.com/topic/278216-pdo-isnt-it-slow/#findComment-1431319
Share on other sites

So you're saying, then, that this code is inefficient, being that this is just one of quite a number of queries in my functions.php file.

$connection = connect_db(); //this is where I call the new PDO connection
    $album     = mysql_prep(rawurldecode($album));
    $performer = mysql_prep(urldecode($performer));
    $query  = $connection->prepare ("select title, length, year, album_artist, artist, orig_artist, track_number, label, coverpic, rating
                from songlist
             where album = :album and album_artist = :performer order by track_number asc");
    $query->bindParam(':album', $album);
    $query->bindParam(':performer', $performer);
    $query->execute();
    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {

but I'm not sure how to call the connection (as a call time parameter) any other way. I've tried putting the connection into my header and leaving it open, but that hasn't worked. Sorry if this is a basic question.

Link to comment
https://forums.phpfreaks.com/topic/278216-pdo-isnt-it-slow/#findComment-1431362
Share on other sites

It's a good albeit generally unnecessary practice to close it when you're completely done. Not after every query.

 

1. What's mysql_prep() doing? I'm not too confident you need it.

2. That's basically what your code should look like. Is connect_db() returning a new PDO object every time? That's the inefficient part. Create a PDO object the first time and reuse that every subsequent time; the Singleton pattern is typically how that happens.

3. I did find this that explicitly says the connection is closed once the object goes away (eg, with $connection=null).

Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.

 

Example #3 Closing a connection

<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
// use the connection here


// and now we're done; close it
$dbh = null;
?>

 

Link to comment
https://forums.phpfreaks.com/topic/278216-pdo-isnt-it-slow/#findComment-1431402
Share on other sites

Thanks for the reply. I'd forgotten all about mysql_prep, actually (a little function that checks which version of PHP and magic_quotes, etc). I commented it out and re-tested, but it's still slow. Your second point, though, I think must be the reason. I am creating a NEW PDO and referring to that with my function call connect_db(). I'm afraid I know no other way - what do I do to call a NEW PDO the first time, but not subsequently?

Link to comment
https://forums.phpfreaks.com/topic/278216-pdo-isnt-it-slow/#findComment-1431412
Share on other sites

You can modify connect_db() so it always returns the same object each time it's called.

function connect_db() {
	static $connection = null;
	if (!$connection) {
		$connection = new PDO(/* ... */);
		// and whatever else
	}
	return $connection;
}
That bit with static $connection (so PHP "remembers" $connection across calls) makes this a singleton.

 

Simply reusing $connection might be enough, but if that variable ever drops out of scope (ie, you use it inside a function or class method) then it'll get cleaned up.

 

On the down side this doesn't make it easy to close the connection, should you want to do that manually at some point. Like if you have a long-running script that only needs the database a little bit early on. If that case arises then you should switch to a full class implementation instead of the simple function above.

Link to comment
https://forums.phpfreaks.com/topic/278216-pdo-isnt-it-slow/#findComment-1431421
Share on other sites

Thank you for the explanation. I'd seen the term singleton but wasn't sure what it referred to. I've amended my connection.php and it's definitely quicker than before. I still wouldn't say it was anywhere near as fast as MySQL but it's not awkwardly slow any more. I really appreciate the time taken by all here in replying.

Link to comment
https://forums.phpfreaks.com/topic/278216-pdo-isnt-it-slow/#findComment-1431427
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.