【发布时间】:2015-03-18 13:48:51
【问题描述】:
我有一个在 ARM 机器上运行的 MySQL 服务器,我有一个通过 Web 界面(或网站上的 php)在 phpmyadmin 中运行的查询
select * from some_table where id IN (select id from other_table)
这个简单的查询会导致 MySQL 服务挂起,直到重启。 是什么原因?
换句话说:
如果我手动输入 id - 无论是 1 还是 100 - 它都不会失败:
select * from some_table where id IN (1,2,3,4,5,6,7,8,9....120)
另一方面 - 如果 ids 是从内部查询生成的,那么服务将挂起:
select * from some_table where id IN (select id from other_table)
我拥有对服务器的完全访问权限,并且可以根据需要对其进行重新配置。我该如何解决这个问题?
数据库服务器
Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.5.35-1ubuntu1 - (Ubuntu)
Protocol version: 10
User: root@localhost
Server charset: UTF-8 Unicode (utf8)
网络服务器
nginx/1.4.6
Database client version: libmysql - 5.5.35
PHP extension: mysqli
phpMyAdmin
Version information: 4.0.10deb1
附:我所说的挂起是指mysql 变得无响应。命令top 将显示 MySQL 服务正在耗尽所有内核。通过命令sudo /etc/init.d/mysql restart完成重启。
附言挂起的完整查询。小提琴:http://sqlfiddle.com/#!9/84fe8/1
select
goo.t1_score,
goo.t2_score,
gr.t1_score,
gr.t2_score,
gr.unique_key
from games_ongoing goo, game_results gr
where
goo.id in (
select max(id)
from games_ongoing
group by unique_key
having count(id) >= 3 and MIN(t1_score) = MIN(t2_score) and MIN(t1_score) = 0
)
and gr.unique_key = goo.unique_key
【问题讨论】:
-
你正在使用子查询,这就是为什么
-
你为什么要这样做? JOIN 有什么问题?
-
使用子查询有什么问题?我整天都在 oracle 中这样做
-
@Strawberry 在完整的子查询中,我还使用了 GROUP BY 语句。甚至可以将其转换为 JOIN 吗?
-
您的查询不太有意义(除非两个表之间可能存在 1-1 相关性,但 MAX 将没有意义)。考虑遵循这个简单的两步课程行动: 1. 如果您还没有这样做,请提供适当的 DDL(和/或 sqlfiddle),以便我们可以更轻松地复制问题。 2. 如果您尚未这样做,请提供与步骤 1 中提供的信息相对应的所需结果集。
标签: php mysql phpmyadmin server