inwoker
inwoker

Reputation: 23

Query works too slow when there is no results

I have the following problem. My query executes for 0.0002 sec when there are entries that match entries and 0.0367 when there are none.

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

I have ~80,000 entries in main_pages_options and ~20,000 entries in main_pages.

Upvotes: 1

Views: 127

Answers (1)

Fabian Barney
Fabian Barney

Reputation: 14549

Try creating a multi-column index on option_id and value.

Upvotes: 1

Related Questions