问题描述
SQL Server 中的 T-SQL 查询是否支持短路?
Do T-SQL queries in SQL Server support short-circuiting?
例如,我有一个情况,我有两个数据库,我正在比较两个表之间的数据以匹配和复制一些信息.在一个表中,ID"字段总是有前导零(例如000000001234"),而在另一个表中,ID 字段可能有也可能没有前导零(可能是000000001234"或1234").
For instance, I have a situation where I have two database and I'm comparing data between the two tables to match and copy some info across. In one table, the "ID" field will always have leading zeros (such as "000000001234"), and in the other table, the ID field may or may not have leading zeros (might be "000000001234" or "1234").
所以我的查询来匹配两者是这样的:select * from table1 where table1.ID LIKE '%1234'
So my query to match the two is something like:select * from table1 where table1.ID LIKE '%1234'
为了加快速度,我正在考虑在类似之前添加一个 OR ,它只是说:table1.ID = table2.ID处理两个 ID 都有填充零且相等的情况.
To speed things up, I'm thinking of adding an OR before the like that just says:table1.ID = table2.IDto handle the case where both ID's have the padded zeros and are equal.
这样做是否会通过匹配="上的项目而不评估每一行的 LIKE 来加速查询(它会短路并跳过 LIKE)吗?
Will doing so speed up the query by matching items on the "=" and not evaluating the LIKE for every single row (will it short circuit and skip the LIKE)?
推荐答案
SQL Server 确实NOT 短路 where 条件.它不能,因为它是基于成本的系统:SQL Server 如何短路 WHERE 条件评估.
SQL Server does NOT short circuit where conditions.it can't since it's a cost based system: How SQL Server short-circuits WHERE condition evaluation .
这篇关于SQL Server - 查询短路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!