帮我改变这个单一的复杂查询以使用临时表

时间:2010-06-09 14:32:11

标签: mysql optimization multiple-tables

关于系统: - 有导师创建课程和包 - 正在遵循基于标签的搜索方法。当新教师注册和教师创建包时(这使得教师和包可搜索),创建了标签关系。有关详细信息,请查看标记在此系统中的工作方式部分?下方。

以下是相关查询 任何人都可以帮我建议使用临时表的方法。我们已将所有相关字段编入索引,并且看起来这是使用此方法的最短时间: -

 SELECT SUM(DISTINCT( t.tag LIKE "%Dictatorship%"
                      OR tt.tag LIKE "%Dictatorship%"
                      OR ttt.tag LIKE "%Dictatorship%" )) AS key_1_total_matches
       ,
       SUM(DISTINCT( t.tag LIKE "%democracy%"
                      OR tt.tag LIKE "%democracy%"
                      OR ttt.tag LIKE "%democracy%" ))    AS key_2_total_matches
       ,
       COUNT(DISTINCT( od.id_od ))                        AS
       tutor_popularity,
       CASE
         WHEN ( IF(( wc.id_wc > 0 ), ( wc.wc_api_status = 1
                                       AND wc.wc_type = 0
                                       AND wc.class_date > '2010-06-01 22:00:56'
                                       AND wccp.status = 1
                                       AND ( wccp.country_code = 'IE'
                                              OR wccp.country_code IN ( 'INT' )
                                           ) ), 0)
              ) THEN 1
         ELSE 0
       END                                                AS 'classes_published'
       ,
       CASE
         WHEN ( IF(( lp.id_lp > 0 ), ( lp.id_status = 1
                                       AND lp.published = 1
                                       AND lpcp.status = 1
                                       AND ( lpcp.country_code = 'IE'
                                              OR lpcp.country_code IN ( 'INT' )
                                           ) ), 0)
              ) THEN 1
         ELSE 0
       END                                                AS 'packs_published',
       td . *,
       u . *
FROM   tutor_details AS td
       JOIN users AS u
         ON u.id_user = td.id_user
       LEFT JOIN learning_packs_tag_relations AS lptagrels
         ON td.id_tutor = lptagrels.id_tutor
       LEFT JOIN learning_packs AS lp
         ON lptagrels.id_lp = lp.id_lp
       LEFT JOIN learning_packs_categories AS lpc
         ON lpc.id_lp_cat = lp.id_lp_cat
       LEFT JOIN learning_packs_categories AS lpcp
         ON lpcp.id_lp_cat = lpc.id_parent
       LEFT JOIN learning_pack_content AS lpct
         ON ( lp.id_lp = lpct.id_lp )
       LEFT JOIN webclasses_tag_relations AS wtagrels
         ON td.id_tutor = wtagrels.id_tutor
       LEFT JOIN webclasses AS wc
         ON wtagrels.id_wc = wc.id_wc
       LEFT JOIN learning_packs_categories AS wcc
         ON wcc.id_lp_cat = wc.id_wp_cat
       LEFT JOIN learning_packs_categories AS wccp
         ON wccp.id_lp_cat = wcc.id_parent
       LEFT JOIN order_details AS od
         ON td.id_tutor = od.id_author
       LEFT JOIN orders AS o
         ON od.id_order = o.id_order
       LEFT JOIN tutors_tag_relations AS ttagrels
         ON td.id_tutor = ttagrels.id_tutor
       LEFT JOIN tags AS t
         ON t.id_tag = ttagrels.id_tag
       LEFT JOIN tags AS tt
         ON tt.id_tag = lptagrels.id_tag
       LEFT JOIN tags AS ttt
         ON ttt.id_tag = wtagrels.id_tag
