- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
查看查询的 EXPLAIN
计划,如何确定最适合优化的地方?
我很欣赏首先要检查的事情之一是是否使用了良好的索引,但除此之外我有点难过。通过过去的反复试验,我有时发现执行连接的顺序可能是一个很好的改进来源,但如何通过查看执行计划来确定这一点?
虽然我非常希望对如何优化查询有一个很好的总体理解(非常感谢推荐阅读!),但我也意识到讨论具体案例通常比抽象讨论更容易。由于我目前正在用这个问题撞墙,非常感谢您的想法:
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE S const PRIMARY,l,p,f4 PRIMARY 2 const 1 Using temporary 1 SIMPLE Q ref PRIMARY,S S 2 const 204 Using index 1 SIMPLE V ref PRIMARY,n,Q Q 5 const,db.Q.QID 6 Using where; Using index; Distinct 1 SIMPLE R1 ref PRIMARY,L L 154 const,db.V.VID 447 Using index; Distinct 1 SIMPLE W eq_ref PRIMARY,w PRIMARY 5 const,db.R.RID,const 1 Using where; Distinct 1 SIMPLE R2 eq_ref PRIMARY,L PRIMARY 156 const,db.W.RID,const 1 Using where; Distinct
Am I correct in interpreting the final row of the execution plan as follows:
R2
need be fetched per output row;R2
?If so, my problem lies in the filtering which occurs in that final step. If the condition results in no filtering (e.g. WHERE `Col_1_to_3` IN (1,2,3)
), the query runs extremely quickly (~50ms); however, if the condition restricts the rows selected (WHERE `Col_1_to_3` IN (1,2)
), the query takes considerably longer (~5s). If the restriction is to a single match (WHERE `Col_1_to_3` IN (1)
), the optimiser suggests an altogether different execution plan (which performs marginally better than 5s, but still a lot worse than 50ms). It doesn't seem as though there's a better index that can be used on that table (given it's already fully using the primary key to return one row per result?).
How should one interpret all this information? Am I right in guessing that, because such output filtering is taking place on the final table to be joined, considerable effort is wasted versus joining the table earlier and filtering such rows sooner? If so, how does one determine when in the execution plan R2
ought to be joined?
Whilst I resisted including the query & schema in full here (as I would really likely to know what to look for, not merely be told the answer), I understand it's necessary to advance the discussion:
SELECT DISTINCT
`Q`.`QID`
FROM
`S`
NATURAL JOIN `Q`
NATURAL JOIN `V`
NATURAL JOIN `R` AS `R1`
NATURAL JOIN `W`
JOIN `R` AS `R2` ON (
`R2`.`SID` = `S`.`SID`
AND `R2`.`RID` = `R1`.`RID`
AND `R2`.`VID` = `S`.`V_id`
AND `R2`.`Col_1_to_3` IN (1,2) -- this is where performance suffers!
)
WHERE
AND `S`.`SID` = @x
AND `W`.`WID` = @y
;
表R
的定义是:
CREATE TABLE `R` (
`SID` smallint(6) unsigned NOT NULL,
`RID` smallint(6) unsigned NOT NULL,
`VID` varchar(50) NOT NULL DEFAULT '',
`Col_1_to_3` smallint(1) DEFAULT NULL,
`T` varchar(255) DEFAULT NULL,
PRIMARY KEY (`SID`,`RID`,`VID`),
KEY `L` (`SID`,`VID`,`Col_1_to_3`),
CONSTRAINT `R_f1` FOREIGN KEY (`SID`) REFERENCES `S` (`SID`),
CONSTRAINT `R_f2` FOREIGN KEY (`SID`, `VID`) REFERENCES `V` (`SID`, `VID`),
CONSTRAINT `R_f3` FOREIGN KEY (`SID`, `VID`, `Col_1_to_3`) REFERENCES `L` (`SID`, `VID`, `LID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
最佳答案
取决于您要做什么以及查询是什么。
通常,对于 EXPLAIN 中具有 Using where
的每一行,您需要使用索引(possible keys
和 keys
柱子)。这些是您的过滤器,包括 WHERE 和 ON。说 Using index
就更好了。这意味着有一个覆盖索引,MySQL 可以直接从索引中检索数据,而不是访问表数据中的行。
应该查看没有 Using where
且返回大量行的行。这些是表中所有行的返回值。我不知道你的查询是什么,所以我不知道是否要在这里 panic 。尝试过滤结果集以减小大小并提高性能。
您通常应该尽量避免看到 Using filesort
或 Using temporary
,尽管只有在您不希望看到它们时它们才不好。
Filesort 通常与 ORDER 子句一起出现。您通常希望 MySQL 使用覆盖索引(Using index
),以便已按顺序从服务器返回行。如果不是,则 MySQL 必须随后使用文件排序对它们进行排序。
使用临时
引用派生表时可能会很糟糕,因为它们没有索引。看起来你已经明确地创建了一个带索引的临时表,所以在这里,它还不错。有时,您唯一的选择是使用派生表,因此 Using temporary
。
关于mysql - 如何基于EXPLAIN计划优化MySQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10148851/
INSERT 或 UPDATE 语句中的 EXPLAIN 关键字是执行查询,还是只是为您显示(“解释”)查询 - MySQL 新手,无法足够快地找到问题。 最佳答案 Reference说 The EX
这两个命令有什么区别? db.collection.explain().find() db.collection.find().explain() 最佳答案 正在运行 db.collection.ex
SQLite Explain(解释) 在SQLite 语句之前,可以使用 “EXPLAIN” 关键字或 “EXPLAIN QUERY PLAN” 短语,用于描述表的细节。 如果省略了 EXPLAI
我有一个查询,在 WHERE 子句之后有几个 filter 条件。 此外,大多数涉及的列都有索引。 当我运行 EXPLAIN 命令时,我看到: -> Bitmap Index Scan on fea
我需要实现对 MySQL EXPLAIN 命令输出的自动分析,它将查询标记为“坏”(例如,如果不使用索引)、“中等”(可以优化)和“好” ”。 是否有任何现有的解决方案或任何算法来实现它? 最佳答案
我在 SQL 字符串中的查询前面添加了 EXPLAIN 我在本地服务器上运行的网站的网页 但是我如何才能看到 EXPLAIN 的输出? Echo 和 print_r 都返回:资源 id #33 我如何
我有一个快速且非常简单的问题。 我有一个包含以下 SQL 的表: CREATE TABLE `users` ( `id` int(20) NOT NULL AUTO_INCREMENT, `use
我有一个缓慢的 MySQl 查询,大约需要 15 秒才能运行。所以我做了一些调查,发现我可以使用 EXPLAIN 语句来查看瓶颈在哪里。所以我这样做了,但真的无法破译这些结果。 如果我不得不尝试一下,
我在数据库中有一个“posts”表,它在 user_id 上有非唯一索引(键:MUL)。 mysql> show columns from posts; +---------+------------
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我有一个非常大、复杂的查询,我正在尝试使用 MySQL EXPLAIN SELECT 或 EXPLAIN EXTENDED SELECT 进行优化。 如果我针对查询运行它,我会看到查询中的每个表都在
8.2.2. EXPLAIN Output Format基于以下 SQL 查询的连续优化,给出了几个 EXPLAIN 示例: EXPLAIN SELECT tt.TicketNumber, tt.Ti
下面的查询完全符合我的预期,它很直观并且不会生成中间表。缺点是需要很长时间才能完成。 在这种情况下,我要做的是逐步分解查询并创建那些中间表和索引。这一次,我想更好地处理 explain 提供的提示,并
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显。关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情。当我们去设计数据库表结构,对操作数
Mysql Explain 这里做一个资料的全面整理。 一.语法 explain < table_name > 例如: explain select * from t3 where
MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP)。这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL语句的。这条命令并没有提供任何调整建议,但它能够提供重要的信息
使用方法,在select语句前加上explain就可以了: 如:explain select * from test1 EXPLAIN列的解释: table:显示
我正在使用 python shap包以更好地理解我的机器学习模型。 (来自 documentation:“SHAP(SHpley Additive exPlanations)是一种解释任何机器学习模型
如标题。我想知道我的查询是否优化得很好。 最佳答案 是的,在Cassandra 1.2中,您可以打开request tracing进行查询。 关于optimization - 有没有办法 "EXPLA
如何使用 DB2 的 Explain 功能? -- 既可以运行它,也可以使用它来优化查询。是否有更好的工具可用于 DB2? 我以前构建过查询,但我必须知道它们需要多长时间的唯一方法是运行它们并为它们计
我是一名优秀的程序员,十分优秀!