2017-07-27 69 views
-2

我目前正在试图优化在VB格式一些SQL查询(古代的,我知道),但我只是试图让查询更有效。 这是怎样的一些疑问,现在一个例子:目前试图优化SQL查询

SELECT DISTINCT Appointments.WithID, Users.LastName, Users.FirstName 
FROM Appointments INNER JOIN AppointmentTypes ONAppointmentTypes.ID = 
Appointments.AppTypeID 
INNER JOIN Users ON Users.ID = Appointmens.WithID 
WHERE Appointments.Hide = 0 
ORDER BY Users.LastName, Users.FirstName 

以上是运行,那么对于所有的结果,这一般是返回Appointments.WithID,A每个循环发生的初始查询每个地方的另一个查询运行,例如以下项目:

SELECT Appointments.CustomerID FROM Appointments 
WHERE Appointments.Hide = 0 
AND Appointments.WithID = (And this is where the "Appointment.WithID's 
from the previous query are entered) 

所以我不知道如果我解释这个正确或没有,但所有的一切,第二个查询运行了多次针对每个Appointment.WithID这是从第一个查询中找到的。我需要一种方法,可以将第二个查询合并到第一个查询中,以便它不会运行第二个查询数百次,具体取决于返回的WithID的数量。

+2

添加appointments.customerId到第一查询。 –

+2

仅仅包括客户ID成'SHOW CREATE TABLE Appointments'第一查询 –

+0

后的结果。创建一些示例数据和预期结果。 –

回答

1

不能你只是appointments.customerid添加到您的select语句?

+0

Upvoted让这个人的代表高到足以发表评论。 –

+3

相同的评论已经写了两次。无需再次将其作为答案发布。 –

+1

这不提供问题的答案。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/16851031) –