SQL Server中的子查询 [英] Subqueries in sql server

查看:122
本文介绍了SQL Server中的子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

您能告诉我sql server中子查询的缺点吗?

我已经通过谷歌.

但是,我只有一个答案,那就是子查询的执行速度比联接慢,这是因为sql服务器中的查询优化器进程.

请告诉我是否还有其他弊端...

Hi friends,

Could you please tell me the disadvantages of subqueries in sql server?

I have gone through google.

But, I got only one answer that subqueries execution is slow than joins because of query optimizer process in sql server.

Please tell me if there are other disadvantages also...

推荐答案

了解最新情况的最好方法是使用SQL为您提供执行计划.不过这里是基础知识.

1)加入:

SQL查看查询,并基本上从连接的表中创建一个新表,然后从那里获取结果.所以最终,它只是一个电话!

2)子查询:

SQL现在必须运行多个查询才能获得结果,尽管这可能是即时的,因为SQL是由神奇的仙女制作的,但仍然会占用大量资源.

了解缺点的最佳方法是运行带有执行计划的查询并查看结果!这样,您可以自己查看!
The best way to see whats going on is to use SQL to give you an execution plan. Here are the basics though.

1)JOIN:

SQL looks at the query, and basically creates a new table of the joined tables and then gets the results from there. So ultimately, its just one call!

2) Sub query:

SQL now has to run multiple queries to get your result, which, although might be instant because SQL is made by magical fairies, is still going to be a lot more resource intensive.

The best way for you to see the disadvantages is to run a query with an execution plan and see the results! That way, you can see for yourself!


除了先前的解决方案之外,您还可以使用数据库性能监视工具,例如
In addition to the previous solution, you can use database performance monitoring tools such as SQL Profiler[^], if you are concerned with number figures in terms of their difference in execution.


在这种情况下,我更喜欢EXISTS.
SQL Server:JOIN vs IN vs EXISTS-逻辑差异 [ ^ ]
IN vs. JOIN vs. EXISTS [
I prefer EXISTS for this kind of situation.
SQL Server: JOIN vs IN vs EXISTS - the logical difference[^]
IN vs. JOIN vs. EXISTS[^]


这篇关于SQL Server中的子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