0

我正在使用以下 mysql 查询来获取一些期望的结果,但问题是它需要更多时间。目前这个查询的执行时间是 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

谢谢

4

1 回答 1

2

首先,您不需要所有的left outer join,因为您的where子句正在撤消其中的大部分。我的猜测是所有都可以变成内部连接,但至少:

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;

其次,您正在对ticketstatus_tbl. 您应该在ticketstatus_tbl(display_status, flag_color, ticket_status, question_id). 如果可以,将 更改ticket_status not in ('Close')为肯定的陈述: ticket_status in (Open , 'In Progress', . . .)。这使得使用索引更容易。

这是一个开始。

于 2013-08-24T13:07:21.403 回答