0

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

以下是相关查询 谁能帮我建议一种使用临时表的方法。我们已经为所有相关字段编制了索引,看起来这是使用这种方法的最短时间:-

 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 中的包的标签关系。所有单个标签都存储在标签表中。
4

1 回答 1

1

临时表不是灵丹妙药。您的查询的根本问题在于这样的模式:

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

LIKE 比较左侧的通配符保证不能使用索引。实际上,您正在扫描所涉及的所有三个表...

您需要利用全文搜索,无论是MySQL 的原生 FTS还是像 Sphinx 这样的第三方工具。我所知道的所有 FTS 都包含一个表示匹配强度的得分/排名值 -您可以阅读 MySQL 文档以获取算法详细信息。但是分数/排名与您所获得的不同:SUM(DISTINCT LIKE...),您可以使用以下内容获得相同的结果:

  SELECT t.id_tag, 
         COUNT(*) AS num_matches 
    FROM TABGS
   WHERE MATCH(tag) AGAINST ('Dictatorship')
GROUP BY t.id_tag
于 2010-06-09T15:38:19.293 回答