>我在运行此查询时遇到问题:
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 |
+-------------------------+-------------------------+
这是我的个人电脑,另外2台:
+-------------------------+-------------------------+
| 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和Amazon上。
在这些系统上,发送此查询时,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版本的问题还是操作系统的问题?
听起来你在 ubuntu sql 数据库中有一堆数据,每个数据库表中有多少行?如果要查找每个usuario_id的最新 logs.id,则应大大减少运行此查询的查询时间,然后循环访问usuario_ids,并仅在需要时获取标题列。Varchar(500) 是很多数据。
select l.id,l.usuario_id from logs l
where l.id in(select max(id)
from logs
group by usuario_id);
虽然这不是对您的问题的直接回答,但可以重写查询以删除子查询:
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 中的相同数据运行原始查询