SQLSERVER语句 in和exists哪个效率高本人测试证明
SQLSERVR语句 in和exists哪个效率高本人测试证明 最近很多人讨论in和exists哪个效率高,今天就自己测试一下 我使用的是客户的数据库GPOSDB(已经有数据) 环境 :SQLSERVER2005 Windows7 我的测试条件 :两个表作连接根据VC_IC_CardNO字段,查出CT_InhouseCard
SQLSERVR语句 in和exists哪个效率高本人测试证明
最近很多人讨论in和exists哪个效率高,今天就自己测试一下
我使用的是客户的数据库GPOSDB(已经有数据)
环境:SQLSERVER2005 Windows7
我的测试条件:两个表作连接根据VC_IC_CardNO字段,查出CT_InhouseCard表中的VC_IC_CardNO(卡号)在CT_FuelingData表中存在的记录
前提:某些人可能在SQL语句中有多个in,或者多个exists,这些情况很难测试效率的,因为大家的条件都不相同
例如下面两个SQL语句
<span>1</span> <span>SELECT</span><span> OrderNo, SiteCode, AreaCode </span><span>2</span> <span>FROM</span><span> SchedulingProgram </span><span>3</span> <span>WHERE</span> AreaCode <span>IN</span> ( <span>'</span><span>P</span><span>'</span>, <span>'</span><span>M</span><span>'</span> ) <span>AND</span> SiteCode <span>IN</span> ( <span>SELECT</span><span> SiteCode </span><span>4</span> <span>FROM</span><span> EnvBasicInfo </span><span>5</span> <span>WHERE</span> cityiD <span>=</span> <span>31</span> ) <span>AND</span> OrderNo <span>NOT</span> <span>IN</span><span> ( </span><span>6</span> <span>SELECT</span><span> OrderNo </span><span>7</span> <span>FROM</span> KK_DeliveryinfoTmp )
上面SQL语句IN里面有IN和NOT IN
<span>1</span> <span>SELECT</span><span> OrderNo, SiteCode, AreaCode </span><span>2</span> <span>FROM</span><span> SchedulingProgram </span><span>3</span> <span>WHERE</span> ( AreaCode <span>IN</span> ( <span>'</span><span>P</span><span>'</span>, <span>'</span><span>M</span><span>'</span> ) <span>AND</span> SiteCode <span>IN</span> ( <span>SELECT</span><span> SiteCode </span><span>4</span> <span>FROM</span><span> EnvBasicInfo </span><span>5</span> <span>WHERE</span> cityiD <span>=</span> <span>31</span><span> ) </span><span>6</span> ) <span>AND</span> <span>NOT</span> <span>EXISTS</span> ( <span>SELECT</span><span> OrderNo </span><span>7</span> <span>FROM</span><span> KK_DeliveryinfoTmp </span><span>8</span> <span>WHERE</span> KK_DeliveryinfoTmp.OrderNo <span>=</span> SchedulingProgram.OrderNo )
上面的SQL语句IN里面又有NOT EXISTS
这样的情况很难测试同等条件下IN语句和EXISTS语句的效率
还有一个非SARG运算符
在《SQLSERVER企业级平台管理实践》的第424页里提到:
SQLSERVER对筛选条件(search argument/SARG)的写法有一定的建议
对于不使用SARG运算符的表达式,索引是没有用的,SQLSERVER对它们很难使用比较优化的做法。非SARG运算符包括
NOT、、NOT EXISTS、NOT IN、NOT LIKE和内部函数,例如:Convert、Upper等
所以当您的表中有索引并且SQL语句包含非SARG运算符,那么当测试SQL语句的执行时间的时候肯定相差很大,
因为有些SQL语句走索引,有些SQL语句不走索引
建表脚本
注意:两个表中都有索引!!
CT_FuelingData表
<span> 1</span> <span>USE</span> <span>[</span><span>GPOSDB</span><span>]</span> <span> 2</span> <span>GO</span> <span> 3</span> <span>/*</span><span>***** 对象: Table [dbo].[CT_FuelingData] 脚本日期: 08/24/2013 11:00:34 *****</span><span>*/</span> <span> 4</span> <span>SET</span> ANSI_NULLS <span>ON</span> <span> 5</span> <span>GO</span> <span> 6</span> <span>SET</span> QUOTED_IDENTIFIER <span>ON</span> <span> 7</span> <span>GO</span> <span> 8</span> <span>SET</span> ANSI_PADDING <span>ON</span> <span> 9</span> <span>GO</span> <span>10</span> <span>CREATE</span> <span>TABLE</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>CT_FuelingData</span><span>]</span><span>( </span><span>11</span> <span>[</span><span>RecordNO</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span><span>, </span><span>12</span> <span>[</span><span>I_FD_StationNo</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NOT</span> <span>NULL</span><span>, </span><span>13</span> <span>[</span><span>VC_FD_No</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NOT</span> <span>NULL</span><span>, </span><span>14</span> <span>[</span><span>VC_FD_Cardno</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NOT</span> <span>NULL</span><span>, </span><span>15</span> <span>[</span><span>I_FD_CardStatus</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NULL</span><span>, </span><span>16</span> <span>[</span><span>LI_FD_CTC</span><span>]</span> <span>[</span><span>bigint</span><span>]</span> <span>NOT</span> <span>NULL</span><span>, </span><span>17</span> <span>[</span><span>I_FD_TypeCode</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NULL</span><span>, </span><span>18</span> <span>[</span><span>I_FD_PumpID</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NOT</span> <span>NULL</span><span>, </span><span>19</span> <span>[</span><span>VC_FD_OilType</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NULL</span><span>, </span><span>20</span> <span>[</span><span>DE_FD_Volume</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>18</span>, <span>2</span>) <span>NULL</span><span>, </span><span>21</span> <span>[</span><span>DE_FD_Price</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>18</span>, <span>2</span>) <span>NULL</span><span>, </span><span>22</span> <span>[</span><span>DE_FD_Amount</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>18</span>, <span>2</span>) <span>NULL</span><span>, </span><span>23</span> <span>[</span><span>I_FD_Point</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>10</span>, <span>2</span>) <span>NULL</span><span>, </span><span>24</span> <span>[</span><span>D_FD_DateTime</span><span>]</span> <span>[</span><span>datetime</span><span>]</span> <span>NOT</span> <span>NULL</span><span>, </span><span>25</span> <span>[</span><span>VC_FD_GroupNo</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NULL</span><span>, </span><span>26</span> <span>[</span><span>D_FD_GroupDate</span><span>]</span> <span>[</span><span>datetime</span><span>]</span> <span>NULL</span><span>, </span><span>27</span> <span>[</span><span>DE_FD_CardAmount</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>18</span>, <span>2</span>) <span>NULL</span><span>, </span><span>28</span> <span>[</span><span>DE_FD_VolumeTotals</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>18</span>, <span>2</span>) <span>NULL</span><span>, </span><span>29</span> <span>[</span><span>DE_FD_AmountTotals</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>18</span>, <span>2</span>) <span>NULL</span><span>, </span><span>30</span> <span>[</span><span>I_FD_ISSend</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NULL</span><span>, </span><span>31</span> <span>[</span><span>VC_FD_CardMoneyauthFile</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NULL</span><span>, </span><span>32</span> <span>[</span><span>D_Month</span><span>]</span> <span>[</span><span>datetime</span><span>]</span> <span>NULL</span><span>, </span><span>33</span> <span>CONSTRAINT</span> <span>[</span><span>PK_CT_FuelingData_1</span><span>]</span> <span>PRIMARY</span> <span>KEY</span> <span>CLUSTERED</span> <span>34</span> <span>( </span><span>35</span> <span>[</span><span>VC_FD_No</span><span>]</span> <span>ASC</span> <span>36</span> )<span>WITH</span> (PAD_INDEX <span>=</span> <span>OFF</span>, STATISTICS_NORECOMPUTE <span>=</span> <span>OFF</span>, IGNORE_DUP_KEY <span>=</span> <span>OFF</span>, ALLOW_ROW_LOCKS <span>=</span> <span>ON</span>, ALLOW_PAGE_LOCKS <span>=</span> <span>ON</span>) <span>ON</span> <span>[</span><span>PRIMARY</span><span>]</span> <span>37</span> ) <span>ON</span> <span>[</span><span>PRIMARY</span><span>]</span> <span>38</span> <span>39</span> <span>GO</span> <span>40</span> <span>SET</span> ANSI_PADDING <span>OFF</span>
CT_InhouseCard表
<span> 1</span> <span>USE</span> <span>[</span><span>GPOSDB</span><span>]</span> <span> 2</span> <span>GO</span> <span> 3</span> <span>/*</span><span>***** 对象: Table [dbo].[CT_InhouseCard] 脚本日期: 08/24/2013 10:59:58 *****</span><span>*/</span> <span> 4</span> <span>SET</span> ANSI_NULLS <span>ON</span> <span> 5</span> <span>GO</span> <span> 6</span> <span>SET</span> QUOTED_IDENTIFIER <span>ON</span> <span> 7</span> <span>GO</span> <span> 8</span> <span>SET</span> ANSI_PADDING <span>ON</span> <span> 9</span> <span>GO</span> <span>10</span> <span>CREATE</span> <span>TABLE</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>CT_InhouseCard</span><span>]</span><span>( </span><span>11</span> <span>[</span><span>RecordNO</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span><span>, </span><span>12</span> <span>[</span><span>VC_IC_CardNO</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NOT</span> <span>NULL</span><span>, </span><span>13</span> <span>[</span><span>VC_IC_PhysicalNO</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NULL</span><span>, </span><span>14</span> <span>[</span><span>I_IC_CardType</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NULL</span><span>, </span><span>15</span> <span>[</span><span>VC_IC_UserName</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NULL</span><span>, </span><span>16</span> <span>[</span><span>VC_IC_JobNO</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NULL</span><span>, </span><span>17</span> <span>[</span><span>VC_IC_UserID</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NULL</span><span>, </span><span>18</span> <span>[</span><span>VC_IC_Password</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NULL</span><span>, </span><span>19</span> <span>[</span><span>DE_IC_CardAmount</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>18</span>, <span>2</span>) <span>NULL</span><span>, </span><span>20</span> <span>[</span><span>DE_IC_AppendAmount</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>18</span>, <span>2</span>) <span>NULL</span><span>, </span><span>21</span> <span>[</span><span>DE_IC_ConsumerAmount</span><span>]</span> <span>[</span><span>decimal</span><span>]</span>(<span>18</span>, <span>2</span>) <span>NULL</span><span>, </span><span>22</span> <span>[</span><span>I_IC_ISLost</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NULL</span><span>, </span><span>23</span> <span>[</span><span>D_IC_UsedDateTime</span><span>]</span> <span>[</span><span>datetime</span><span>]</span> <span>NULL</span><span>, </span><span>24</span> <span>[</span><span>D_IC_UselifeDateTime</span><span>]</span> <span>[</span><span>datetime</span><span>]</span> <span>NULL</span><span>, </span><span>25</span> <span>[</span><span>I_IC_IssueStationNO</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NULL</span><span>, </span><span>26</span> <span>[</span><span>VC_IC_IssuerNO</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>50</span>) <span>NULL</span><span>, </span><span>27</span> <span>[</span><span>D_IC_IssueDateTime</span><span>]</span> <span>[</span><span>datetime</span><span>]</span> <span>NULL</span><span>, </span><span>28</span> <span>[</span><span>D_IC_LastUpdateDateTime</span><span>]</span> <span>[</span><span>datetime</span><span>]</span> <span>NULL</span><span>, </span><span>29</span> <span>[</span><span>I_IC_CardStatus</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NULL</span><span>, </span><span>30</span> <span>[</span><span>VC_IC_Remark</span><span>]</span> <span>[</span><span>varchar</span><span>]</span>(<span>256</span>) <span>NULL</span><span>, </span><span>31</span> <span>CONSTRAINT</span> <span>[</span><span>PK_CT_InhouseCard</span><span>]</span> <span>PRIMARY</span> <span>KEY</span> <span>CLUSTERED</span> <span>32</span> <span>( </span><span>33</span> <span>[</span><span>VC_IC_CardNO</span><span>]</span> <span>ASC</span> <span>34</span> )<span>WITH</span> (PAD_INDEX <span>=</span> <span>OFF</span>, STATISTICS_NORECOMPUTE <span>=</span> <span>OFF</span>, IGNORE_DUP_KEY <span>=</span> <span>OFF</span>, ALLOW_ROW_LOCKS <span>=</span> <span>ON</span>, ALLOW_PAGE_LOCKS <span>=</span> <span>ON</span>) <span>ON</span> <span>[</span><span>PRIMARY</span><span>]</span> <span>35</span> ) <span>ON</span> <span>[</span><span>PRIMARY</span><span>]</span> <span>36</span> <span>37</span> <span>GO</span> <span>38</span> <span>SET</span> ANSI_PADDING <span>OFF</span>
测试脚本
因为这个是客户的数据库,本来里面已经有数据了,所以在测试之前先更新两个表的统计信息,以做到公正
<span>1</span> <span>USE</span> <span>[</span><span>GPOSDB</span><span>]</span> <span>2</span> <span>GO</span> <span>3</span> <span>UPDATE</span> <span>STATISTICS</span><span> CT_FuelingData </span><span>4</span> <span>UPDATE</span> <span>STATISTICS</span><span> CT_InhouseCard </span><span>5</span> <span>GO</span>
IN语句
<span> 1</span> <span>USE</span> <span>[</span><span>GPOSDB</span><span>]</span> <span> 2</span> <span>GO</span> <span> 3</span> <span>DBCC</span><span> DROPCLEANBUFFERS </span><span> 4</span> <span>GO</span> <span> 5</span> <span>DBCC</span><span> FREEPROCCACHE </span><span> 6</span> <span>GO</span> <span> 7</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span> <span> 8</span> <span>GO</span> <span> 9</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span> <span>10</span> <span>GO</span> <span>11</span> <span>SET</span> <span>STATISTICS</span> PROFILE <span>ON</span> <span>12</span> <span>GO</span> <span>13</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>CT_FuelingData</span><span>]</span> <span>WHERE</span> <span>[</span><span>VC_FD_Cardno</span><span>]</span> <span>IN</span> (<span>SELECT</span> <span>[</span><span>VC_IC_CardNO</span><span>]</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>CT_InhouseCard</span><span>]</span>)
EXISTS语句
<span> 1</span> <span>USE</span> <span>[</span><span>GPOSDB</span><span>]</span> <span> 2</span> <span>GO</span> <span> 3</span> <span>DBCC</span><span> DROPCLEANBUFFERS </span><span> 4</span> <span>GO</span> <span> 5</span> <span>DBCC</span><span> FREEPROCCACHE </span><span> 6</span> <span>GO</span> <span> 7</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span> <span> 8</span> <span>GO</span> <span> 9</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span> <span>10</span> <span>GO</span> <span>11</span> <span>SET</span> <span>STATISTICS</span> PROFILE <span>ON</span> <span>12</span> <span>GO</span> <span>13</span> <span>SELECT</span> <span>*</span> <span>14</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>CT_FuelingData</span><span>]</span> <span>15</span> <span>WHERE</span> <span>EXISTS</span> ( <span>SELECT</span> <span>[</span><span>VC_IC_CardNO</span><span>]</span> <span>16</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>CT_InhouseCard</span><span>]</span> <span>17</span> <span>WHERE</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>CT_FuelingData</span><span>]</span>.<span>[</span><span>VC_FD_Cardno</span><span>]</span> <span>=</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>CT_InhouseCard</span><span>]</span>.<span>[</span><span>VC_IC_CardNO</span><span>]</span> )
测试结果
IN语句
<span> 1</span> <span>SQL Server 执行时间: </span><span> 2</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>2</span><span> 毫秒。 </span><span> 3</span> <span>SQL Server 分析和编译时间: </span><span> 4</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。 </span><span> 5</span> <span> 6</span> <span>SQL Server 执行时间: </span><span> 7</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。 </span><span> 8</span> <span>SQL Server 分析和编译时间: </span><span> 9</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。 </span><span>10</span> <span>11</span> <span>SQL Server 执行时间: </span><span>12</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。 </span><span>13</span> <span>SQL Server 分析和编译时间: </span><span>14</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。 </span><span>15</span> <span>16</span> <span>SQL Server 执行时间: </span><span>17</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。 </span><span>18</span> <span>SQL Server 分析和编译时间: </span><span>19</span> CPU 时间 <span>=</span> <span>31</span> 毫秒,占用时间 <span>=</span> <span>67</span><span> 毫秒。 </span><span>20</span> <span>21</span> (<span>167</span><span> 行受影响) </span><span>22</span> 表 <span>'</span><span>Worktable</span><span>'</span>。扫描计数 <span>0</span>,逻辑读取 <span>0</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。 </span><span>23</span> 表 <span>'</span><span>CT_FuelingData</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>31</span> 次,物理读取 <span>1</span> 次,预读 <span>64</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。 </span><span>24</span> 表 <span>'</span><span>CT_InhouseCard</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>2</span> 次,物理读取 <span>1</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。 </span><span>25</span> <span>26</span> (<span>4</span><span> 行受影响) </span><span>27</span> <span>28</span> <span>SQL Server 执行时间: </span><span>29</span> CPU 时间 <span>=</span> <span>16</span> 毫秒,占用时间 <span>=</span> <span>192</span> 毫秒。
EXISTS语句
<span> 1</span> <span>SQL Server 分析和编译时间: </span><span> 2</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。 </span><span> 3</span> <span> 4</span> <span>SQL Server 执行时间: </span><span> 5</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。 </span><span> 6</span> <span>SQL Server 分析和编译时间: </span><span> 7</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>34</span><span> 毫秒。 </span><span> 8</span> <span> 9</span> (<span>167</span><span> 行受影响) </span><span>10</span> 表 <span>'</span><span>Worktable</span><span>'</span>。扫描计数 <span>0</span>,逻辑读取 <span>0</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。 </span><span>11</span> 表 <span>'</span><span>CT_FuelingData</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>31</span> 次,物理读取 <span>1</span> 次,预读 <span>64</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。 </span><span>12</span> 表 <span>'</span><span>CT_InhouseCard</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>2</span> 次,物理读取 <span>1</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。 </span><span>13</span> <span>14</span> (<span>4</span><span> 行受影响) </span><span>15</span> <span>16</span> <span>SQL Server 执行时间: </span><span>17</span> CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>163</span> 毫秒。
大家可以看到除了执行时间有一点差别,IO是一样的
因为数据量比较大,所以两个查询都用到了Worktable(中间表)来存储中间结果
IN语句的执行计划
EXISTS语句的执行计划
从执行计划可以看到两个SQL语句的开销都是一样的,而且大家都使用了右半连接(Right Semi Join)
至于什么是半连接(Semi-join)大家可以看一下这篇文章:SQL Join的一些总结
总结
从上面实际的执行来比较,,IN语句和EXISTS语句基本上都是一样的效率
如有不对的地方,欢迎大家来拍砖o(∩_∩)o

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











SQL Server 데이터베이스에 이미 존재하는 동일한 이름을 가진 개체의 경우 다음 단계를 수행해야 합니다. 개체 유형(테이블, 뷰, 저장 프로시저)을 확인합니다. IF NOT EXISTS를 사용하면 객체가 비어 있는 경우 생성을 건너뛸 수 있습니다. 개체에 데이터가 있는 경우 다른 이름을 사용하거나 구조를 수정하세요. 기존 개체를 삭제하려면 DROP을 사용하세요. 주의하세요. 백업을 권장합니다. 삭제되거나 이름이 바뀐 개체에 대한 참조가 없는지 확인하려면 스키마 변경 사항을 확인하세요.

가져오기 단계는 다음과 같습니다. MDF 파일을 SQL Server의 데이터 디렉터리(일반적으로 C:\Program Files\Microsoft SQL Server\MSSQL\DATA)에 복사합니다. SSMS(SQL Server Management Studio)에서 데이터베이스를 열고 연결을 선택합니다. 추가 버튼을 클릭하고 MDF 파일을 선택합니다. 데이터베이스 이름을 확인하고 확인 버튼을 클릭합니다.

SQL Server 서비스가 시작되지 않는 경우 해결해야 할 몇 가지 단계는 다음과 같습니다. 오류 로그를 확인하여 근본 원인을 확인합니다. 서비스 계정에 서비스를 시작할 수 있는 권한이 있는지 확인하세요. 종속성 서비스가 실행 중인지 확인하세요. 바이러스 백신 소프트웨어를 비활성화합니다. SQL Server 설치를 복구합니다. 복구가 작동하지 않으면 SQL Server를 다시 설치하십시오.

SQL Server 포트 번호를 보려면 SSMS를 열고 서버에 연결합니다. 개체 탐색기에서 서버 이름을 찾아 마우스 오른쪽 단추로 클릭하고 속성을 선택합니다. 연결 탭에서 TCP 포트 필드를 확인하세요.

SQL Server 데이터베이스 파일은 일반적으로 다음 기본 위치에 저장됩니다. Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data 데이터베이스 파일 경로를 수정하여 데이터베이스 파일 위치를 사용자 정의할 수 있습니다. 환경.

실수로 SQL Server 데이터베이스를 삭제한 경우 다음 단계를 수행하여 복구할 수 있습니다. 데이터베이스 활동 중지, 데이터베이스 로그 확인, 백업에서 복원, DBCC CHECKDB 사용 파티 도구. 데이터 손실을 방지하려면 데이터베이스를 정기적으로 백업하고 트랜잭션 로깅을 활성화하십시오.

SQL Server 설치가 실패하면 다음 단계에 따라 정리할 수 있습니다. SQL Server 제거 레지스트리 키 삭제 파일 및 폴더 삭제 컴퓨터를 다시 시작합니다.

다음 단계에 따라 SQL Server 영어 설치를 중국어로 변경할 수 있습니다. SQL Server 서비스를 중지하고, 인스턴스 언어를 변경하고, 응용 프로그램을 다시 시작합니다.
