gpt4 book ai didi

mysql - 优化sql查询

转载 作者:行者123 更新时间:2023-12-01 00:43:31 25 4
gpt4 key购买 nike

我的查询最多需要 40 秒才能执行,我想知道如何让它更快......

SELECT rp.`id` as rip,r.`id`, 
rl.`id` as rlid, r.`number`, r.`type`,
a.`title` as atitle, a.`id` as aid, more,
more_valid
FROM
`R_l_p` rp INNER JOIN
`Rl` rl ON rp.`rl_id` = rl.`id` INNER JOIN
`R` r ON r.`id` = rl.`r_id` INNER JOIN
`A` a ON a.`id` = r.`a_id`
LEFT JOIN
(SELECT `type`, `v`, `r_id`, COUNT(*) AS more
FROM `R_l`
WHERE `type` = 'stream' AND `v` = 1
GROUP BY `r_id`) stm ON stm.`r_id` = r.`id`
LEFT JOIN
(SELECT `type`, `v`, `r_id`, COUNT(*) AS more_valid
FROM `R_l`
WHERE `type` = 'stream' AND `v` = 0
GROUP BY `r_id`) morelink ON morelink.`r_id` = r.`id`
WHERE rp.`link` = 'dead' AND rl.`type` = 'stream'
ORDER BY rip ASC
LIMIT 0, 1000
+-----+--------------+---------------+---------+--------------------------+-------------+----------+---------------------------+--------+----------------------------------------------+| id  | select_type  |    table      |  type   |      possible_keys       |    key      | key_len  |           ref             | rows   |                    Extra                     |+-----+--------------+---------------+---------+--------------------------+-------------+----------+---------------------------+--------+----------------------------------------------+|  1  | PRIMARY      | r             | ALL     | PRIMARY,a_id             | NULL        | NULL     | NULL                      | 21804  | Using temporary; Using filesort              ||  1  | PRIMARY      | a             | eq_ref  | PRIMARY                  | PRIMARY     | 4        | db453988339.r.a_id        |     1  |                                              ||  1  | PRIMARY      | rl            | ref     | PRIMARY,r_id,type        | r_id        | 4        | db453988339.r.id          |     1  | Using where                                  ||  1  | PRIMARY      | derived2      | ALL     | NULL                     | NULL        | NULL     | NULL                      | 21077  |                                              ||  1  | PRIMARY      | derived3      | ALL     | NULL                     | NULL        | NULL     | NULL                      |     1  |                                              ||  1  | PRIMARY      | rp            | eq_ref  | rl_id                    | rl_id       | 4        | db453988339.rl.id         |     1  | Using where                                  ||  3  | DERIVED      | R_link        | ALL     | type                     | NULL        | NULL     | NULL                      | 27580  | Using where; Using temporary; Using filesort ||  2  | DERIVED      | R_link        | ALL     | type                     | NULL        | NULL     | NULL                      | 27580  | Using where; Using temporary; Using filesort |+-----+--------------+---------------+---------+--------------------------+-------------+----------+---------------------------+--------+----------------------------------------------+

谢谢你:)

最佳答案

一方面,我将用一个内联 View 替换两个内联 View ,通过将条件从WHERE 子句插入 SELECT 列表中的表达式),并消除 more_link View 。

我会将 stm 内联 View 修改为如下所示:

       ( SELECT q.r_id
, SUM(q.v=1) AS `more`
, SUM(q.v=0) AS `more_valid`
FROM `R_l` q
WHERE q.type = 'stream'
GROUP BY q.r_id
) stm

我还会提供一个覆盖索引来优化该内联 View 查询,例如:

    CREATE INDEX R_1_IX1 ON R_1 (type, r_id, v)

在前导列 type 上使用相等谓词,MySQL 可以使用索引优化 GROUP BY 操作(避免“使用文件排序”操作)。我们希望在解释的额外列中看到“使用索引”。

不幸的是,派生表不会被索引。但至少通过这一更改,您只具体化了一个内联 View ,避免了额外的连接操作。


也就是说,我将替换这两行:

LEFT JOIN (SELECT `type`, `v`, `r_id`, COUNT(*) AS more FROM `R_l` WHERE `type` = 'stream' AND `v` = 1 GROUP BY `r_id`) stm ON stm.`r_id` = r.`id`
LEFT JOIN (SELECT `type`, `v`, `r_id`, COUNT(*) AS more_valid FROM `R_l` WHERE `type` = 'stream' AND `v` = 0 GROUP BY `r_id`) morelink ON morelink.`r_id` = r.`id`

有了这个:

  LEFT
JOIN ( SELECT q.r_id
, SUM(q.v=1) AS `more`
, SUM(q.v=0) AS `more_valid`
FROM `R_l` q
WHERE q.type = 'stream'
GROUP BY q.r_id
) stm
ON stm.r_id = r.id

关于mysql - 优化sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25983653/

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