没有结果时查询工作太慢

时间:2012-03-22 16:32:44

标签: mysql query-optimization

我有以下问题。当条目匹配条目时,我的查询执行0.0002秒,没有条目时,我的查询执行0.0367。

CREATE TABLE IF NOT EXISTS `main_pages_options` (
  `page_id` int(11) NOT NULL,
  `value` varchar(255) NOT NULL,
  `option_id` int(11) NOT NULL,
  UNIQUE KEY `page_id` (`page_id`,`option_id`),
  FULLTEXT KEY `value` (`value`)
) ENGINE=MyISAM ;

SELECT page_id
FROM main_pages_options
  WHERE (option_id = 1 AND value = 'Test 1') 
  OR (option_id = 2 AND value = 'Test 2') 
  OR (option_id = 3 AND value = 'Test 3')
GROUP BY page_id 
HAVING COUNT(*) = 3

SELECT page_id
FROM main_pages_options
  WHERE (option_id = 1 AND value = 'Test 7') 
  OR (option_id = 2 AND value = 'Test 2') 
  OR (option_id = 3 AND value = 'Test 3')
GROUP BY page_id 
HAVING COUNT(*) = 3

我在main_pages_options中有~80,000个条目,在main_pages中有~20,000个条目。

1 个答案:

答案 0 :(得分:1)

尝试在option_idvalue上创建多列索引。