简体   繁体   English

对于from子句中的表,当一列是相互的时简化where子句

[英]Simplifying where clause when a column is mutual for the tables at from clause

I would like to learn if there is any more efficient way to write the query below: 我想了解下面是否有更有效的方式编写查询:

SELECT * 
FROM   requests srp 
       INNER JOIN surgeons rpsur 
               ON rpsur.id = srp.surgeon_id 
       LEFT OUTER JOIN #usersurgeons usersurgeons 
                    ON usersurgeons.surgeon_id = srp.surgeon_id 
       LEFT OUTER JOIN surgeons LOsurgeons 
                    ON usersurgeons.surgeon_id = LOsurgeons.id 
       LEFT OUTER JOIN provsurgeons LOprovsurgeons 
                    ON LOprovsurgeons.id = LOsurgeons.provsurgeon_id 
       INNER JOIN #selectedsurgeons up 
               ON up.surgeon_id = rpsur.id 
       LEFT OUTER JOIN provsurgeons ps 
                    ON ps.id = rpsur.provsurgeon_id 
WHERE  rpsur.isprimary = 0 
       AND usersurgeons.isprimary = 0 
       AND LOsurgeons.isprimary = 0 
       AND LOprovsurgeons.isprimary = 0 
       AND up.isprimary = 0 
       AND ps.isprimary = 0 

I am not happy with the where clause here, is there any more professional way to write this, rather than adding the clauses to the join lines (such as on xx.id = yy.id and xx.isPrimary=0 )?? 我对这里的where子句不满意,有没有更专业的方法来编写此子句,而不是将子句添加到on xx.id = yy.id and xx.isPrimary=0行中(例如on xx.id = yy.id and xx.isPrimary=0 )?

From this query alone there are not many things that can be said. 仅凭此查询,没有什么可以说的。 You should consider adding some more context (how do you get data into those temporary tables and the structure of %surgeons tables): 您应该考虑添加更多上下文(如何将数据获取到这些临时表和%surgeons表的结构中):

1) Select * makes almost impossible to use any index and also provides a lot of columns ( Requests.* , surgeons.* , Provsurgeons.* etc.) in your final result. 1) Select *使得几乎不可能使用任何索引,并且在最终结果中还提供了许多列( Requests.*surgeons.*Provsurgeons.*等)。 Return only the columns that you need. 仅返回所需的列。

2) If isPrimary = 0 filtering is performed often in your queries (not just this one), you can consider creating a view that fetches data filtered by isPrimary = 0. Eg vwSurgeons , vwProvsurgeons . 2)如果isPrimary = 0经常在查询中执行过滤(而不仅仅是此查询),则可以考虑创建一个视图,以获取isPrimary = 0过滤的数据。例如, vwSurgeonsvwProvsurgeons Then, you can just JOIN directly to the view instead of the table. 然后,您可以直接加入视图而不是加入表。

3) [already mentioned in the comments] Any condition that excludes NULL values for the OUTER JOIN ed table will transform the OUTER into INNER . 3)[已在注释中提及]任何条件在OUTER JOIN ed表中排除NULL值都会将OUTER转换为INNER

Instead of joining all tables and having a where clause at the end, use a derived tables only with filtered records. 不要将所有表都连接起来并在末尾使用where子句,而只能将派生表与经过过滤的记录一起使用。 This way your query performance will be better. 这样,您的查询性能将更好。

SELECT * FROM requests srp INNER JOIN surgeons rpsur ON rpsur.id = srp.surgeon_id LEFT OUTER JOIN ( SELECT * FROM #usersurgeons WHERE isprimary = 0 )usersurgeons ON usersurgeons.surgeon_id = srp.surgeon_id LEFT OUTER JOIN ( SELECT * FROM surgeons WHERE isprimary = 0 )LOsurgeons ON usersurgeons.surgeon_id = LOsurgeons.id LEFT OUTER JOIN ( SELECT * FROM provsurgeons WHERE isprimary = 0 )LOprovsurgeons ON LOprovsurgeons.id = LOsurgeons.provsurgeon_id INNER JOIN ( SELECT * FROM #selectedsurgeons WHERE isprimary = 0 )up ON up.surgeon_id = rpsur.id LEFT OUTER JOIN ( SELECT * FROM provsurgeons WHERE isprimary = 0 ) ps ON ps.id = rpsur.provsurgeon_id WHERE rpsur.isprimary = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM