More optimization for comment relish plugin

In my previous post about optimizing the comment relish plugin I managed to lower the load generated by the comment relish plugin on the database server by adding an index on a column in the cr_emailed table and by optimizing a query ( basically removing a "useless?" join ). This improved the load time a lot but some users still reported slow load times on blogs with a lot of comments.

Looking more over the source code I realized that the function that tried to find new commentators was executed on each page. This function was executing a mysql query that joined two tables wp_comments and wp_cr_emailed. I think you can imagine the result of this on blogs with a large number of comments.

The solution was to execute this function only when a new comment was posted. And this comes with two benefits:

  1. because this is called only when a new comment is posted ( or approved ) the rest of the blog will remain as fast as before the plugin was installed.
  2. because of the way we call the function ( as an action/hook associated with the code that processes the comments ) we have more information about the comment so we don't have to do the query where we join two tables. We still do one query to get the whole comment data and one to see if this is a new commentator ( not emailed yet ) but these use indexes and are really fast.

Get the new optimized plugin here and let me know how it works for you.

Update:

the plugin link above contains a plugin that tries to send email even when the blog receives a pingback or trackback as reported by Rhys. I have uploaded another version that corrects this problem here: Optimized Comment Relish plugin-0.3 (3.58 KB)

37 thoughts on “More optimization for comment relish plugin

  1. Thank you for this, I guess I should’ve explained myself better.

    On my old theme, and the old version of comment relish, for reasons you identified in that post, my blog would crash if somebody sent me a trackback. As a result, I managed to edit it so that trackbacks didn’t activate the code like comments did.

    Excellent work by the way!

  2. Very nice patch (and much needed, jeez, looking for new commenters on each page load?? silliest stuff ever)

    A few more stuff to fix for you!

    - It still misses some stripslashes (on line 288 and when outputting the options in the admin page, otherwise you see plenty of “I\’m happy you\’ve left a comment”

    - it’s full of PHP short tags (<? instead of <?php) which will make the plugin break everything on purist setups.

    1. Thanks for the suggestions. I will release another patch that fixes the stripslashes thing. As for the short tags, I never encountered such a problem.

  3. So installed v3 on a test instance of my site and left 4 comments. I got 4 welcome messages – anyone experienced this as well?

    cheers,

    Zac

  4. The problem is most likely because the plugin was not able to create the cr_emailed table. deactivate the plugin and ( If you can access wordpress database ) lookup in wp_options for cr_installed and set it to ‘false’ and then just reactivate the plugin. This should just create the table : cr_emailed. look for it in the database to make sure it’s there. If it’s there then you should not have that problem anymore.

  5. I was having the same problem as Zac, and I couldn’t get the plugin to create the table, so I just manually created it.

    Just replace the wp in wp_cr_emailed if you have a different wordpress prefix.

    CREATE TABLE wp_cr_emailed (
    emailed_ID mediumint(9) NOT NULL AUTO_INCREMENT,
    time bigint(11) DEFAULT ’0′ NOT NULL,
    email varchar(255) NOT NULL,
    UNIQUE KEY emailed_ID (emailed_ID),
    INDEX ( email )
    );

  6. great article. thanks for posting the optimization tips.

    I got a question though. Would it be a hard thing to do to add a delay into the email that relish sends out? Ive noticed that when someone new leaves a comment on my site, the welcome email I set up is sent out to them immediately. I would like to be able to put a delay of maybe 2 or 5 mins before the email is sent out.

    thanks

    1. You could do that if you would create a system to send emails in batches … right now the plugin hooks into the comment posting function so it would be unacceptable to make the user wait for 2-5 minutes before his comment is posted.

  7. So have your fixed all the bugs or is anyone experiencing more problems with this? If so, is there another available amended version of this plugin somewhere else that works correctly? Thanks for your help.

    1. The only problem that might exist but may now show up for everybody is that the plugin doesn’t create the database when you install it.
      You can check your wp db after you install it. If you see a table named {WP_PREFIX}_cr_emailed then it’s ok.If not then you should create the db manually by running this sql in a mysql client like phpmyadmin or whatever you use:

      CREATE TABLE {WP_PREFIX}_cr_emailed (
      emailed_ID mediumint(9) NOT NULL AUTO_INCREMENT,
      time bigint(11) DEFAULT 0 NOT NULL,
      email varchar(255) NOT NULL,
      UNIQUE KEY emailed_ID (emailed_ID),
      INDEX ( email )
      );

      WP_PREFIX is the prefix that all wordpress tables have. By default this is ‘wp’

  8. i tryed to create the db manually with the code:

    CREATE TABLE _cr_emailed (
    emailed_ID mediumint(9) NOT NULL AUTO_INCREMENT,
    time bigint(11) DEFAULT ‘0′ NOT NULL,
    email varchar(255) NOT NULL,
    UNIQUE KEY emailed_ID (emailed_ID),
    INDEX ( email )
    );

    but the response is:

    query SQL :

    CREATE TABLE _cr_emailed ( emailed_ID mediumint(9) NOT NULL AUTO_INCREMENT, time bigint(11) DEFAULT ‘0&email varchar(255) NOT NULL, UNIQUE KEY emailed_ID (emailed_ID), INDEX ( email ) )

    Messaggio di MySQL:

    #1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘0&email varchar(255) NOT NULL,
    UNIQUE KEY emailed_ID (emailed_ID),
    INDEX ( emai’ at line 3

    How can I fix this?
    THANKS

    1. My comment with indications about how to create the table was wrong. I used tags in it and didn’t realize wordpress would interpret the tags.
      Also this line:
      time bigint(11) DEFAULT ‘0′ NOT NULL,
      should have been simply:
      time bigint(11) DEFAULT 0 NOT NULL,

      Anyway please take a look again at the corrected comment, right above yours and try again.

    1. I just updated the link to the latest optimized version. the rest are not updated because I don’t want to encourage anyone to download broken versions.
      Thanks for the notice.

Leave a Reply