gpt4 book ai didi

mysql - 如何改善MariaDB索引表的慢查询?

转载 作者:行者123 更新时间:2023-11-29 06:28:36 26 4
gpt4 key购买 nike

此查询平均需要 2/3 秒。 join 和 where 中的每个字段都是索引。

如何改进此查询?

SELECT DISTINCT pro_id, pro_url, pro_data, pro_capa, pro_destaque, pro_destaque_data, pro_nome, pro_likes, pro_views, pro_comentarios_total, pro_autor FROM projeto JOIN utilizador_projeto ON pro_id=utp_proid JOIN utilizador ON utp_utiid=uti_id WHERE pro_activo=1 AND pro_privacidade=1 ORDER BY pro_destaque_data DESC LIMIT 24;

慢速查询日志:

# Time: 190923  1:10:58
# User@Host: root[root] @ [10.133.247.241]
# Thread_id: 36 Schema: db QC_hit: No
# Query_time: 3.575462 Lock_time: 0.000114 Rows_sent: 24 Rows_examined: 104820
# Rows_affected: 0 Bytes_sent: 3974

MariaDB [db]> EXPLAIN SELECT DISTINCT pro_id, pro_url, pro_data, pro_capa, pro_destaque, pro_destaque_data, pro_nome, pro_likes, pro_views, pro_comentarios_total, pro_autor FROM projeto JOIN utilizador_projeto ON pro_id=utp_proid JOIN utilizador ON utp_utiid=uti_id WHERE pro_activo=1 AND pro_privacidade=1 ORDER BY pro_destaque_data DESC LIMIT 24;
+------+-------------+--------------------+--------+-----------------------------------------------------------------------------------------------+---------------------+---------+---------------------------------+-------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+--------------------+--------+-----------------------------------------------------------------------------------------------+---------------------+---------+---------------------------------+-------+------------------------------+
| 1 | SIMPLE | projeto | ref | PRIMARY,pro_destaques_index,pro_likes_index,pro_comments_index,pro_views_index,pro_date_index | pro_destaques_index | 2 | const,const | 17865 | Using where; Using temporary |
| 1 | SIMPLE | utilizador_projeto | ref | utp_utiid,utp_proid | utp_proid | 4 | db.projeto.pro_id | 1 | Distinct |
| 1 | SIMPLE | utilizador | eq_ref | PRIMARY | PRIMARY | 4 | db.utilizador_projeto.utp_utiid | 1 | Using index; Distinct |
+------+-------------+--------------------+--------+-----------------------------------------------------------------------------------------------+---------------------+---------+---------------------------------+-------+------------------------------+

最佳答案

    WHERE  pro_active=1
AND pro_privacy=1
ORDER BY pro_highlight_date DESC

请求这个“综合”索引:

INDEX(pro_active, pro_privacy,   -- in either order
pro_highlight_date)

可能Rows_examined:139482将下降到Rows_examined:24

假设这 3 列位于同一个表中。不要用表名作为列名前缀,而是执行以下操作:

...
FROM project AS pro
...
WHERE pro.active = 1 ...

这消除了任何歧义(并且需要大约相同的击键次数)。

Every fields in join and where are indexes

杀伤力太大了​​。而且单列索引通常不如多列(复合)索引有用。请参阅:http://mysql.rjweb.org/doc.php/index_cookbook_mysql

关于mysql - 如何改善MariaDB索引表的慢查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57999866/

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