简体   繁体   English

存在查询需要很长时间

[英]query with exist take so long

From my debian terminal I try to execute in mysql client a query like:从我的 debian 终端,我尝试在 mysql 客户端中执行如下查询:

SELECT * 
FROM stop_times_lazio_set2_withtime2 AS B  
WHERE EXISTS 
(SELECT * 
FROM stop_times_lazio_set2_emptytime2 AS A 
WHERE B.trip_id=A.trip_id);

table A contains around 3 million records. table A包含大约 300 万条记录。 table B is a sub set of A of around 400000 records. table B是 A 的子集,大约有 400000 条记录。

I'd like to select every records of A thats have a row "parent" with the same id (yes its not an unique/primary id)我想选择 A 的每条记录,它都有一行具有相同 ID 的“父”(是的,它不是唯一/主 ID)

Now it takes more than hours...now I'm around 2h and i still seen just a blinking pointer... is it the query correct?现在需要几个小时……现在我大约 2 小时,我仍然只看到一个闪烁的指针……查询正确吗? Even I can't access to others mysql client like phpmyadmin.即使我无法访问其他 mysql 客户端,如 phpmyadmin。 There is any way to speed up the process?有什么办法可以加快这个过程吗? There is a way to check how many records are processed at running times?有没有办法检查在运行时处理了多少条记录?

I guess you have already indexed trip_id ?我猜你已经索引了trip_id There is another way writing the query, maybe it helps:还有另一种编写查询的方法,也许它会有所帮助:

SELECT *
FROM stop_times_lazio_set2_withtime
WHERE trip_id IN (SELECT trip_id FROM stop_times_lazio_set2_emptytime2)

I would expect a straight JOIN to be much much faster...我希望直接 JOIN 会快得多......

SELECT B.* 
FROM stop_times_lazio_set2_withtime2 AS B
JOIN stop_times_lazio_set2_emptytime2 AS A ON B.trip_id=A.trip_id

Why not using a simpler request ?为什么不使用更简单的请求?

SELECT A.* 
FROM stop_times_lazio_set2_emptytime2 AS A, stop_times_lazio_set_withtime2 AS B
WHERE B.trip_id=A.trip_id;

With that many records, it will obviously take time.有这么多记录,显然需要时间。

You can actually prevent it by processing only a few at a time by adding this at the end of the request您实际上可以通过在请求末尾添加它来一次只处理几个来防止它

LIMIT <beginning>, <number of records>

Did you tried "left join"??你试过“左连接”吗??

sample:样本:

select columns from withtime 
  left join emptytime on withtime.tripid=emptytime.tripid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM