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

  • Home
  • SEARCH
  • 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 8343067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:56:59+00:00 2026-06-09T05:56:59+00:00

i need to simplify this query because the server that hold mysql can’t do

  • 0

i need to simplify this query because the server that hold mysql can’t do it in the time limit setted.

SELECT `B`.*,
       `C`.`name`,
       DATE_ADD(`B`.`SAILING-DATE`, INTERVAL `B`.`NIGHTS` DAY) AS dataEnd 

FROM `table1` AS B 
INNER JOIN `table2` AS A 
INNER JOIN `table3` AS C

WHERE `B`.`ID`=`A`.`ID` 
AND `B`.`CAT`=`A`.`CAT` 
AND `C`.`code`=`A`.`code`
AND (`B`.`NIGHTS`>=$aNumber AND `B`.`NIGHTS`<=$anotherNumber) 
// where are other AND like this on `table1`

Where table1 and table2 has about 25.000 rows.

I thought to suddivide the query executing a first query on table1, than loop all the result and make a single query for every result, but i think is too inefficient.

can someone help me? thanks!!!

EDIT:

THIS IS THE EXPLAIN:

enter image description here

EDIT2:

I CAN’T ADD INDEX CAUSE THE FIELD ID, CAT and code are not unique in every table.. in each there are more rows with same ID CAT and code

EDIT3:

I think is better to divide all into subquery more simple.. can someone give me an idea? thanks!

  • 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-09T05:57:01+00:00Added an answer on June 9, 2026 at 5:57 am

    One thing you can try is the following:

    SELECT `B`.*, 
           `C`.`name`, 
           DATE_ADD(`B`.`SAILING-DATE`, INTERVAL `B`.`NIGHTS` DAY) AS dataEnd  
    
    FROM (SELECT * 
             FROM `table1` 
             WHERE table1.NIGHTS >=$aNumber AND table1.NIGHTS <=$anotherNumber) AS B  
    INNER JOIN `table2` AS A  ON `B`.`ID`=`A`.`ID` AND `B`.`CAT`=`A`.`CAT`
    INNER JOIN `table3` AS C  ON `A`.`code` = `C`.`code`
    

    This will effectively prefilter the rows in table1 by only selecting those with Nights fulfilling your required criteria reducing the number involved in joins on the other tables.

    Also note that your JOIN conditions should be after the INNER JOIN using ON – you effectively have no need of the WHERE clause in this instance.

    One other thing you should investigate is the query plan – see the EXPLAIN command for more information.

    In this instance, you may benefit from an index on the ID, CAT, and CODE columns, but, rather than just add an index to everything, examine the output from EXPLAIN first to see where the best savings are – adding indexes will have a cost when you insert data.

    EDIT:

    Okay, so the subquery performs well, but you are worried about the situation where you can’t prefilter table1. First things first, let’s revise your original query, and try that:

    SELECT B.*, 
           C.name, 
           DATE_ADD(B.SAILING-DATE, INTERVAL B.NIGHTS DAY) AS dataEnd  
    
    FROM table1 AS B  
    INNER JOIN table2 AS A  ON (B.ID=A.ID AND B.CAT=A.CAT)
    INNER JOIN table3 AS C  ON (A.code = C.code)
    

    I wonder if the odd formatting of your original query (the join conditions should really follow the table you are joining on) are confusing MySql in some way. I’ve also removed the back-ticks as they aren’t needed and, in my opinion, make the query much harder to read (and type).

    If performance is not what you expect, please post the results of SHOW INDEX on table1 and table2. It may be that adding an index on the combination of ID and CAT in table1 and ID and CAT in table2 (a composite index) may improve your performance here.

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

Sidebar

Related Questions

I need a way to simplify this command: grep 'SEARCHTERM' server.log | grep -v
I've got a query that presently looks something like this (I'll simplify it to
I need to simplify this xml ( to a list ), and associate the
Need help with a query that I wrote: I have three tables Company id
I've been working in a time attendance application and need some enlightment. This is
I am looking for a nice simple LINQ query that can get an item
I am trying to simplify this query as it is going to be dynmaically
I need some help with a query (sorry, I'm not too good at this!)
I'm trying to fine-tune my MySQL server so I check my settings, analyzing slow-query
I need to simplify this code to avoid hardcoding. I mean the variable baseUrl

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.