简体   繁体   English

在条件检查之间Mysql中有多个条件? 有什么方法可以优化此查询?

[英]More than one condition in Mysql Between Condition Checking? Is there any way to optimize this query?

I am Working on report Generating Application, I am generating the query using PHP based on user operations. 我正在研究“生成应用程序”报告,正在使用基于用户操作的PHP生成查询。 Now I am query the details based on the experience of the staff members, there is the possibilities for more than one selecting option. 现在,我根据工作人员的经验查询详细信息,其中可能有多个选择选项。 The Result is taken from joining 5 tables and sub queries, My Query is Working fine and fetching the exact result 结果是从联接5个表和子查询中获得的,“我的查询”工作正常并且正在获取确切的结果

First Scenario : Taking Staff Members between total 15 to 20 Years Experience 第一种情况:使工作人员的总经验达到15至20年

Query 询问

SELECT sd.staff_ref_id AS staff_code,
       sd.sur,
       sd.staff_name AS Name,
       sq.degree AS Qualification,
       sd.designation,
       d.department_name,
       TIMESTAMPDIFF(MONTH, s.join_date, CURDATE()) AS rec_exp_months,
       sum(spe.years) AS p_exp_yrs,
       sum(spe.months) AS p_exp_months
FROM staff_details sd
LEFT JOIN
  (SELECT s.staff_id,
          group_concat(sq.degree) AS degree
   FROM staff_details s,
        staff_qualification sq
   WHERE s.staff_id = sq.staff_id
   GROUP BY sq.staff_id
   ORDER BY sq.row_num)sq ON sd.staff_id = sq.staff_id
LEFT JOIN staff_department s ON s.staff_id = sd.staff_id
LEFT JOIN department d ON d.department_id =s.depart_id
LEFT JOIN staff_previous_experience spe ON spe.staff_id = sd.staff_id
WHERE ((TIMESTAMPDIFF(MONTH,sd.join_date,CURDATE())) + (SELECT (sum(sqn.years)*12) + sum(sqn.months) FROM staff_previous_experience sqn WHERE sqn.staff_id=sd.staff_id GROUP BY sqn.staff_id) BETWEEN 120 AND 180)
GROUP BY staff_ref_id,
         sd.sur,
         sd.staff_name,
         sd.designation,
         d.department_name,
         sq.degree,
         s.join_date

Second Scenario: Taking staff members between total experience 10 to 15 years and 15 to 20 years Experience 第二种情况:使工作人员的总经验介于10至15年和15至20年之间

Query 询问

SELECT sd.staff_ref_id AS staff_code,
       sd.sur,
       sd.staff_name AS Name,
       sq.degree AS Qualification,
       sd.designation,
       d.department_name,
       TIMESTAMPDIFF(MONTH, s.join_date, CURDATE()) AS rec_exp_months,
       sum(spe.years) AS p_exp_yrs,
       sum(spe.months) AS p_exp_months
FROM staff_details sd
LEFT JOIN
  (SELECT s.staff_id,
          group_concat(sq.degree) AS degree
   FROM staff_details s,
        staff_qualification sq
   WHERE s.staff_id = sq.staff_id
   GROUP BY sq.staff_id
   ORDER BY sq.row_num)sq ON sd.staff_id = sq.staff_id
LEFT JOIN staff_department s ON s.staff_id = sd.staff_id
LEFT JOIN department d ON d.department_id =s.depart_id
LEFT JOIN staff_previous_experience spe ON spe.staff_id = sd.staff_id
WHERE ((TIMESTAMPDIFF(MONTH,sd.join_date,CURDATE())) + (SELECT (sum(sqn.years)*12) + sum(sqn.months) FROM staff_previous_experience sqn WHERE sqn.staff_id=sd.staff_id GROUP BY sqn.staff_id) BETWEEN 120 AND 180 OR (TIMESTAMPDIFF(MONTH,sd.join_date,CURDATE())) + (SELECT (sum(sqn.years)*12) + sum(sqn.months) FROM staff_previous_experience sqn WHERE sqn.staff_id=sd.staff_id GROUP BY sqn.staff_id) BETWEEN 180 AND 240)
GROUP BY staff_ref_id,
         sd.sur,
         sd.staff_name,
         sd.designation,
         d.department_name,
         sq.degree,
         s.join_date

Note: The above highlighted area, i am checking total experience, i am converting both input and result from database as months and checking the conditions 注意:以上突出显示的区域,我正在检查总体经验,我正在将来自数据库的输入和结果转换为月并检查条件

Question : The Darken Area is generated query from php, that i am using the between query ..based on how much options selected from experience field, the between query will be automatically generated.. 问题:Darken Area是从php生成的查询,即我正在使用之间查询 ..基于从体验字段中选择的选项数量, 之间查询将自动生成。
Is There any way to Optimize this..? 有什么方法可以优化此效果吗?
Is there an option to use between option in query for several conditions ...? 是否可以在查询多个条件的选项之间使用一个选项...?

My Query Execution Time is 0.0663 sec 我的查询执行时间为0.0663秒

awaiting Responses, Suggestions and advice's Thanks in Advance 等待回应,建议和意见的预先感谢

It feels like this will give you the same effect as the first WHERE : 感觉这将给您与第一个WHERE相同的效果:

WHERE sd.join_date >= CURDATE() - INTERVAL 180 MONTH
  AND sd.join_date  < CURDATE() - INTERVAL 120 MONTH

And have INDEX(join_date) . 并具有INDEX(join_date)

If so, then it is so much simpler that is must be faster. 如果是这样,那么它必须简单得多,因此必须更快。

The GROUP BY in the highlighted subquery is unnecessary. 突出显示的子查询中的GROUP BY是不必要的。

The ORDER BY row_num is probably invalid and ignored because of the GROUP BY . ORDER BY row_num可能无效,并且由于GROUP BY而被忽略。

It seems like s is unnecessary in the derived table after the LEFT JOIN . 似乎在LEFT JOIN之后的派生表中s是不必要的。

Why separately compute BETWEEN 120 AND 180 and BETWEEN 180 AND 240 ? 为什么分别计算BETWEEN 120 AND 180以及BETWEEN 180 AND 240 Seems like they could be merged together. 好像它们可以合并在一起。

If it is not faster, then make the cleanups I suggested in comments and let's start over. 如果不是更快,请进行我在注释中建议的清理工作,让我们重新开始。 And please provide SHOW CREATE TABLE for each table. 并且请为每个表提供SHOW CREATE TABLE

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

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