使用LIMIT进行MySQL定时

时间:2018-03-15 10:43:00

标签: mysql phpmyadmin

我有一张包含73,000条记录的表格。

当我运行此查询时:

SELECT id, first_name, email 
FROM candidates 
WHERE first_name != 'DC3101 -' AND sent IS null

它在0.0022秒内完成并返回9,000条记录。我只想要4000个结果,所以我跑了这个:

SELECT id, first_name, email 
FROM candidates 
WHERE first_name != 'DC3101 -' AND sent IS null LIMIT 4000

然而,这会导致脚本超时并崩溃(我正在使用phpMyAdmin)。

LIMIT条款是否存在我可能不知道的特定性能问题?

1 个答案:

答案 0 :(得分:1)

我无法复制这种行为(无论如何都在InnoDB中)......

DROP TABLE IF EXISTS candidates;

CREATE TABLE candidates 
(id SERIAL PRIMARY KEY
,first_name VARCHAR(20) NOT NULL
,sent TINYINT NULL
);

SELECT COUNT(*) total, SUM(first_name = 'DC3101') dc FROM candidates;
+-------+-------+
| total | dc    |
+-------+-------+
| 73000 | 64000 |
+-------+-------+
1 row in set (0.03 sec)

SELECT id,first_name FROM candidates WHERE first_name != 'DC3101' AND sent IS NULL ORDER BY id LIMIT 4000;
+-------+------------+
| id    | first_name |
+-------+------------+
| 64001 | IJ7686     |
...
| 68000 | XI8405     |
+-------+------------+
4000 rows in set (0.16 sec)

表格可能已损坏。