gpt4 book ai didi

mysql - 帮助我更改这个单一的复杂查询以使用临时表

转载 作者:行者123 更新时间:2023-11-29 15:01:21 25 4
gpt4 key购买 nike

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

以下是相关查询任何人都可以帮我建议一种使用临时表的方法。我们已经对所有相关字段建立了索引,看起来这是使用这种方法可能花费的最短时间:-

 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

问题

上述查询返回的结果是正确的(并且逻辑按照预期工作),但是对于较重的数据,查询所花费的时间会惊人地增加,而对于我拥有的当前数据,与正常查询时间相比,查询所花费的时间大约为 10 秒大约 0.005 - 0.0002 秒,这使得它完全无法使用。

有人在我之前的问题中建议执行以下操作:-

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

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

到目前为止我从未使用过临时表。只有我能得到一些提示,某种示意图,这样我才能开始......

查询有问题吗?执行时间超过 10 秒的原因是什么?

标签在该系统中如何工作?

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

最佳答案

临时表并不是 Elixir 。您的查询的根本问题在于这样的模式:

   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

关于mysql - 帮助我更改这个单一的复杂查询以使用临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3006801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com