WHERE  ( u.country = 'IE'
          OR u.country IN ( 'INT' ) )
       AND CASE
             WHEN ( ( tt.id_tag = lptagrels.id_tag )
                    AND ( lp.id_lp > 0 ) ) THEN lp.id_status = 1
                                                AND lp.published = 1
                                                AND lpcp.status = 1
                                                AND ( lpcp.country_code = 'IE'
                                                       OR lpcp.country_code IN (
                                                          'INT'
                                                          ) )
             ELSE 1
           END
       AND CASE
             WHEN ( ( ttt.id_tag = wtagrels.id_tag )
                    AND ( wc.id_wc > 0 ) ) THEN wc.wc_api_status = 1
                                                AND wc.wc_type = 0
                                                AND
             wc.class_date > '2010-06-01 22:00:56'
                                                AND wccp.status = 1
                                                AND ( wccp.country_code = 'IE'
                                                       OR wccp.country_code IN (
                                                          'INT'
                                                          ) )
             ELSE 1
           END
       AND CASE
             WHEN ( od.id_od > 0 ) THEN od.id_author = td.id_tutor
                                        AND o.order_status = 'paid'
                                        AND CASE
             WHEN ( od.id_wc > 0 ) THEN od.can_attend_class = 1
             ELSE 1
                                            END
             ELSE 1
           END
       AND ( t.tag LIKE "%Dictatorship%"
              OR t.tag LIKE "%democracy%"
              OR tt.tag LIKE "%Dictatorship%"
              OR tt.tag LIKE "%democracy%"
              OR ttt.tag LIKE "%Dictatorship%"
              OR ttt.tag LIKE "%democracy%" )
GROUP  BY td.id_tutor
HAVING key_1_total_matches = 1
       AND key_2_total_matches = 1
ORDER  BY tutor_popularity DESC,
          u.surname ASC,
          u.name ASC
LIMIT  0, 20  

问题

上述查询返回的结果是正确的(AND逻辑按预期工作),但查询所花费的时间对于较重的数据而言惊人地上升,而对于当前数据,我认为它与正常查询时序相差10秒大约0.005 - 0.0002秒,这使它完全无法使用。

有人在我之前的问题中建议做以下事项: -

  • 创建一个临时表,并在此处插入可能最终在最终结果集中的所有相关数据
  • 在此表上运行多个更新,一次加入一个所需的表,而不是同时加入所有表
  • 最后对此临时表执行查询以提取最终结果

这一切都是在存储过程中完成的,最终结果通过了单元测试,并且速度非常快。

到目前为止,我从未使用临时表。只有我能得到一些提示,一种原理图表示,以便我可以从...开始......

查询有问题吗? 10秒以上执行时间背后的原因是什么?

标签在此系统中的工作方式?

  • 当导师注册时,会输入标签,并根据教师的详细信息(如姓名,姓氏等)创建标签关系。
  • 当教师创建包时,会再次输入标签,并根据包的名称,描述等包的详细信息创建标签关系。
  • 存储在tutors_tag_relations中的教师的标签关系以及存储在learning_packs_tag_relations中的包的标签关系。所有单个标签都存储在标签表中。

1 个答案:

答案 0 :(得分:1)

临时表不是银弹。您的查询的根本问题在于以下模式:

   t.tag LIKE "%Dictatorship%"
OR tt.tag LIKE "%Dictatorship%"
OR ttt.tag LIKE "%Dictatorship%"

通配LIKE比较的左侧可确保无法使用索引。实际上,你正在扫描涉及的所有三个表......

您需要利用全文搜索,MySQL's native FTS或Sphinx等第三方搜索。我所知道的所有FTS都包括一个得分/等级值,表示比赛的强度 - you can read the MySQL documentation for the algorithm details。但得分/等级与你得到的不同:SUM(DISTINCT LIKE...),你可以使用类似的东西得到同样的东西:

  SELECT t.id_tag, 
         COUNT(*) AS num_matches 
    FROM TABGS
   WHERE MATCH(tag) AGAINST ('Dictatorship')
GROUP BY t.id_tag