gpt4 book ai didi

mysql - 如何首先且仅运行一次子查询

转载 作者:行者123 更新时间:2023-11-29 01:13:49 24 4
gpt4 key购买 nike

如何让 MySQL 首先运行子查询并且只运行一次?现在 MySQL 对表 t1 中的每一行运行内部查询,这是一个性能灾难。

explain select * from t1 where uid in (select id from t0);
+----+--------------------+-------+------+---------------+------+---------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+------+---------------+------+---------+------+----------+-------------+
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 18954249 | Using where |
| 2 | DEPENDENT SUBQUERY | t0 | ALL | NULL | NULL | NULL | NULL | 12749 | Using where |
+----+--------------------+-------+------+---------------+------+---------+------+----------+-------------+

最佳答案

如果您执行内部联接,您将获得所需的结果。

来自:

select * from t1 where uid in (select id from t0);

收件人:

select * from t1 join t0 on t1.t0id = t0.id

http://en.wikipedia.org/wiki/Join_(SQL)#Inner_join

关于mysql - 如何首先且仅运行一次子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16121305/

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