Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:59:04+00:00 2026-05-22T16:59:04+00:00

Dears, I need your help to optimize the below query. I have two tables,

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:59:04+00:00Added an answer on May 22, 2026 at 4:59 pm

    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 );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need your help in solving the following 2 functions/problems: 1) I have to
Dear friends, I need your help. I need to send .bmp file to another
dear i need to get query from 3 tables i try to show how
I need to compare two value (date) from query . String myFormatString = yyyyMMdd;
dear all, i need to combine two different result in one script. The problem
Dear g++ hackers, I have the following question. When some data of an object
Dear All, please help me since I'm newbie in SQL Server. I have a
Dear all, I have a select query that currently produces the following results: DoctorName
I have the following string and regex: var string = Dear [to name], [your
i have one text box formated with follwoing syntax $(#cont_dt).blur(function(){ $(this).format({format:#,###.00, locale:us}); }) this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.