mysql查询需要30秒才能执行...太长时间了

时间:2010-08-17 15:42:00

标签: php mysql

我正在尝试将MySQL查询作为网站上的搜索进行运行,这需要很长时间。

我直接从phpMyAdmin运行它,大约需要20秒才能完成。

这是我正在运行的查询:

SELECT * , bb_business.id AS bid
FROM bb_business
LEFT OUTER JOIN bb_offers ON bb_business.id = bb_offers.store_id
LEFT OUTER JOIN bb_cat_business ON bb_business.id = bb_cat_business.store_id
LEFT OUTER JOIN bb_categories ON bb_categories.id = bb_cat_business.cat_id
WHERE bb_business.active = '1'
AND (
bb_business.business_name LIKE '%altas%'
OR bb_categories.category_name LIKE '%altas%'
OR bb_business.business_name LIKE '%juice%'
OR bb_categories.category_name LIKE '%juice%'
)
AND (
bb_business.city LIKE '%%'
OR bb_business.state LIKE '%%'
OR bb_business.zip LIKE '%%'
)
GROUP BY bb_business.business_name
ORDER BY bb_business.business_name DESC 
LIMIT 1 , 10`

速度:显示0 - 0行(总共1行,查询占用20.1130秒)

我不确定向您展示结构的最佳方式。但是这里有一些表的复制和粘贴:

bb_ach            128 InnoDB latin1_swedish_ci 32.0 KB - 
bb_business       2,862 InnoDB latin1_swedish_ci 1.6 MB - 
bb_categories     15 MyISAM utf8_general_ci 6.7 KB 60 Bytes

bb_cat_business   2,926 MyISAM utf8_general_ci 49.6 KB 17 Bytes

bb_emails         13 MyISAM utf8_general_ci 6.0 KB - 
bb_member         409 MyISAM utf8_general_ci 62.7 KB - 
bb_member_card    593 MyISAM utf8_general_ci 112.1 KB - 
bb_merchant       154 InnoDB latin1_swedish_ci 32.0 KB - 
bb_offers         80 InnoDB latin1_swedish_ci 16.0 KB - 
bb_products       2 InnoDB latin1_swedish_ci 32.0 KB 

如果我能提供其他任何东西。请告诉我。

非常感谢您的帮助。

EDIT ##################################

好的,使用OMG小马的建议,我改变了一些事情。制作一些东西MyISAM并创建了一些FULLTEXT索引来使用。

我最终得到了这个问题:

SELECT *, bb_business.id AS bid FROM bb_business 
LEFT OUTER JOIN bb_offers ON bb_business.id = bb_offers.store_id 
LEFT OUTER JOIN bb_cat_business ON bb_business.id = bb_cat_business.store_id 
LEFT OUTER JOIN bb_categories ON bb_categories.id = bb_cat_business.cat_id 
WHERE bb_business.active = '1' AND 
MATCH(bb_business.business_name) AGAINST ('".addslashes($business)."') OR 
MATCH(bb_categories.category_name) AGAINST ('".addslashes($business)."') OR 
MATCH (bb_business.city,bb_business.state,bb_business.zip) AGAINST ('".addslashes($city)."') 
GROUP BY bb_business.business_name 
ORDER BY bb_business.imported,".$this->sortCol." ".$this->sortDirection." LIMIT ".$this->start.",".$this->limit

我认为它使它更慢;)

EXPLAIN给出了这个:

id  select_type  table  type  possible_keys  key  key_len  ref  rows  Extra  
1 SIMPLE bb_business ALL NULL NULL NULL NULL 2877 Using temporary; Using filesort 
1 SIMPLE bb_offers ALL NULL NULL NULL NULL 94   
1 SIMPLE bb_cat_business ALL NULL NULL NULL NULL 5697   
1 SIMPLE bb_categories eq_ref PRIMARY PRIMARY 8 buxback_site.bb_cat_business.cat_id 1 Using where 

结果是第一个查询最多需要40秒才能完成。如果我再次搜索完全相同的短语,速度就像我期望的MYSQL一样。在几毫秒内。这只是第一个需要FOREVER的搜索...

还有什么想法?我很乐意付钱给别人帮忙。

3 个答案:

答案 0 :(得分:2)

以面值进行查询,这是我的重写:

   SELECT *, bb_business.id AS bid
     FROM bb_business b
LEFT JOIN bb_offers o ON o.store_id = b.id
LEFT JOIN bb_cat_business cb ON cb.store_id = b.id
LEFT JOIN bb_categories c ON c.id = cb.cat_id
    WHERE b.active = '1'
      AND (    bb_business.business_name LIKE '%altas%'
            OR bb_business.business_name LIKE '%juice%'
            OR MATCH(c.category_name) AGAINST ('altas', 'juice'))          
 GROUP BY b.business_name
 ORDER BY b.business_name DESC 
    LIMIT 0, 10
  • LIMIT从零开始 - 从1开始将省略第一条记录。
  • 您可以在MyISAM表上使用全文搜索(FTS)功能,这就是MATCH() .. AGAINST。您可以通过添加全文索引来加快速度......
  • SELECT *是一种糟糕的方法 - 最好拼出你真正想要的每一个

答案 1 :(得分:1)

使用EXPLAIN查看正在进行的操作,数据库如何执行数据库。

LIKE会破坏性能,当参数以%。

开头时,数据库无法使用任何索引

答案 2 :(得分:0)

只是想回来并恢复此并发布答案。我最终创建了一个索引表并创建了一个PHP脚本来索引主数据库。最重要的是,MySQL在多个表中处理多个匹配方面做得不好。