For each Comment posted on my website, I am running a query on the article_comment_review table to see if the current logged-in member has already done a review or not. (I stick the results in an array.)
Originally my plan was to run one query that would bring back all Reviews for Article #1, and then stick them in an array. Then down in my HTML section, as I displayed each Comment, I would iterate through this array, and see if for the current Comment and current User, if there was a Review. If so, then I would hide the Survey, otherwise display it.
Follow me?
My fear is this…
If a given Article had 1,000 Comments, and 10,000 Users “reviewed” each Comment, then my array would be 10 Million records!!
And so that is what is behind my OP.
I suppose I could run a query for each Comment, but that seems more costly than to just return a recordset/array that is reasonably close to what I am checking.
If you use generators, you take practically no performance hit…you’d need a huge amount of data (or a reaally crappy server) to bog it down then.
Otherwise, it’s not iterating through an array that causes issues, it’s gathering the data that is slow (and what makes generators so fast in comparison to a regular loop).
For something like that, I would add a table for user reviews, with columns something like “user_id”, “comment_id” and when a user reviews a comment or whatever, add that user/comment ID combo to that table. Then you can do a quick check to see if a user has a reviewed that comment in a fashion similar to “SELECT * FROM reviews WHERE user_id = $user_id AND comment_id = $comment_id”. Of course you could also do a $comments_reviewed = “SELECT comment_id FROM reviews WHERE user_id = $user_id” to get a list of all comments that user has already commented on.
Lol don’t copy/paste any of that as it’s not functional… it’s just to give a general idea of the process.
That’s correct, if you are only intending to show that particular member’s comments.
Then when you want to show all comments, you reuse this table, and just omit the where the Reviewer is the current logged-in Member part.
Not fancy at all…you’re going to be foreach’ing the array anyway. Just remember, it’s the gathering and structuring of data that takes all the time.
But if you’re deadset against using one, then consider just paginating the data ( i.e., fetching 20 rows at a time, rather than all of it at once ) if having a lot of records being returned is a concern
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\
");
$result = $sth->fetchAll();
print_r($result);
?>
Well, when you grab results from the database, prepared statements or otherwise, you’re storing those results either in an object or an array to process them. To the best of my knowledge there isn’t much (if any) difference in overhead between objects and arrays. Both are just storing that data in memory while you work with it, clearing it either when you unset the obj/array or when the script ends. So as far as performance goes, storing it in an array should be just fine.