SQL 中派生表的性能问题

发布于 2024-10-14 03:00:45 字数 944 浏览 2 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-10-21 03:00:45

希望这会有所帮助...这是一些 mysql CREATE INDEX 语句。基本上,如果您可以添加索引,请确保有一个索引覆盖连接 2 个或更多表的每个列。

CREATE INDEX idx_nc
ON network_communications(COMMUNICATIONS_ID);

CREATE INDEX idx_cim
ON communication_interest_mapping(COMMUNICATION_ID);

CREATE INDEX idx_ces
ON consumer_expert_score(CONSUMER_ID, CONSUMER_EXPERT_ID);

派生表本身并不坏,但在这种情况下(见下文),您将从 Consumer_action_log 中提取 comm_type_id 为 4 的所有记录。似乎没有返回到其他表的连接。这可能是sql永远不会返回的原因。

SELECT cr.COMMUNICATIONS_ID, 
           cr.CONSUMER_ID, 
           cr.ACTION_LOG_ID, 
           nc.PARENT_COMMUNICATIONS_ID, 
           nc.SENDER_CONSUMER_ID, 
           ces.EXPERT_SCORE, 
           cim.CONSUMER_INTEREST_EXPERT_ID

    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;

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.

CREATE INDEX idx_nc
ON network_communications(COMMUNICATIONS_ID);

CREATE INDEX idx_cim
ON communication_interest_mapping(COMMUNICATION_ID);

CREATE INDEX idx_ces
ON consumer_expert_score(CONSUMER_ID, CONSUMER_EXPERT_ID);

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.

SELECT cr.COMMUNICATIONS_ID, 
           cr.CONSUMER_ID, 
           cr.ACTION_LOG_ID, 
           nc.PARENT_COMMUNICATIONS_ID, 
           nc.SENDER_CONSUMER_ID, 
           ces.EXPERT_SCORE, 
           cim.CONSUMER_INTEREST_EXPERT_ID

    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;
美人迟暮 2024-10-21 03:00:45

除了 John 在 anser 中指出的查找表中应存在的索引之外,我还确保您的 Consumer_action_log 表中也有 COMM_TYPE_ID 上的索引。

然后,在您的子句中添加一个关键字...当查询组织良好而不是依赖查询引擎来优化时,我总是看到很好的结果...在这里查看另一个示例

SELECT STRAIGHT_JOIN
         cr.COMMUNICATIONS_ID AS ANSWER_ID,
         cr.CONSUMER_ID as VIEWER_ID,  
         etc... rest of your query...

它可能是优化器正在尝试查看其他表以找出要获取的内容。请参阅我提供链接的其他 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

SELECT STRAIGHT_JOIN
         cr.COMMUNICATIONS_ID AS ANSWER_ID,
         cr.CONSUMER_ID as VIEWER_ID,  
         etc... rest of your query...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文