1

I know it´s difficult to answer without knowing the model, but I have next heavy query that takes around 10 secs to complete in my MySQL database. I guess it can be optimized, but I´m not that skilled.

SELECT DISTINCT
    b . *
FROM
    boats b,
    states s,
    boat_people bp,
    countries c,
    provinces pr,
    cities ct1,
    cities ct2,
    ports p,
    addresses a,
    translations t,
    element_types et
WHERE
    s.name = 'Confirmed' AND bp.id = '2'
        AND b.state_id = s.id
        AND b.id NOT IN (SELECT 
            bc.boat_id
        FROM
            boat_calendars bc
        WHERE
            (date(bc.since) <= '2015-02-09 09:23:00 +0100'
                AND date(bc.until) >= '2015-02-09 09:23:00 +0100')
                OR (date(bc.since) <= '2015-02-10 09:23:00 +0100'
                AND date(bc.until) >= '2015-02-10 09:23:00 +0100'))
        AND b.people_capacity_id >= bp.id
        AND c.id = (SELECT DISTINCT
            t.element_id
        FROM
            translations t,
            element_types et
        WHERE
            t.element_translation = 'Spain'
                AND et.name = 'Country'
                AND t.element_type_id = et.id)
        AND pr.country_id = c.id
        AND pr.id = (SELECT DISTINCT
            t.element_id
        FROM
            translations t,
            element_types et
        WHERE
            t.element_translation = 'Mallorca'
                AND et.name = 'Province'
                AND t.element_type_id = et.id)
        AND ((ct1.province_id = pr.id AND p.city_id = ct1.id AND b.port_id = p.id)
        OR (ct2.province_id = pr.id AND a.city_id = ct2.id AND b.address_id = a.id)); 

Basically, it tries to get all the boats, that are not already booked in Confirmed state and that are in a province and a country ie. Mallorca, Spain.

Please, let me know if you need some more details about de purpose of the query or the model.

4
  • 1
    remove * instead give column names in select statement. it will increase some performance. Its one of the way to optimize Commented Feb 6, 2015 at 9:01
  • 1
    do you have indexes defined on joined columns? Commented Feb 6, 2015 at 9:06
  • You have cross joins against translations and element_types . Neither of these tables are used in the main query and there is no join condition against them to limit the joins. Commented Feb 6, 2015 at 9:42
  • Fix your query to have proper, explicit join syntax. First, it is hard to understand. Second, you could be missing join conditions between tables. Commented Feb 6, 2015 at 11:43

3 Answers 3

1

remove * from select clause. instead give column names in select clause. it will increase some performance. Its one of the way to optimize

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of having a sub query, use LEFT JOIN NULL (just google for it) and it will help a lot.

Comments

0

All your answers are good. But, according to @POHH suggestions, I increased the performance magically by just replacing the b.* for b.somecolumnsnames.

From 10 to 1 or 2 secs.

1 Comment

you can check it as answer then ... :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.