我在运行此查询时遇到问题:
select * from logs l
where l.id in(select max(id)
from logs
group by usuario_id); 在这个结构上:
+-----------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| usuario_id | int(11) | YES | MUL | NULL | |
| projeto_id | int(11) | YES | MUL | NULL | |
| title | varchar(500) | YES | | NULL | |
+-----------------+--------------+------+-----+-------------------+----------------+我试着在3个不同的服务器上运行这个查询:
mysql> show variables like "%version%";
+-------------------------+-------------------------+
| Variable_name | Value |
+-------------------------+-------------------------+
| innodb_version | 5.5.40 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.5.40-0ubuntu0.12.04.1 |
| version_comment | (Ubuntu) |
| version_compile_machine | x86_64 |
| version_compile_os | debian-linux-gnu |
+-------------------------+-------------------------+这是我的个人电脑,还有另外两台:
+-------------------------+-------------------------+
| Variable_name | Value |
+-------------------------+-------------------------+
| innodb_version | 5.5.40 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.5.40-0ubuntu0.14.04.1 |
| version_comment | (Ubuntu) |
| version_compile_machine | x86_64 |
| version_compile_os | debian-linux-gnu |
+-------------------------+-------------------------+托管在digitalocean和亚马逊。
在这些系统上,mysql进程在发送此查询时保持100%运行,并且只有在手动停止查询时才会停止...
奇怪的是,这个查询在windows (使用wampp mysql)上运行没有任何问题。
mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+------------------------------+
| Variable_name | Value |
+-------------------------+------------------------------+
| innodb_version | 5.6.12 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.6.12-log |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | Win64 |
+-------------------------+------------------------------+这是MySQL版本的问题还是操作系统的问题?
发布于 2014-11-17 22:52:48
听起来你在ubuntu sql数据库中有一堆数据,每个数据库表中有多少行?如果您正在为每个usuario_id查找最新的标题,那么运行此程序将大大减少查询时间,然后遍历usuario_ids并仅在需要时获取logs.id列。varchar(500)是大量的数据。
select l.id,l.usuario_id from logs l
where l.id in(select max(id)
from logs
group by usuario_id); 发布于 2014-11-17 22:57:39
虽然这不是对您的问题的直接回答,但可以重写查询以删除子查询:
SELECT l.*
FROM logs l
LEFT JOIN logs lo
ON lo.id > l.id
AND lo.usuario_id = l.usuario_id
WHERE lo.id IS NULL这至少有助于您在步骤中隔离问题的原因。
另一个(显而易见的)步骤是使用MySQL v5.5.40对Windows中的相同数据运行原始查询
https://stackoverflow.com/questions/26974924
复制相似问题