SQL 中派生表的性能问题
我在使用 MySQL 中的派生表时遇到问题。使用派生表本质上会减慢查询的处理速度吗?
这是我试图运行的查询。它不会执行并且只是超时。
它确实成功了。确实,我已将问题隔离到最后一次加入。当我取出最后一个连接时,它工作正常。但是当我将最后一个连接添加回来时,它拒绝执行。
SELECT cr.COMMUNICATIONS_ID AS ANSWER_ID,
cr.CONSUMER_ID as VIEWER_ID,
cr.ACTION_LOG_ID,
nc.PARENT_COMMUNICATIONS_ID AS QUESTION_ID,
nc.SENDER_CONSUMER_ID AS REPLIER_ID,
ces.EXPERT_SCORE AS REPLIER_EXPERTISE,
cim.CONSUMER_INTEREST_EXPERT_ID AS DOMAIN
FROM (SELECT 234 AS CONSUMER_ID,
ACTION_LOG_ID,
COMMUNICATIONS_ID
FROM consumer_action_log
WHERE COMM_TYPE_ID=4) AS cr
JOIN network_communications AS nc ON
cr.COMMUNICATIONS_ID=nc.COMMUNICATIONS_ID
JOIN communication_interest_mapping AS cim ON
nc.PARENT_COMMUNICATIONS_ID=cim.COMMUNICATION_ID
JOIN consumer_expert_score AS ces ON
nc.SENDER_CONSUMER_ID=ces.CONSUMER_ID
AND cim.CONSUMER_INTEREST_EXPERT_ID=ces.CONSUMER_EXPERT_ID;
I am having trouble using a derived table in MySQL. Does using a derived table inherently slow down the processing of a query?
Here is the query I am trying to run. It won't execute and just times out.
It does succeed. Really, I have isolated the problem to the last join. When I take out the last join it works fine. But when I add the last join back in it refuses to execute.
SELECT cr.COMMUNICATIONS_ID AS ANSWER_ID,
cr.CONSUMER_ID as VIEWER_ID,
cr.ACTION_LOG_ID,
nc.PARENT_COMMUNICATIONS_ID AS QUESTION_ID,
nc.SENDER_CONSUMER_ID AS REPLIER_ID,
ces.EXPERT_SCORE AS REPLIER_EXPERTISE,
cim.CONSUMER_INTEREST_EXPERT_ID AS DOMAIN
FROM (SELECT 234 AS CONSUMER_ID,
ACTION_LOG_ID,
COMMUNICATIONS_ID
FROM consumer_action_log
WHERE COMM_TYPE_ID=4) AS cr
JOIN network_communications AS nc ON
cr.COMMUNICATIONS_ID=nc.COMMUNICATIONS_ID
JOIN communication_interest_mapping AS cim ON
nc.PARENT_COMMUNICATIONS_ID=cim.COMMUNICATION_ID
JOIN consumer_expert_score AS ces ON
nc.SENDER_CONSUMER_ID=ces.CONSUMER_ID
AND cim.CONSUMER_INTEREST_EXPERT_ID=ces.CONSUMER_EXPERT_ID;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
希望这会有所帮助...这是一些 mysql CREATE INDEX 语句。基本上,如果您可以添加索引,请确保有一个索引覆盖连接 2 个或更多表的每个列。
派生表本身并不坏,但在这种情况下(见下文),您将从 Consumer_action_log 中提取 comm_type_id 为 4 的所有记录。似乎没有返回到其他表的连接。这可能是sql永远不会返回的原因。
Hope this helps... Here's some mysql CREATE INDEX statements. Basically, if you can add indexes, make sure there's an index that covers each of your columns that connect 2 or more tables.
Derived tables aren't inherently bad, but in this case (see below) you're pulling all the records from consumer_action_log that have a comm_type_id of 4. There doesn't seem to be a connection back to the other tables. That might be the cause of the sql never returning.
除了 John 在 anser 中指出的查找表中应存在的索引之外,我还确保您的 Consumer_action_log 表中也有 COMM_TYPE_ID 上的索引。
然后,在您的子句中添加一个关键字...当查询组织良好而不是依赖查询引擎来优化时,我总是看到很好的结果...在这里查看另一个示例
它可能是优化器正在尝试查看其他表以找出要获取的内容。请参阅我提供链接的其他 StackOverflow 答案中的评论。
Aside from the indexes that should exist on your lookup tables as pointed out in anser by John, I would ensure you have an index on COMM_TYPE_ID in your consumer_action_log table too.
Then, add one keyword to your clause... I've always seen great results when a query is well organized instead of relying on the query engine to optimze...see another sample here
It might be the optimizer is trying to look at other tables to figure out what to get. See comments in other StackOverflow answer I've provided link to.