如何加快mysql搜索速度

时间:2012-12-21 18:43:24

标签: mysql search

我正在使用此代码从数据库读取,但我得到的结果有点慢。所以我需要一些关于这个问题的帮助。

SELECT mod_part.id AS mp_id,parts.id as part_id,outlets.name as out_name, parts.part_name as part_name, parts.brand, outlets.sell, outlets.cost, outlets.qty, outlets.comp, parts.supplier, brands.name AS brand_name 
FROM outlets,models,manufacturers,brands,parts,mod_part 
WHERE outlets.part_id=mod_part.id 
      AND mod_part.part_id=parts.id 
      AND brands.id=mod_part.brand_id 
      AND mod_part.model_id=models.id 
      AND parts.man_id=manufacturers.id 
      AND (MATCH (manufacturers.name) AGAINST ('".$_GET['search']."') 
      OR MATCH (parts.part_name) AGAINST('".$_GET['search']."') 
      OR MATCH(models.name) AGAINST('".$_GET['search']."')) 
GROUP BY parts.id

2 个答案:

答案 0 :(得分:2)

你可以尝试使用内连接

$search = mysql_real_escape_string($_GET['search']);  // escape from sql injection here

然后在你的sql中使用它(请使用PDO或SQLI代替)

"SELECT mo.id AS mp_id,p.id as part_id,o.name as out_name,p.part_name as part_name,p.brand,o.sell,o.cost,o.qty,o.comp,p.supplier,b.name AS brand_name 
 FROM outlets 
 INNER JOIN models m  ON  m.id = mo.model_id
 INNER JOIN parts p ON  mo.part_id= p.id 
 INNER JOIN manufacturers ma ON p.man_id=ma.id 
 INNER JOIN brands b ON  bid=mo.brand_id
 INNER JOIN mod_part mo ON mo.part_id=mo.id 

WHERE 
   (MATCH (ma.name)  AGAINST ('".$search."')
   OR MATCH (p.part_name)  AGAINST('".$search."') 
   OR MATCH(m.name)  AGAINST('".$search."'))
GROUP BY p.id ";

答案 1 :(得分:0)

如果查询速度很慢,请尝试查看explain plan。否则,您将不得不提供更多信息,例如表结构,索引,代码等。

此外,您允许SQL注入。请阅读该主题并修复代码。如果你告诉你正在使用哪种语言,我可以尝试添加一些链接。否则,请查看“绑定”您的变量。