在SELF JOINS上以指数方式增加查询执行时间

时间:2018-06-28 15:07:37

标签: mysql

此问题与此处实施解决方案的性能有关SQL select rows with number in sequence

我有以下查询:

SELECT employee.ID, wh1.hour_number,wh2.hour_number,wh3.hour_number,wh4.hour_number,wh5.hour_number,wh6.hour_number,wh7.hour_number,wh8.hour_number,wh9.hour_number
FROM wp_posts as employee
JOIN employee_has_workhours as wh1 on employee.ID = wh1.wp_posts_ID
JOIN employee_has_workhours as wh2 ON wh1.wp_posts_ID = wh2.wp_posts_ID AND wh1.hour_number + 1 = wh2.hour_number
JOIN employee_has_workhours as wh3 ON wh2.wp_posts_ID = wh3.wp_posts_ID AND wh2.hour_number + 1 = wh3.hour_number
JOIN employee_has_workhours as wh4 ON wh3.wp_posts_ID = wh4.wp_posts_ID AND wh3.hour_number + 1 = wh4.hour_number 
JOIN employee_has_workhours as wh5 ON wh4.wp_posts_ID = wh5.wp_posts_ID AND wh4.hour_number + 1 = wh5.hour_number
JOIN employee_has_workhours as wh6 ON wh5.wp_posts_ID = wh6.wp_posts_ID AND wh5.hour_number + 1 = wh6.hour_number
JOIN employee_has_workhours as wh7 ON wh6.wp_posts_ID = wh7.wp_posts_ID AND wh6.hour_number + 1 = wh7.hour_number
JOIN employee_has_workhours as wh8 ON wh7.wp_posts_ID = wh8.wp_posts_ID AND wh7.hour_number + 1 = wh8.hour_number
JOIN employee_has_workhours as wh9 ON wh8.wp_posts_ID = wh9.wp_posts_ID AND wh8.hour_number + 1 = wh9.hour_number
WHERE employee.ID IN(23,55)

如果我仅将一个ID放入IN子句IN(23),我将在0.016秒内得到24个结果
如果我只在IN子句IN(55)中放入一个ID,那么在同一时间我会得到32个结果
如果我将两个ID都放在IN子句IN(23,55)中,我将得到56个结果(正确),但是查询需要约1.5秒,并且执行时间随着IN子句中的每个联接或新ID的增加而呈指数增长。 >

我的问题是,当结果只是两个快速查询的组合时,为什么使用多个ID iside IN子句的查询要花这么长时间,以及如何避免这种情况。 谢谢。

0 个答案:

没有答案