否定平等与不平等(negated equal vs. not equal)
最近,我一直在阅读一些代码
if (! (a == b) )
代替
if ( a != b )
在一些地方。
显然这些在逻辑上是相同的,但我想知道是否有任何特定的原因使用其中一个。
在某些情况下,哪一个更好?或者这只是个人风格的问题?
Lately, I've been reading some code that has
if (! (a == b) )
instead of
if ( a != b )
in some places.
Obviously these are logically equivalent, but I'm wondering if there is any particular reason to use one over the other.
Are there certain circumstances where one is preferable, or is it all just a matter of personal style?
原文:https://stackoverflow.com/questions/746123
最满意答案
那应该没问题。
对于大量的行(特别是如果它们包含长字符串值)我注意到这些常量的内部表可能需要很长时间才能编译,但我怀疑你会在30行中遇到这个问题。
从快速测试到最后,它们比表变量更具优势,因为SQL Server似乎维护了一些有关它们的统计信息,因此可以选择适当的连接类型。
我刚刚尝试了一个包含500行和50个不同值的CTE,并且下面计划中的估计行数是正确的。 (尽管在编译时已经完成了所有这些工作并且可能为此目的生成了一个独特的
id
列表,但是SQL Server不仅仅将其替换为计划而不是必须这样做,这有点遗憾再次在运行时)That should be fine.
For a large number of rows (especially if they contain long string values) I have noticed that these internal tables of constants can take a long time to compile but I doubt you will hit this issue for 30 rows.
From a quick test this end they have an advantage over table variables as SQL Server seems to maintain some statistical information about them so can choose an appropriate join type.
I just tried a CTE with 500 rows and 50 distinct values and the estimated number of rows in the plan below was correct. (though it is a bit of a shame that having worked all this stuff out at compile time and presumably having generated a distinct list of
id
s for that purpose that SQL Server doesn't just substitute that into the plan instead of having to do it again at run time)
相关问答
更多-
如果要插入很多行,则可以使用COPY而不是INSERT。 性能比INSERT更好。 I solved it in a different manner, and could reduce the import time from 7 hours to only 1. So, instead of validating the data before the INSERT (in the WHERE clase of my initial post here), I let the INSERT operat ...
-
如果SQL Server中某些条件不存在,如何检索数据?(How to retrieve data if not exist with certain condition in SQL Server?)[2023-04-20]
UNION ALL是解决这个问题的一种方法 您已定义双方并且UNION ALL将它们连接起来 我将把你发布的所有代码粘在一起到目前为止: select top 1 UserID,ReportHide,RColumnName,RFilterName from [HQWebMatajer].[dbo].[SecurityUserDetails] where [UserID]='faisal.3012' and [ReportName]='TotalSales' UNION ALL select Securi ... -
如何使用SQL从数据库中检索不同的值范围?(How to retrieve the different range of values from database using SQL?)[2023-06-25]
单程 select count(case when lightvalue < 24 then 1 end), count(case when lightvalue between 24 and 30 then 1 end), count(case when lightvalue > 30 then 1 end) from tbl One way select count(case when lightvalue < 24 then 1 end), count(cas ... -
如果这是你需要的而不是计算diff,那么用新数组插入替换旧数组可能会更好 It just might be better to insert the replace the old array with new array if that is what you need instead of calculating the diff
-
对于Oracle,请查看SAMPLE子句。 以下将查看表的随机1% select * from MDSYS.SDO_COORD_REF_SYS sample(1); 您仍然可以在其上添加rownum=1过滤器。 For Oracle, look at the SAMPLE clause. The following will look at random 1% of a table select * from MDSYS.SDO_COORD_REF_SYS sample(1); You can stil ...
-
您在join条件中使用了错误的列。 这些表应该在user_id 。 SQL小提琴 SELECT stype.id, stype.user_id, stype.type, stype.date_start FROM share_types AS stype LEFT JOIN shares AS share ON share.user_id = stype.user_id WHERE share.share_type_id IS NULL You were using the wrong column in ...
-
为了在mysql查询中查找特定记录,将id值传递给下面的4位。 SELECT * FROM tablename WHERE UserID = '4' 如果你想得到特定的价值,那么就这样使用 $query = "SELECT * FROM tablename WHERE UserID = '".$_SESSION['ID']."'"; REFFERENCE For finding particular record in mysql query pass the id value in the place ...
-
这种方式是否经过优化以检索数据库中不存在的值?(Is this way is optimized to retrieve values which are not exist in database?)[2023-12-11]
那应该没问题。 对于大量的行(特别是如果它们包含长字符串值)我注意到这些常量的内部表可能需要很长时间才能编译,但我怀疑你会在30行中遇到这个问题。 从快速测试到最后,它们比表变量更具优势,因为SQL Server似乎维护了一些有关它们的统计信息,因此可以选择适当的连接类型。 我刚刚尝试了一个包含500行和50个不同值的CTE,并且下面计划中的估计行数是正确的。 (尽管在编译时已经完成了所有这些工作并且可能为此目的生成了一个独特的id列表,但是SQL Server不仅仅将其替换为计划而不是必须这样做,这有点遗 ... -
从Array中获取特定值,该值在数据库中不存在(Getting specific value from Array which does not exist in database)[2024-01-01]
如果您使用单个查询来检查所有值,那么您获得的是现有值的列表,您仍然必须在原始列表中搜索任何差异。 单独检查每个值更简单: String[] values = { "headache", "cold" }; SQLiteDatabase db = getReadableDatabase(); db.beginTransaction(); try { for (String value : values) { long count = DatabaseUtils.queryNumEnt ... -
如果数据已加载到临时表中,则可以使用此方法 SELECT * FROM myStagingTable ST WHERE NOT EXISTS (SELECT * FROM TypeTable TT WHERE ST.FKCol = TT.IdType) 要执行基于集合的操作,您需要一个具有所需FK值的表。 这可以是同一连接上的临时表,并通过扫描文件进行填充。 通过将myStagingTable更改为#myFKTable ,相同的SQL将在 ...