2
Dears,
我需要您的幫助來優化以下查詢。我有兩個表格,一個用於存儲書籍數據,第二個表格用於將書籍映射到與其相關的標籤。我想統計每個類別中某個出版商有多少書籍。這個查詢做的工作,但我需要優化它:
GROUP BY查詢優化
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
的explain
結果是
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
表是:
--
-- 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);
+1但請注意,它不一定有幫助。如果標籤比發佈者更有選擇性,那麼規劃者應該更喜歡去找標籤。 – 2011-05-29 22:35:24