K
K
klinnov2017-04-10 18:44:39
MySQL
klinnov, 2017-04-10 18:44:39

Is there any way to optimize the following query?

Is there any way to optimize the following query? Thank you!

SELECT t_ps.*, cl.*, c.*, ml.description, m.name as m_name
FROM `tt_payment_settings`    t_ps
  JOIN `tt_card`                c ON t_ps.id_card = c.id_card
  JOIN `tt_card_lang`            cl ON cl.id_card = c.id_card
  JOIN `tt_module`            m USING (id_module)
  JOIN `tt_module_lang`        ml ON ml.id_module = t_ps.id_module

WHERE IF(
    (
      SELECT COUNT(t_ps.id_payment_settings)
      FROM `tt_payment_settings`    t_ps
        JOIN `tt_card`                c ON t_ps.id_card = c.id_card
        JOIN `tt_card_lang`            cl ON cl.id_card = c.id_card
        JOIN `tt_module`            m USING (id_module)
        JOIN `tt_module_lang`        ml ON ml.id_module = t_ps.id_module

      WHERE t_ps.id_shop = 651
            AND t_ps.id_season = 7
            AND cl.id_lang = 1
            AND ml.id_lang = 1
    )>0
    , t_ps.id_shop = 651
      AND t_ps.id_season = 7
      AND cl.id_lang = 1
      AND ml.id_lang = 1
    , t_ps.id_shop = 651
      AND t_ps.id_season IS NULL
      AND cl.id_lang = 1
      AND ml.id_lang = 1
)

--- UPD
Changed it like this:
SELECT tt.*, cl.*, c.*, ml.description, m.name as m_name
FROM `tt_payment_settings`    ps
  JOIN `tt_card`                c ON tt.id_card = c.id_card
  JOIN `tt_card_lang`            cl ON cl.id_card = c.id_card
  JOIN `tt_module`            m USING (id_module)
  JOIN `tt_module_lang`        ml ON ml.id_module = tt.id_module

WHERE tt.id_shop = 651
      AND cl.id_lang = 1
      AND ml.id_lang = 1
      AND tt.goo_active = 1
      AND
      IF(
          EXISTS(SELECT *
                 FROM `tt_payment_settings`    ps
                 WHERE tt.id_shop = 651
                       AND tt.id_season = 7)

          ,tt.id_season = 7
          ,tt.id_season IS NULL
      )

If it's possible to do something better, I'll be glad to see

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom Karetnikov, 2017-04-11
@art_karetnikov

At least 10 days of gauptic watch must be issued for checking the availability of a record through count. Yuzai exists.
As much as possible, try to explain what exactly you are trying to do here. Because this design looks disgusting.
Correct code is concise and concise. If there are duplications of the same thing, then most likely there are jambs.

A
Anton Anton, 2017-04-11
@Fragster

replace if with a union of two selects. Exists has been written about before.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question