我目前正在为较小的网站构建一个小型CMS。现在我想提取text_content
字段中的所有单词,并将它们存储在我的word
表中供以后分析。从SQL中的文本字段中提取单词
page(id int,
title varchar(45),
# ... a bunch of meta fields ...
html_content text,
text_content text);
word(page_id int, # Foreign key
word varchar(100)); # I presume there are no words longer than 100 chars
目前我正在使用下面的代码,对于较大的文本块运行速度非常缓慢(可以理解)。
// Sidenote: $_POST is sanitized above scope of this code.
$_POST['text_content'] = str_replace("\t", "",
htmlspecialchars_decode(strip_tags($_POST['html_content'])));
// text is in swedish, so we add support for swedish vowels
$words = str_word_count($_POST['text_content'], 1, "åäöÅÄÖ");
// Delete all previous records of words
$this->db->delete("word", array('page_id' => $_POST['id']));
// Add current ones
foreach($words as $word)
{
if (trim($word) == "")
continue;
$this->db->query("INSERT INTO word(page_id, word) VALUES(?, ?)",
array($_POST['id'], strtolower(trim($word))));
}
现在,我不满意这个解决方案。我正在考虑在数据库中创建一个触发器,它与php版本几乎完全相同。 是否有可能在MySQL中创建触发器来执行所述操作(如果有的话) - 如何?或者,还有更好的方法?我对此采取疯狂的方法吗?
谢谢。我不知道你可以这样做。现在执行45秒的时间现在在0.9秒内执行。这解决了我的问题:) – 2009-10-30 20:00:42
绝对。从客户端到服务器通信,每个查询都有相当大的开销。此外,blurb可以传递到一个存储过程,将其分解并执行插入操作 – 2009-10-30 20:01:45