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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:45:53+00:00 2026-06-06T19:45:53+00:00

I have an SQL query with a nested join: SELECT rh.host, rh.report, COUNT(results.id), COUNT(results_2.id),

  • 0

I have an SQL query with a nested join:

SELECT rh.host, rh.report, COUNT(results.id), COUNT(results_2.id), COUNT(results_3.id), COUNT(results_4.id)
FROM report_hosts rh
INNER JOIN report_results rr ON rh.report = rr.report
LEFT OUTER JOIN results ON rr.result = results.id AND results.type =  'Hole' AND results.host = rh.host
LEFT OUTER JOIN results results_2 ON rr.result = results_2.id AND results_2.type =  'Warning' AND results_2.host = rh.host
LEFT OUTER JOIN results results_3 ON rr.result = results_3.id AND results_3.type =  'Note' AND results_3.host = rh.host
LEFT OUTER JOIN results results_4 ON rr.result = results_4.id AND results_4.type =  'Log' AND results_4.host = rh.host
GROUP BY rh.host

The query as-is takes about 5sec with 99.7% copying to temp table. An EXPLAIN of the full query shows as:

+----+-------------+-----------+--------+---------------+---------+---------+-------------------+------+---------------------------------+
| id | select_type | table     | type   | possible_keys | key     | key_len | ref               | rows | Extra                           |
+----+-------------+-----------+--------+---------------+---------+---------+-------------------+------+---------------------------------+
|  1 | SIMPLE      | rr        | ALL    | report        | NULL    | NULL    | NULL              | 3139 | Using temporary; Using filesort |
|  1 | SIMPLE      | rh        | ref    | report        | report  | 5       | openvas.rr.report |  167 | Using where                     |
|  1 | SIMPLE      | results   | eq_ref | PRIMARY,type  | PRIMARY | 4       | openvas.rr.result |    1 |                                 |
|  1 | SIMPLE      | results_2 | eq_ref | PRIMARY,type  | PRIMARY | 4       | openvas.rr.result |    1 |                                 |
|  1 | SIMPLE      | results_3 | eq_ref | PRIMARY,type  | PRIMARY | 4       | openvas.rr.result |    1 |                                 |
|  1 | SIMPLE      | results_4 | eq_ref | PRIMARY,type  | PRIMARY | 4       | openvas.rr.result |    1 |                                 |
+----+-------------+-----------+--------+---------------+---------+---------+-------------------+------+---------------------------------+

When I remove the LEFT JOINs, the query executes in about 1s, each LEFT JOIN adds about one additional second execution time.

My question:
Can anyone explain, why the copy to temp table task of one join takes longer if there are more LEFT JOINs? Is MySQL copying the temp table several times for each JOIN?

How can I avoid this? Am I missing an index?

What I intend to accomplish:
I have a table with scanning results of several hosts. Each result is classified in types ( “Hole”, “Warning”, “Note” or “Log”). I want to select each host and the corresponding amount of Holes, Warnings, Notes and Logs. As a “restriction” I have the fact, that not each host has each type of results.

  • 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-06T19:45:55+00:00Added an answer on June 6, 2026 at 7:45 pm

    You’re joining a single table several times, which effectively is like joining multiple tables. You should be able to handle that with some case statements and a where clause instead. (In fact you may not need the where clause.)

    SELECT rh.host, rh.report, 
     COUNT(CASE WHEN results.type = 'Hole' THEN 1 ELSE NULL END) as Holes, 
     COUNT(CASE WHEN results.type = 'Warning' THEN 1 ELSE NULL END) as Warnings,
     COUNT(CASE WHEN results.type = 'Note' THEN 1 ELSE NULL END) as Notes, 
     COUNT(CASE WHEN results.type = 'Log' THEN 1 ELSE NULL END) as Logs
    FROM 
     report_hosts rh
    INNER JOIN 
     report_results rr 
    ON 
     rh.report = rr.report
    LEFT OUTER JOIN 
     results 
    ON 
     rr.result = results.id 
     AND results.host = rh.host
    WHERE
     results.type = 'Hole' 
     OR results.type = 'Warning' 
     OR results.type = 'Note' 
     OR results.type = 'Log'
    GROUP BY rh.host, rh.report
    

    Case statements, IME, are not the greatest performers, but your data bloat from the many joins may offset that and give this better performance.

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

Sidebar

Related Questions

i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have an sql query like this: SELECT IF( (SELECT COUNT(*) FROM `test_table2` WHERE
I have a nested SQL query : SELECT * FROM ( SELECT * FROM
I have 2 tables, using an inner join to query them. SELECT COUNT(table2.id) FROM
I have a nested SQL query : SELECT DISTINCT(topic_id) FROM bb_posts WHERE topic_id NOT
i have this sql query select * from table where name like ? but
I have this SQL query from the logs select content = 5%id%201=1 from pages
I have a pure-SQL queryset: SELECT ft2.user_id, avg(...) figure, count(...) as figure_count FROM figures_table
I have an sql query pulling a few results from different tables, one of
I have a SQL query of the form SELECT ... FROM A@DB1 a, B@DB1

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.