usef_ksa
usef_ksa

Reputation: 1659

GROUP BY query optimization

Dears,
I need your help to optimize the below query. I have two tables, one for storing books data and the second table for mapping the books to tags related to it. I want to count how many books form a certain publisher in every category. This query do the job but I need to optimize it:

select count(book.id),publisher from book,tag where book.id=tag.book_id  AND publisher ='Addison-Wesley Professional' AND tag.name='PHP' group by category

the result of explain is

    id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  tag     ref     PRIMARY     PRIMARY     92  const   1   Using where; Using index; Using temporary; Using f...
1   SIMPLE  book    eq_ref  PRIMARY     PRIMARY     4   test.tag.book_id    1   Using where

the tables are:

-- 
-- Table structure for table `book`
-- 

CREATE TABLE `book` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(30) NOT NULL,
  `ISBN` varchar(10) NOT NULL,
  `category` varchar(30) NOT NULL,
  `publisher` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

-- 
-- Dumping data for table `book`
-- 

INSERT INTO `book` VALUES (1, 'PHP and MySQL Web Development', '9780672329', 'Web Development', 'Addison-Wesley Professional');
INSERT INTO `book` VALUES (2, 'JavaScript Patterns', '0596806752', 'Web Development', 'O''Reilly Media');



-- 
-- Table structure for table `tag`
-- 

CREATE TABLE `tag` (
  `name` varchar(30) NOT NULL,
  `book_id` int(11) NOT NULL,
  PRIMARY KEY  (`name`,`book_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table `tag`
-- 

INSERT INTO `tag` VALUES ('MySQL', 1);
INSERT INTO `tag` VALUES ('PHP', 1);

Upvotes: 2

Views: 152

Answers (1)

piotrm
piotrm

Reputation: 12356

You need a composite index on (publisher, category) in your book table, in that exact order, so the subquery doing grouping on category can restrict results fast to those having exact publisher and then use second part of the index to group on category.

ALTER TABLE book ADD INDEX publ_cat( publisher, category );

Upvotes: 2

Related Questions