如何优化我的JOIN以加快我的查询?

时间:2018-04-06 17:01:30

标签: mysql

我有6个查询,如下面列出的查询..

  • 每个都需要6秒才能运行
  • 页面加载总共36秒

有没有办法优化这类查询?

SELECT
tickets.ticketID,
tickets.ticket,
tickets.name1,
tickets.address1,
tickets.city,
tickets.cstate,
tickets.zip,
tickets.caller_type,
tickets.phone,
tickets.caller,
tickets.caller_phone,
tickets.contact,
tickets.contact_phone,
tickets.call_back,
tickets.location,
tickets.printable_text,
tblnotes.ntDate,
tblnotes.ntText,
tblstatus.stDesc,
tblUsers.username 
FROM tblusers
RIGHT OUTER JOIN tickets ON tblusers.ID = tickets.ownerID
LEFT OUTER JOIN tblstatus ON tblstatus.stID = tickets.statusID
LEFT OUTER JOIN tblnotes ON tblnotes.ntID = tickets.noteID 
WHERE tblstatus.stDesc <> "Closed"

1 个答案:

答案 0 :(得分:0)

编辑:试试这个

SELECT
tickets.ticketID,
tickets.ticket,
tickets.name1,
tickets.address1,
tickets.city,
tickets.cstate,
tickets.zip,
tickets.caller_type,
tickets.phone,
tickets.caller,
tickets.caller_phone,
tickets.contact,
tickets.contact_phone,
tickets.call_back,
tickets.location,
tickets.printable_text,
tblnotes.ntDate,
tblnotes.ntText,
tblstatus.stDesc,
tblUsers.username 
FROM tickets
INNER JOIN tblusers ON tblusers.ID = tickets.ownerID
INNER JOIN tblstatus ON tblstatus.stID = tickets.statusID
LEFT OUTER JOIN tblnotes ON tblnotes.ntID = tickets.noteID 
WHERE tickets.statusID <> 3

张贴作为答案,因为我无法发表评论

您的条件为where tblstatus.stDesc <> "Closed"

假设您在stID上有索引 将其更改为where tblstatus.stID <> put the id value

也将左外连接更改为内连接,因为你有where where条件的任何方式,你可以保持左连接tblnotes,因为我不确定它是否有一行对应{{1 }}

我还会将tbltickets表格移至tickets,然后使用from进行内部联接

仅当联接表可能没有数据时才使用tblusers,但您仍希望显示主表中的数据