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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:40:56+00:00 2026-05-12T19:40:56+00:00

I’m using MySQL 5, and I need to do this sentence to get the

  • 0

I’m using MySQL 5, and I need to do this sentence to get the results. It’s the first attempt when I think about it. I know there are several ways to improve it, but I want to get your opinions first:

SELECT p.id, p.image, p.lat, p.lng, 
       p.category_id, p2.title, p2.description, c2.name
FROM place p
LEFT JOIN place_translation p2 ON p.id = p2.id
LEFT JOIN trip_place t ON p.id = t.place_id
LEFT JOIN category c ON p.category_id = c.id
LEFT JOIN category_translation c2 ON c.id = c2.id
WHERE c.root_id =1
AND c2.lang =  'en'
AND p2.lang =  'en'
AND t.trip_id =1
AND p.root_id =1
AND p.lft >39
AND p.rgt <44
ORDER BY p2.title

It’s a multilingual service, so we have place_translation and category_translation. We get the places that belongs to a trip and that belongs to a city (it’s a nested set, so root_id, lft and rgt)

The indexes are:

on table place: id, category_id
on table place_translation: id-lang index
on table trip_place: place_id, trip_id
on table category: id
on table category_translation: id-lang index

So, with your experience, how would you improve it to make it smaller? I know I don’t have to denormalized until I have problems; but I want to fix a good base.

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-05-12T19:40:57+00:00Added an answer on May 12, 2026 at 7:40 pm

    There are two obvious things. Since you have conditions on values from each table in WHERE, you require that you found matching rows in the JOIN clauses. This means you should use inner joins, not left outer joins. Next, to filter the results as soon as possible you can move some of the conditions from WHERE to JOIN .. ON.

    I’ve also reordered the tables in a way that makes sense to me, i.e. first you are selecting from trip_place, because you know you want only places from this trip. Then for each row from trip_place find the matching place, and for each place find the matching category. Translations are last because they don’t affect the query functionally. So the query would be something like this:

    SELECT
        p.id, p.image, p.lat, p.lng, 
        p.category_id, p2.title, p2.description, c2.name
    FROM
        trip_place t
        JOIN place p ON p.id = t.place_id AND p.root_id = 1 AND p.lft > 39 AND p.rgt < 44
        JOIN category c ON p.category_id = c.id AND c.root_id = 1
        JOIN place_translation p2 ON p.id = p2.id AND p2.lang = 'en'
        JOIN category_translation c2 ON c.id = c2.id AND c2.lang = 'en'
    WHERE
        t.trip_id = 1
    ORDER BY p2.title
    

    I can’t tell if it’s actually faster, but it might be. You should run EXPLAIN on both of them. From the look at the list of indexes, you should probably add an index on place (root_id, p.lft, p.rgt) (all of them as a single index, not each column individually).

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

Sidebar

Related Questions

No related questions found

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.