<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>睡到25点 &#187; 重复评论</title>
	<atom:link href="http://www.voidman.com/tag/%e9%87%8d%e5%a4%8d%e8%af%84%e8%ae%ba/feed" rel="self" type="application/rss+xml" />
	<link>http://www.voidman.com</link>
	<description>个人博客，记录与分享，仅此而已。</description>
	<lastBuildDate>Fri, 30 Jul 2010 09:54:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>WordPress 糟糕的重复评论检测方式</title>
		<link>http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html</link>
		<comments>http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html#comments</comments>
		<pubDate>Thu, 22 Jan 2009 14:13:49 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Blog Related]]></category>
		<category><![CDATA[comment]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[重复评论]]></category>

		<guid isPermaLink="false">http://www.voidman.com/?p=231</guid>
		<description><![CDATA[去旧空间清理文件时，用 FTP 拉下一大堆 mysql slow queries 日志。由于我的博客平时几乎没什么人来访问，所以我也没怎么去关心是否存在 mysql slow queries。看了下日志，发现绝大部分 slow query 都是由 WordPress 的重复评论检测造成成的： 没想到居然直接用 Text 类型的 comment_content 字段来判断评论重复，太衰了。开始还怀疑是不是日志搞错了，于是找到相应的代码确认： // Simple duplicate check // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content) $dupe = &#34;SELECT comment_ID FROM $wpdb-&#62;comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' &#34;; if &#40; $comment_author_email &#41; $dupe .= &#34;OR comment_author_email = [...]]]></description>
			<content:encoded><![CDATA[<p>去旧空间清理文件时，用 FTP 拉下一大堆 mysql slow queries 日志。由于我的博客平时几乎没什么人来访问，所以我也没怎么去关心是否存在 mysql slow queries。看了下日志，发现绝大部分 slow query 都是由 WordPress 的重复评论检测造成成的：</p>
<p class="imgwrap">
<img src="http://img.voidman.com/wp/2009/01/wp_comment_mysql_slow_queries.png" alt="wp_comment_mysql_slow_queries" class="grayborder" style="padding: 2px"/>
</p>
<p>没想到居然直接用 Text 类型的 comment_content 字段来判断评论重复，太衰了。开始还怀疑是不是日志搞错了，于是找到相应的代码确认：<span id="more-231"></span></p>

<div class="wp_syntax"><pre class="php"><span style="color: #008000; font-style: italic;">// Simple duplicate check</span>
<span style="color: #008000; font-style: italic;">// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)</span>
<span style="color: #008080;">$dupe</span> = <span style="color: #ff00ff;">&quot;SELECT comment_ID FROM $wpdb-&gt;comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' &quot;</span>;
<span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span> <span style="color: #008080;">$comment_author_email</span> <span style="color: #800000;">&#41;</span>
  <span style="color: #008080;">$dupe</span> .= <span style="color: #ff00ff;">&quot;OR comment_author_email = '$comment_author_email' &quot;</span>;
<span style="color: #008080;">$dupe</span> .= <span style="color: #ff00ff;">&quot;) AND comment_content = '$comment_content' LIMIT 1&quot;</span>;
<span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span> <span style="color: #008080;">$wpdb</span>-&gt;<span style="color: #006600;">get_var</span><span style="color: #800000;">&#40;</span><span style="color: #008080;">$dupe</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span> <span style="color: #ff0000;">defined</span><span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'DOING_AJAX'</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span>
    <span style="color: blue;">die</span><span style="color: #800000;">&#40;</span> __<span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'Duplicate comment detected; it looks as though you<span style="">\'</span>ve already said that!'</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span>;
&nbsp;
  wp_die<span style="color: #800000;">&#40;</span> __<span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'Duplicate comment detected; it looks as though you<span style="">\'</span>ve already said that!'</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span>;
<span style="color: #800000;">&#125;</span>
<span style="color: #008000; font-style: italic;">// 上述代码位于 wp-includes/comment.php 文件的 wp_allow_comment 函数中</span></pre></div>

<p>有点奇怪 WP 为什么不考虑加个 comment_hash 字段，将评论内容 hash 一下，然后建个索引，那检测速度肯定杠杠滴。</p>
<h4>Related Posts</h4><ul class="related_post"><li><a href="http://www.voidman.com/2009/05/using-wordpress-plugin-api-hack-discuz.html" title="WordPress 的 Hook 机制在 Discuz 二次开发中的应用">WordPress 的 Hook 机制在 Discuz 二次开发中的应用</a></li><li><a href="http://www.voidman.com/2009/04/at-reply-mail-notification-1-release.html" title="At Reply Mail Notification 1.0 Release">At Reply Mail Notification 1.0 Release</a></li><li><a href="http://www.voidman.com/2009/03/lbs-to-wordpress.html" title="LBS 转 WordPress 不完全记录">LBS 转 WordPress 不完全记录</a></li><li><a href="http://www.voidman.com/2009/01/at-reply-mail-notification-test.html" title="At Reply Mail Notification 测试贴">At Reply Mail Notification 测试贴</a></li><li><a href="http://www.voidman.com/2008/11/automatically-delete-post-revisions.html" title="让 Wordpress 自动删除 Post Revisions">让 Wordpress 自动删除 Post Revisions</a></li><li><a href="http://www.voidman.com/2008/11/google-xml-sitemaps-ignore-the-specified-categories.html" title="让 Google Sitemaps Generator 可以忽略指定的日志分类">让 Google Sitemaps Generator 可以忽略指定的日志分类</a></li><li><a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html" title="Wordpress 分页文章静态化的更优解决方案">Wordpress 分页文章静态化的更优解决方案</a></li><li><a href="http://www.voidman.com/2008/07/upgraded-to-wordpress-v26.html" title="升级到 WordPress 2.6">升级到 WordPress 2.6</a></li><li><a href="http://www.voidman.com/2008/03/upgraded-to-wordpress-25-rc1.html" title="升级到 WordPress 2.5 RC1">升级到 WordPress 2.5 RC1</a></li><li><a href="http://www.voidman.com/2008/03/wordpress-theme-wpcandy-remix.html" title="WordPress Theme WPCandy Remix">WordPress Theme WPCandy Remix</a></li></ul><hr /><a href="http://www.voidman.com">Voidman.com</a> | Link: <a href="http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html">http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html</a><br /><img alt="linezing" width="1" height="1" src="http://img.tongji.linezing.com/288120/tongji.gif" />]]></content:encoded>
			<wfw:commentRss>http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
