【问题标题】:too many results and slow results in mysql/php search enginemysql/php 搜索引擎中的结果太多且结果缓慢
【发布时间】:2012-04-10 11:41:05
【问题描述】:

好的,我可能会看到为什么我会得到很多结果,因为我使用了 MATCH 查询。我想要的是输出相关搜索。例如,如果搜索是“对 green it 感兴趣”,它只输出那些有兴趣的表中的结果、green 或 IT(如果有意义的话)。

现在我得到了所有的结果,而且它真的很慢。我不想使用 mysql/php 以外的任何东西来实现这一点,所以没有第三方搜索引擎,如 Lucene 或替代品。

这是搜索代码:

    <?php

mysql_connect("webhost", "user", "pass") or die(mysql_error());
mysql_select_db("replies") or die(mysql_error());

// Retrieve result using term
$term = $_POST['term'];

$sql = mysql_query("SELECT * from 'replies' `where interest_in_green_it` MATCH '%$term%' or `green_it_good_thing MATCH` '%$term%' or    `green_it_save_environment` MATCH '%    $term%' or  `green_it_save_money` MATCH '%$term%' or    `green_it_incentive` MATCH '%$term%' or `uel_green_it MATCH` '%$term%' or   `MATCH_green_it` MATCH '%$term%' or     `printing_costs` MATCH '%$term%' or `travel_costs` MATCH '%$term%' or   `comments` MATCH '%$term%' or   `uel_green_modules` MATCH '%$term%' or  `year_of_study` MATCH '%    $term%' or      `questionnaire_filled` MATCH '%$term%'");

while ($row = mysql_fetch_array){
echo 'ID: '.$row['ID'];
echo '<br/> Do you have an interest in green IT?: '.$row['interest_in_green_it'];
echo '<br/> Do you think green IT is a good thing?: '.$row['green_it_good_thing'];
echo '<br/> Would you consider green IT if it meant saving the environment?: '.$row['green_it_save_environment'];
echo '<br/> Would you consider green IT if it meant saving money?: '.$row['green_it_save_money'];
echo '<br/> What would be the better incentive to practice green IT?: '.$row['green_it_incentive'];
echo '<br/> DO you think UEL is doing enough to practice green IT? : '.$row['uel_green_it'];
echo '<br/> Do you like green IT?: '.$row['like_green_it'];
echo '<br/> Your estimated monthly travel costs to UEL: '.$row['travel_costs'];
echo '<br/> Your estimated printing costs at UEL at any one time: '.$row['printing_costs'];
echo '<br/> Your comments: '.$row['comments'];
echo '<br/> Would you like to see more green modules at UEL?: '.$row['uel_green_modules'];
echo '<br/> What is your year of study?: '.$row['year_of_study'];
echo '<br/> If you did not fill in the questionnaire why not?: '.$row['questionnaire_filled'];
echo '<br/><br/>';
}

$_GET = array_map('trim', $_GET); 
$_POST = array_map('trim', $_POST); 
$_COOKIE = array_map('trim', $_COOKIE); 
$_REQUEST = array_map('trim', $_REQUEST); 
if(get_magic_quotes_gpc()): 
$_GET = array_map('stripslashes', $_GET); 
$_POST = array_map('stripslashes', $_POST); 
$_COOKIE = array_map('stripslashes', $_COOKIE); 
$_REQUEST = array_map('stripslashes', $_REQUEST); 
endif; 
$_GET = array_map('mysql_real_escape_string', $_GET); 
$_POST = array_map('mysql_real_escape_string', $_POST); 
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE); 
$_REQUEST = array_map('mysql_real_escape_string', $_REQUEST); 

?>

提前谢谢你

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    “我怎样才能改进这个查询?”

    使用indexFULLTEXT index 在这里可能很有用。

    我相信最新的 InnoDB 引擎也支持 FULLTEXT 索引。 (MySQL 5.6+)

    要获得所有可能的结果,请计算一个分数,以确定一个元组与您的条件和ORDER BY score DESC LIMIT 20 的匹配程度,以获得最佳的 20 个匹配项。有关示例,请参阅this answer

    【讨论】:

    • 你从哪里听说的,如果是真的,那真是好消息,有链接吗?
    • 这是甲骨文在收购 Sun 和 MySQL 之后完成的。 blogs.innodb.com/wp/2011/12/…
    • 谢谢! (另外,为劫持这些 cmets 道歉!)
    • 感谢您的回复,我只是想知道索引到底是什么样的以及它在上面的代码中的位置?我正在使用 myISAM 引擎。谢谢
    • 需要事先创建索引。创建表后创建它。索引是您的结果的持久快捷方式。请参阅MySQL manual 了解如何创建索引。我建议使用像phpMyAdmin 这样的数据库管理工具来操作你的表结构。要创建全文索引,请使用ALTER TABLE myTable` ADD FULLTEXT (myColumn1, myColumn2, ...)`。仅当您使用所有列或这些列的任何前缀时,索引才会起作用。手册页也对此进行了说明。
    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    相关资源
    最近更新 更多