有没有比使用 WHERE ... IN(子查询)更好的方法来编写此 SQL?
有没有比使用 WHERE ... IN(子查询)更好的方法来编写此 SQL?
SELECT device.mac, reseller.name, agent.name
FROM device
LEFT JOIN global_user
ON device.global_user_id = global_user.id
LEFT JOIN agent
ON global_user.id = agent.global_user_id
LEFT JOIN reseller
ON global_user.id = reseller.global_user_id
OR agent.reseller_id = reseller.id
WHERE device.global_user_id IN (
SELECT global_user_id
FROM reseller
WHERE id = '200'
) OR device.global_user_id IN (
SELECT global_user_id
FROM agent
WHERE reseller_id = '200'
);
我试图获取特定经销商下所有设备的列表,以及一些经销商/代理商的详细信息。这包括直接分配给经销商的设备和分配给经销商旗下代理商的设备。 reseller.id 是唯一的。它将在 postgresql 数据库上执行。
设备分配给代理商和经销商。代理商被分配给经销商。
这个查询有效,但我不经常在 JOIN 中使用 OR,并且我通常会尝试避免子查询。这个查询概念会经常使用,所以我想确保我没有忽略一些东西。
感谢您的任何反馈。
is there a better way to write this SQL than using WHERE ... IN (subquery)?
SELECT device.mac, reseller.name, agent.name
FROM device
LEFT JOIN global_user
ON device.global_user_id = global_user.id
LEFT JOIN agent
ON global_user.id = agent.global_user_id
LEFT JOIN reseller
ON global_user.id = reseller.global_user_id
OR agent.reseller_id = reseller.id
WHERE device.global_user_id IN (
SELECT global_user_id
FROM reseller
WHERE id = '200'
) OR device.global_user_id IN (
SELECT global_user_id
FROM agent
WHERE reseller_id = '200'
);
im trying to get a list of all of the devices, with some reseller/agent details, under a particular reseller. this would include devices assigned directly to the reseller and devices assigned to agents under the reseller. reseller.id is unique. it will be executed on postgresql database.
devices are assigned to both agents and resellers. agents are assigned to resellers.
this query works, but i haven't often used an OR in a JOIN and i usually try to avoid subqueries. this query concept will be used often, so i'd like to make sure i haven't overlooked something.
thank you for any feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试一下:
说明:
尝试查询的不同变体总是一个好主意,以确保最终获得性能最佳的查询(尽管通常,不同的变体会导致查询优化器生成相同的执行计划)。从 SQL Server 的角度来看,查询处理的顺序意味着 JOIN 在 WHERE 子句之前先处理。因此从理论上讲,这种 JOIN 方法应该更早地缩小结果集。
You could give this a whirl:
Clarification:
Always a good idea to try out different variations of a query to make sure you end up with the best performing query (although often, different variations result in the same execution plan being generated by the query optimiser). Speaking from SQL Server point of view, the order in which the query is processed in means the JOINs are processed first before the WHERE clause. So in theory, this JOIN approach should scale the resultset down earlier.
这个怎么样?
How about this?
如果子查询和 IN 子句很容易替换,我会尽量避免使用它们。如果我正确理解您的
数据库模型
,则此查询应该产生相同的结果:I try to avoid sub-queries and
IN
clause if it is easy to replace them. If I understood youDB model
correctly, this query should produce the same result: