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 7750777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:22:39+00:00 2026-06-01T11:22:39+00:00

SELECT COUNT(`t`.id) FROM `product` `t` INNER JOIN `category` `c1` ON ( `c1`.id =’15’ )

  • 0
SELECT COUNT(`t`.id) 
FROM `product` `t` 
INNER JOIN `category` `c1` ON ( `c1`.id ='15' )
LEFT JOIN `category` `c2` ON ( `c2`.`parent_category_id` = '15' ) 
LEFT JOIN `category` `c3` ON ( `c3`.`parent_category_id` = `c2`.`id` ) 
LEFT JOIN `category` `c4` ON ( `c4`.`parent_category_id` = `c3`.`id` ) 
LEFT JOIN `category` `c5` ON ( `c5`.`parent_category_id` = `c4`.`id` ) 
LEFT JOIN `category` `c6` ON ( `c6`.`parent_category_id` = `c5`.`id` ) 
LEFT JOIN `category` `c7` ON ( `c7`.`parent_category_id` = `c6`.`id` ) 
WHERE 
t.category_id IN (c2.id,c3.id,c4.id,c5.id,c6.id,c7.id) 
AND (t.mod='Accepted')

I have a category and product table, category table has parent_category_id which refers to the same category table but indicates that record is sub category.

    SHOW PROFILE Result (ordered by duration)

state   duration (summed) in sec    percentage
Sending data    1.69029 99.96392
freeing items   0.00015 0.00887
statistics  0.00015 0.00887
checking query cache for query  0.00009 0.00532
init    0.00006 0.00355
preparing   0.00003 0.00177
checking permissions    0.00002 0.00118
optimizing  0.00002 0.00118
Opening tables  0.00002 0.00118
System lock 0.00001 0.00059
starting    0.00001 0.00059
cleaning up 0.00001 0.00059
Table lock  0.00001 0.00059
end 0.00001 0.00059
executing   0.00001 0.00059
logging slow query  0.00001 0.00059
Total   1.69090 100.00000

Change Of STATUS VARIABLES Due To Execution Of Query

variable    value   description
Bytes_received  291 Bytes sent from the client to the server
Bytes_sent  72  Bytes sent from the server to the client
Com_select  1   Number of SELECT statements that have been executed
Handler_read_key    133514  Number of requests to read a row based on a key
Handler_read_next   133549  Number of index columns read with a range constraint or an index scan
Key_read_requests   444695  Number of MyISAM key blocks read from cache
Key_reads   234 Number of MyISAM key blocks that were read from disk. Indicates insufficient key cache size
Key_write_requests  200 Number of MyISAM key blocks that were written to cache
Key_writes  105 Number of MyISAM key blocks written to disk. Indicates insufficient key cache size
Last_query_cost*    19845567    The total cost of this query as computed by the query optimizer
Open_files* 1475    Number of files opened
Qcache_free_memory* 149209104   Amount of Query cache free memory
Qcache_hits 1520    Number of queries served directly by the Query Cache
Qcache_inserts  190 Number of queries inserted into the Query Cache
Qcache_not_cached*  2062788 Number of queries that were not cached by the Query Cache. They are not cacheable or was not cached due to the query_cache_type setting
Questions   1   Number of statements executed by the server
Table_locks_immediate   574 The number of requests for table locks that could be granted immediately
Threads_cached  6   The number of threads in the thread cache
Threads_running*    3   The number of threads that are not sleeping
* Actual values after query execution (not changes due to query)

EXPLAIN Result

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  c1  const   PRIMARY PRIMARY 4   const   1   Using index
1   SIMPLE  t   ref category_id, mod    mod 2   const   48  Using where
1   SIMPLE  c2  ref parent_category_id  parent_category_id  5   const   18  
1   SIMPLE  c3  ref parent_category_id  parent_category_id  5   ucuzolur.c2.id  7   
1   SIMPLE  c4  ref parent_category_id  parent_category_id  5   ucuzolur.c3.id  7   
1   SIMPLE  c5  ref parent_category_id  parent_category_id  5   ucuzolur.c4.id  7   
1   SIMPLE  c6  ref parent_category_id  parent_category_id  5   ucuzolur.c5.id  7   
1   SIMPLE  c7  ref parent_category_id  parent_category_id  5   ucuzolur.c6.id  7   Using where

You can find the profile above. As you see everything is fine except sending data is too slow. This query is just a count query, why sending consume too much time and how can I improve this query. I have 20000 rows in category table.

  • 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-06-01T11:22:41+00:00Added an answer on June 1, 2026 at 11:22 am

    If I understood your goal correctly, rather than creating the whole category structure, you could try using subqueries to drill downwards to find all products that belong to the Category 15 structure.

    Simplified SQL:

    SELECT COUNT(*)
    FROM 'product' 't'
    WHERE EXISTS (
        SELECT *
        FROM 'category' 'c1'
        WHERE 't'.'category_id' = 'c1'.'id'
            AND ('c1'.'id' = 15
            OR (EXISTS
                (SELECT *
                FROM 'category' 'c2'
                WHERE 'c2'.'parent_category_id' = 'c1'.'id'
                    AND ('c2'.'id' = 15                    
                        OR (EXISTS .... 
                            recursion here depending on 
                            how many levels the categories can have)
                )
            ))
        )
    

    This may also behave horribly, but it may also make better use of indexes perhaps, or reduce the number of hits to the category table.

    Attempt 2

    Does the following behave any better?

    SELECT COUNT(`t`.id) 
    FROM `product` `t` 
        LEFT JOIN `category` `c2` ON ( `c2`.`id` = t.category_id )
        LEFT JOIN `category` `c3` ON ( `c2`.`parent_category_id` = `c3`.`id` ) 
        LEFT JOIN `category` `c4` ON ( `c3`.`parent_category_id` = `c4`.`id` ) 
        LEFT JOIN `category` `c5` ON ( `c4`.`parent_category_id` = `c5`.`id` ) 
        LEFT JOIN `category` `c6` ON ( `c5`.`parent_category_id` = `c6`.`id` ) 
        LEFT JOIN `category` `c7` ON ( `c6`.`parent_category_id` = `c7`.`id` ) 
    WHERE (`c2`.`id` = 15
        OR `c3`.`id` = 15 
        OR `c4`.`id` = 15
        OR `c5`.`id` = 15
        OR `c6`.`id` = 15
        OR `c7`.`id` = 15)
    AND (t.mod='Accepted')
    

    Making sure that parent_category has an index of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: SELECT COUNT(*) FROM FirstTable ft INNER JOIN SecondTable st
Example: select count(*) from my table where column1 is not null and (column1 =
I have this query: SELECT COUNT(articles.id) AS count FROM articles, xml_documents, streams WHERE articles.xml_document_id
I have the following two queries: select count(*) from segmentation_cycle_recipients scr , segmentation_instance si
I have following rather simple query select count(*) from tbl t1, tbl t2 For
I have a query that goes like this: SELECT Product.local_price*Rate.exchange_rate AS 'US_price' FROM Product
I'm trying to do a simple Select Count(*) from PRODUCTS where date > xxx
TOPIC_COUNT_SQL = SELECT COUNT(*) FROM topics_topic WHERE topics_topic.object_id = maps_map.id AND topics_topic.content_type_id = %s
When we execute select count(*) from table_name it returns the number of rows. What
SET @v1 := SELECT COUNT(*) FROM user_rating; SELECT @v1 When I execute this query

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.