我是MySQL数据库和MS Access前端。 MySQL数据库表通过ODBC连接链接到MS Access。
如果在" WHERE"中有任何内容,任何带有多个连接表的查询都会运行得非常慢。 (或" HAVING")条款。
例如:
SELECT tblGuests.GuestName, Sum(tblPayments.Payment) AS SumOfPayment, tblRooms.RoomName
FROM (tblGuests LEFT JOIN tblPayments ON tblGuests.GuestID = tblPayments.GuestNo) LEFT JOIN tblRooms ON tblGuests.RoomNo = tblRooms.RoomID
WHERE tblGuests.NoShow=False
GROUP BY tblGuests.GuestName, tblRooms.RoomName;
将花费很长时间(对于20K记录大约需要3分钟。)在通过查询的情况下,完全相同的脚本需要1-1.5秒,因此问题不应该与服务器上的索引或设置相关侧。 (顺便说一下,索引是在必要的列上设置的,并且也建立了关系。)
只有在查询中涉及的表超过2个时才会出现问题,并且" WHERE"条款或在" HAVING"。
例如,如果您修改上面的代码,如
SELECT tblGuests.GuestName, Sum(tblPayments.Payment) AS SumOfPayment
FROM tblGuests LEFT JOIN tblPayments ON tblGuests.GuestID = tblPayments.GuestNo
WHERE tblGuests.NoShow=False
GROUP BY tblGuests.GuestName;
然后它会很快。 (查询只涉及2个表。)另外
SELECT tblGuests.GuestName, Sum(tblPayments.HUFpayment) AS SumOfPayment, tblGuests.NoShow, tblRooms.RoomName
FROM (tblGuests LEFT JOIN tblPayments ON tblGuests.GuestID = tblPayments.GuestNo) LEFT JOIN tblRooms ON tblGuests.RoomNo = tblRooms.RoomID
GROUP BY tblGuests.GuestName, tblGuests.NoShow, tblRooms.RoomName;
完全没有问题,因为没有" WHERE"条款。然而,我在帖子开头提到的非常相似的代码将非常慢,除非我直接在服务器上运行(或通过Pass Through Query)。
您是否知道可能导致此问题的原因以及如何避免此问题(除了一直运行Pass Through Queries之外)?