3

我有一个需要几秒钟才能运行的 Doctrine 查询。我想确保它尽可能快地进行。查询如下(来自 Doctrine):

SELECT DISTINCT task.id AS id0,
                task.rate AS rate1,
                task.revised_rate AS revised_rate2,
                task.title AS title3,
                task.points AS points4,
                task.due AS due5,
                task.created AS created6,
                invoice.id AS id7,
                invoice.title AS title8,
                company.id AS id9,
                company.customer_code AS customer_code10,
                taskStatus.id AS id11,
                taskStatus.title AS title12,
                priority.id AS id13,
                taskUser.id AS id14,
                taskUser.firstname AS firstname15,
                taskUser.lastname AS lastname16,
                invoiceUser.id AS id17,
                invoiceUser.firstname AS firstname18,
                invoiceUser.lastname AS lastname19,
                invoiceStatus.id AS id20,
                invoiceStatus.title AS title21,
                opportunity.id AS id22,
                opportunity.name AS name23,
                SUM(slip.stop - slip.start) AS sclr24,
                task.projected_minutes AS projected_minutes25
FROM task task
INNER JOIN invoice invoice ON task.invoice_id = invoice.id
AND (invoice.deleted IS NULL)
INNER JOIN company company ON invoice.company_id = company.id
LEFT JOIN task_status taskStatus ON task.status_id = taskStatus.id
AND (taskStatus.deleted IS NULL)
LEFT JOIN slip slip ON task.id = slip.task_id
AND (slip.deleted IS NULL)
LEFT JOIN fos_user taskUser ON task.user_id = taskUser.id
AND (taskUser.deleted IS NULL)
LEFT JOIN priority priority ON task.priority_id = priority.id
AND (priority.deleted IS NULL)
LEFT JOIN task_comment taskComment ON task.id = taskComment.task_id
AND (taskComment.deleted IS NULL)
LEFT JOIN task_file taskFile ON task.id = taskFile.task_id
AND (taskFile.deleted IS NULL)
LEFT JOIN task_step taskStep ON task.id = taskStep.task_id
AND (taskStep.deleted IS NULL)
LEFT JOIN fos_user invoiceUser ON invoice.user_id = invoiceUser.id
AND (invoiceUser.deleted IS NULL)
LEFT JOIN invoice_status invoiceStatus ON invoice.status_id = invoiceStatus.id
AND (invoiceStatus.deleted IS NULL)
LEFT JOIN opportunity opportunity ON invoice.opportunity_id = opportunity.id
LEFT JOIN fos_user slipUser ON slip.user_id = slipUser.id
AND (slipUser.deleted IS NULL)
WHERE (taskStatus.id IN (3)
       AND slipUser.id IN (605))
  AND (task.deleted IS NULL)
GROUP BY task.id,
         company.id,
         taskUser.id,
         taskStatus.id,
         invoice.id,
         invoiceUser.id,
         invoiceStatus.id,
         opportunity.id,
         priority.id
ORDER BY priority.id ASC, task.due ASC, company.customer_code ASC, taskUser.firstname ASC, taskUser.lastname ASC, task.points ASC, task.title ASC, taskStatus.title ASC, task.created ASC, invoice.title ASC, invoiceUser.firstname ASC, invoiceUser.lastname ASC, invoiceStatus.title ASC, opportunity.name ASC LIMIT 50
OFFSET 0

这是解释的链接

任务表和索引

发票表和索引

公司表和索引

任务状态表和索引

单据表和索引

fos_user 表和索引

优先级表和索引

任务评论表和索引

任务文件表和索引

任务步骤表和索引

发票状态表

机会表

有什么我可以在加入方面做得更好的事情吗?使它们成为子选择?

任何意见表示赞赏!

4

1 回答 1

1

您可以尝试部分索引

CREATE INDEX idx_deleted_rows (some_id) ON some_table WHERE (deleted IS NULL);
于 2013-04-02T21:26:03.297 回答