简体   繁体   中英

How to include indexed columns in join query rails mysql to improve performance

It is know that index does not work on date columns if they are mapped with any SQL function on them.

People say, including date column with between cases or <= and >= can give indexed date column to show data based on that index (means performance got improved). However, I found that both gives same to same results and apparently no performance taken care of.

How exactly we can minimize the query time if everything is streamlined. Or any better solution to cope with it?

I added index on both project_times.date and project_plans.month

Example:

Query 1 without sql functions on columns project_times.date and project_plans.month that returns 87 records but time is huge. May be or may be not the indexing has done on columns:

ProjectConsultant Load (1169.8ms)  SELECT DISTINCT `project_consultants`.* FROM
 `project_consultants` LEFT OUTER JOIN `projects` ON `projects`.`id` =
 `project_consultants`.`project_id` LEFT OUTER JOIN `project_times` ON
 `project_times`.`project_id` = `projects`.`id` LEFT OUTER JOIN `project_plans` ON
 `project_plans`.`project_id` = `projects`.`id` WHERE (projects.internal =0 and
 (project_times.date >= '2021-01-01' and project_times.date <= '2021-12-31' or
 project_plans.month >= '2021-01-01' and project_plans.month <= '2021-12-31'))

Query 2 with sql functions on columns project_times.date and project_plans.month like ((extract(year from project_times.date)) and (extract(year from project_plans.month)) that returns 87 records but time is again huge (technically NO indexing is done on columns):

ProjectConsultant Load (1342.6ms)  SELECT DISTINCT `project_consultants`.* FROM
 `project_consultants` LEFT OUTER JOIN `projects` ON `projects`.`id` =
 `project_consultants`.`project_id` LEFT OUTER JOIN `project_times` ON
 `project_times`.`project_id` = `projects`.`id` LEFT OUTER JOIN `project_plans` ON
 `project_plans`.`project_id` = `projects`.`id` WHERE (projects.internal =0 and (extract(year
 from project_times.date) ='2021' or extract(year from project_plans.month) ='2021'))

Attached images shows the query plan and explain statements output too.

项目计划表的表结构

项目时间表的表结构

项目顾问表的表结构

查询 1 的查询计划

查询 2 的查询计划

Run those queries with a 2 month range instead of a 12 month range. Query 1 may start using the indexes. This because there is overhead in using an index; so, if it does not provide much benefit (not very selective), it will be eschewed by the Optimizer.

Please don't use LEFT unless you need it.

Those queries seem to limit the output to projects with times and plans in 2021? If that was not the intent, please explain.

Query 2 will definitely not use an index because functions, such as EXTRACT , are not "sargeable".

True; BETWEEN is identical in all respects to >= and <=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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