[英]SQL - Make Join Query Faster
I've made a query that selects 2 values from 2 tables.我做了一个查询,从 2 个表中选择 2 个值。 I need to run this query about 32 times when a visitor visits my website.
当访问者访问我的网站时,我需要运行此查询大约 32 次。 This makes the page quite slow (it takes over 5 seconds to fully load).
这使得页面非常慢(完全加载需要超过 5 秒)。
The query looks like this:查询如下所示:
SELECT tmdb.name, patch.sfo_title
FROM tmdb
RIGHT JOIN patch
ON tmdb.titleid = CONCAT(patch.cusa, '_00')
WHERE cusa = :titleid
LIMIT 1
is there any way to make this query faster?有什么办法可以让这个查询更快? The query isn't the biggest operation if I look at it, so I'm not really sure why it's so slow?
如果我查看它,查询并不是最大的操作,所以我不确定为什么它这么慢?
I would write the query as a left join
(out of preference, not performance):我会将查询写为
left join
(出于偏好,而不是性能):
SELECT t.name, p.sfo_title
FROM patch p LEFT JOIN
tmdb t
ON t.titleid = CONCAT(p.cusa, '_00')
WHERE p.cusa = :titleid
LIMIT 1;
Then for performance, I would recommend indexes on patch(cusa, sfo_title)
and t(titleid)
.然后为了性能,我会推荐
patch(cusa, sfo_title)
和t(titleid)
上的索引。
Note that the use of LIMIT
without ORDER BY
is suspicious, although you might have reasons for it.请注意,使用没有
ORDER BY
的LIMIT
是可疑的,尽管您可能有理由这样做。
You are joining two tables based on a CALCULATED field?您要基于一个 CALCULATED 字段连接两个表吗? No wonder it is slow.
难怪它很慢。 I have no idea how your tables get maintained, but you need to get that concat'ed value of CUSA into the data base as a separate field and get it indexed, as Gordon Linoff suggested.
我不知道您的表是如何维护的,但是您需要将 CUSA 的连接值作为一个单独的字段放入数据库中并对其进行索引,正如 Gordon Linoff 所建议的那样。 You could even maintain it through On Insert and On Update triggers.
您甚至可以通过 On Insert 和 On Update 触发器对其进行维护。 Personally, I would examine why you have similar but different keys in your two tables and try to rationalize it down to one.
就个人而言,我会检查为什么您的两张表中有相似但不同的键,并尝试将其合理化为一个。 That Concat(CUSA, '_00' ) looks suspiciously like an opportunity to simplify the application.
Concat(CUSA, '_00' ) 看起来像是一个简化应用程序的机会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.