简体   繁体   English

mysql查询优化左连接

[英]mysql query optimization left join

I m using following mysql query to fetch some desired result but problem is that it is taking more time. 我使用以下mysql查询来获取一些所需的结果,但问题是它需要更多的时间。 Currently execution time for this query is 7456 ms which is unacceptable for my project, I want to optimize this query any idea?. 目前这个查询的执行时间是7456毫秒,这对我的项目是不可接受的,我想优化这个查询的任何想法?

 SELECT *, DATEDIFF(NOW(),ticketstatus_tbl.updation_date) AS problem_age,images_tbl.image_path
            FROM ticketstatus_tbl 
            LEFT JOIN question_tbl ON ticketstatus_tbl.question_id = question_tbl.question_id 
            LEFT JOIN ticketing_tbl ON ticketstatus_tbl.related_ticket_id = ticketing_tbl.ticket_id
            LEFT JOIN department_tbl ON question_tbl.question_dept = department_tbl.department_id
            LEFT JOIN branch_tbl ON ticketstatus_tbl.branch_id = branch_tbl.id
            LEFT JOIN images_tbl ON images_tbl.question_id = ticketstatus_tbl.question_id and images_tbl.branch_id = ticketstatus_tbl.branch_id
            WHERE (ticketstatus_tbl.ticket_status NOT IN ('Close')
            AND question_tbl.is_active_question = 1
            AND ticketstatus_tbl.display_status = '1'
            AND ticketstatus_tbl.flag_color = 'Yellow'
            AND department_tbl.department_name = 'Admin')order by ticket_number ASC LIMIT 0 ,5

Thanks 谢谢

First, you do not need all the left outer join , because your where clause is undoing most of them. 首先,您不需要所有left outer join ,因为您的where子句正在撤消其中的大部分连接。 My guess is all could be turned into inner joins, but at the minimum: 我的猜测是所有可以变成内连接,但至少:

SELECT *, DATEDIFF(NOW(),ticketstatus_tbl.updation_date) AS problem_age,images_tbl.image_path
FROM ticketstatus_tbl 
            JOIN question_tbl ON ticketstatus_tbl.question_id = question_tbl.question_id 
            LEFT JOIN ticketing_tbl ON ticketstatus_tbl.related_ticket_id = ticketing_tbl.ticket_id
            JOIN department_tbl ON question_tbl.question_dept = department_tbl.department_id
            LEFT JOIN branch_tbl ON ticketstatus_tbl.branch_id = branch_tbl.id
            LEFT JOIN images_tbl ON images_tbl.question_id = ticketstatus_tbl.question_id and images_tbl.branch_id = ticketstatus_tbl.branch_id
WHERE ticketstatus_tbl.ticket_status NOT IN ('Close')
            AND question_tbl.is_active_question = 1
            AND ticketstatus_tbl.display_status = '1'
            AND ticketstatus_tbl.flag_color = 'Yellow'
            AND department_tbl.department_name = 'Admin'
order by ticket_number ASC LIMIT 0 ,5;

Second, you are doing filtering on ticketstatus_tbl . 其次,您正在对ticketstatus_tbl进行过滤。 You should have a composite index on ticketstatus_tbl(display_status, flag_color, ticket_status, question_id) . 你应该在ticketstatus_tbl(display_status, flag_color, ticket_status, question_id)上有一个综合索引ticketstatus_tbl(display_status, flag_color, ticket_status, question_id) If you can, change the ticket_status not in ('Close') to an affirmative statement: ticket_status in ( Open , 'In Progress', . . .) . 如果可以,请将ticket_status not in ('Close')更改为肯定声明: ticket_status in ( Open , 'In Progress', . . .) This makes it easier to use the index. 这使得索引更容易使用。

This is a start. 这是一个开始。

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

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