2012-04-10 34 views
2

これで、MATCHクエリを使用しているので、なぜ私は多くの結果を得ているのでしょうか。私が望むのは、関連する検索結果を出力することです。たとえば、検索が「緑に興味がある」場合は、関心があるtehテーブルから結果を出力するか、または理にかなっている場合は緑またはITを出力します。mysql/php検索エンジンの検索結果が遅すぎると結果が遅くなります

今、私はすべてのリスルを受け取ります。私はこれを達成するために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); 

?> 

は、「どのように私はこのクエリを向上させることができますか?」事前に

答えて

2

をありがとう

indexを使用してください。ここではFULLTEXT indexが役に立つかもしれません。

最新のInnoDBエンジンはFULLTEXTインデックスもサポートしていると思います。

すべての可能な結果が得られないようにするには、タプルがあなたの条件にどれくらいうまく合うかを決めるスコアを計算して、ORDER BY score DESC LIMIT 20を選んで、ベスト20のマッチを得ます。例については、this answerを参照してください。

+0

あなたはそれを聞いたことがあります。それが本当なら、素晴らしいリンクです。 – BenOfTheNorth

+1

これはSunが購入したMySQLを購入した後にOracleによって行われました。 http://blogs.innodb.com/wp/2011/12/innodb-full-text-search-in-mysql-5-6-4/ – Basti

+1

ありがとうございます! (また、これらのコメントのハイジャックのための謝罪!) – BenOfTheNorth

関連する問題