MyBB Community Forums

Full Version: Reduce queries postbit
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have this plugin that shows how many times the post was given reputation but it creates 10 additional queries! How can I reduce the amount of queries. Is there a way to use maybe cache?

Since I don't know much about cache could you help?

$query = $db->query("
            SELECT r.pid, r.uid, COUNT(r.pid) AS reputation
            FROM ".TABLE_PREFIX."reputation r
            LEFT JOIN ".TABLE_PREFIX."posts p ON (r.pid=p.pid)
            LEFT JOIN ".TABLE_PREFIX."users u ON (p.uid=u.uid)
            WHERE r.pid = ".intval($post['pid'])."
            group by r.pid ORDER BY reputation DESC
        ");
    while($row = $db->fetch_array($query))
    {
eval("\$post['test'] = \"".$templates->get("postbit_test")."\";");        
}
}
Where does this query go? What hook are you using? You can reduce that down to 1 query if you store $post['pid'] in an array, and at the final loop you perform a query like :

"WHERE r.pid IN ('pid1', 'pid2', .... 'pid10')"

http://www.w3schools.com/sql/sql_in.asp
Thanks very much for taking time to help me.


I created a plugin that is hooked to postbit and I use the query above to fetch how many times a particular post was given reputation and then I display the value in the post itself.

But the problem is the page has 10 posts and only one or 2 have reputation however the plugin still generates 10 queries.
(2014-09-19, 11:23 AM)TheGarfield Wrote: [ -> ]Where does this query go? What hook are you using? You can reduce that down to 1 query if you store $post['pid'] in an array, and at the final loop you perform a query like :


"WHERE r.pid IN ('pid1', 'pid2', .... 'pid10')"

http://www.w3schools.com/sql/sql_in.asp

Or you could just global $pids Wink I use it in multiple plugins. here's an example:

global $pids;

static $myVar;
                
if (!is_array($myVar) && $mybb->input['method'] != "quickreply")
{
    $myVar = array();
    $query = $db->write_query("SELECT p.uid, x.valid, x.gamertag, x.game1, x.game2, x.game3 FROM ".TABLE_PREFIX."posts p INNER JOIN ".TABLE_PREFIX."xboxlive x ON (p.uid = x.uid) WHERE ".$pids);
                
    while ($row = $db->fetch_array($query)) {
        $myVar[$row['uid']] = $row;
    }               
} else {
    if (!is_array($myVar)) {
        $myVar = array();
    }

    $query = $db->simple_select('xboxlive', '*', 'uid = \''.$post['uid'].'\'');
    $myVar[$post['uid']] = $db->fetch_array($query);
}

Obviously it needs updating for the query you wish to run and the data you are fetching, but this is my general approach.
@Euan T buddy could you help me please to combine your code with mine cause my php knowledge is terrible.
http://php.net

I'm not going to spoon feed you code. I've said it before and I'll say it again: I seriously advise you read up and learn PHP properly rather than patching bits together and continuously asking for support when you inevitably encounter problems.
How do you expect me to learn if nobody wants to give me good support? I need to see working code and then figure how it was done that's what I call learning thanks for reminding me!

P.S. Tried $pids and it returns pid IN('74','77','1023','1038','1207','1594','1612','4488','4788','4874') which seems to be corresponding to post pids. But now how to count the same pins in the reputation table?

The problem is that I have it hooked to $post and it generates 1 query per post but since there are 10 posts!
(2014-09-19, 12:22 PM)marcus123 Wrote: [ -> ]How do you expect me to learn if nobody wants to give me good support?

You don't understand the point of learning.

Checking ready-made houses will never make you a builder, eating ready-made food will never make you a cook, requesting and using ready-made codes will never make you a developer.

Then the next problem is that you don't pay for 'the ready-made food', you don't share it with anyone either. You ask us, volunteers, to write codes which end up being only for your own use as they're parts of something bigger for your forum, so I'm not wondering people are less and less likely to answer your multiple 'development' threads.

Yet, despite that, Euan showed you a complete example. The only thing you need to do is making it work with your own query. Why don't you at least try that and show us your result? That would be a step forward. PHP is really not a black magic.
The reason I don't share my entire code is simple. Since I don't entirely understand PHP I don't wanna end-up using some code that is public and will compromise MYBB forum!

Can I please ask a question?

How to add a variable to cache? I would like to create my own variable then use it across the board simply by globalizing it something like $pids but my own?
Hey Marcus Smile

Okay here's the deal, I think the reason why MyBB's moderators have created the "Plugin Development" forum is for people to post threads about plugins they're developing, and gonna release for free, but need like collecting ideas, beta testers, collaborators and stuff like that.
And the "Plugin Support" forum, is for people having trouble with plugins already released.

On the other hand, I think what bothers the team is that you only come here asking for code, and then fly. Actually I've tried one time to kinda "show" you how to read a MyBB piece of code and understand it, there are thousands of MyBB plugins, and because of the richness of those plugins, there's practically a 0% chance that you can't find your answer in one plugin or the other Smile .

From now on I will let you rely on yourself, there's no better method to understand coding, then when you produce code yourself. Asking on Stackoverflow or here on MyBB is really not gonna help you understand PHP. As Euan linked you to http://www.php.net, they have the most amazing documentation on PHP ever, they even won awards because they had the best documentation for a programming language. Even if you have never coded before, you will understand that strlen function for example, calculate the length of a string ( http://us.php.net/manual/en/function.strlen.php ).

To go back to your question about Cache. Almost 20% of MyBB's plugins use cache, you can get the $cache variable in any hook just by doing :

    function my_own_function() {
        global $cache;
    }

Now you have access to MyBB's caching system, but I won't tell you how to use that, instead, I can tell you that if var_dump it, it will tell you that it's an object, instantiated from a "datacache" class, and if you look in MyBB's code to find that class, any editor (even Notepad++), will tell you that the datacache class can be found in :
inc/class_datacache.php

Upon every method in that file, there is a small definition, read that a bit and you'll certainly find your way on how to read, update or delete your own cache.

Use all the hints you have Wink
Pages: 1 2