最快的方法来确定是否存在logging

正如标题所暗示的那样…我试图用最less的开销来确定一个logging是否存在于表中。

示例查询:

SELECT COUNT(*) FROM products WHERE products.id = ?; vs SELECT COUNT(products.id) FROM products WHERE products.id = ?; vs SELECT products.id FROM products WHERE products.id = ?; 

?'TB100'交换…第一个和第二个查询都会返回完全相同的结果(比如说对话1 )。 最后的查询将按预期的方式返回'TB100' ,或者如果表中不存在该id ,则返回任何内容。

目的是找出是否在表中的id 。 如果不是,则程序将接下来插入logging,如果是,则程序将跳过它或执行基于此问题范围之外的其他程序逻辑的UPDATE查询。

哪个更快,开销更less? (每次程序运行会重复数万次,每天运行多次)。

(通过M $提供的JDBC驱动程序从Java运行M $ SQL Server的查询)

SELECT TOP 1 products.id FROM products WHERE products.id = ?; 将超越您的所有build议,因为它会在find第一条logging后终止执行。

EXISTS (或者NOT EXISTS )是专门用来检查是否存在的东西,因此应该是(而且是)最好的select。 它会暂停第一行匹配,所以它不需要一个TOP子句,它实际上不select任何数据,所以没有列大小的开销。 您可以安全地使用SELECT *这里 – 与SELECT 1SELECT NULLSELECT AnyColumn … (甚至可以使用像SELECT 1/0这样的无效expression式并且不会中断)不同 。

 IF EXISTS (SELECT * FROM Products WHERE id = ?) BEGIN --do what you need if exists END ELSE BEGIN --do what needs to be done if not END 

什么都不能打 –

 SELECT TOP 1 1 FROM products WHERE id = 'some value'; 

你不需要知道表中是否有数据。 不必使用别名。

 SELECT CASE WHEN EXISTS (SELECT TOP 1 * FROM dbo.[YourTable] WHERE [YourColumn] = [YourValue]) THEN CAST (1 AS BIT) ELSE CAST (0 AS BIT) END 

这种方法为你返回一个布尔值。

你也可以使用

  If EXISTS (SELECT 1 FROM dbo.T1 WHERE T1.Name='Scot') BEGIN --<Do something> END ELSE BEGIN --<Do something> END 
 SELECT COUNT(*) FROM products WHERE products.id = ?; 

这是适用于所有数据库的交叉关系数据库解决scheme。

不要以为任何人都已经提到过,但是如果你确定数据不会在你的底下发生变化,那么你可能也想使用NoLock提示来保证在阅读时不被阻塞。

 SELECT CASE WHEN EXISTS (SELECT 1 FROM dbo.[YourTable] WITH (NOLOCK) WHERE [YourColumn] = [YourValue]) THEN CAST (1 AS BIT) ELSE CAST (0 AS BIT) END 
 create or replace procedure ex(j in number) as i number; begin select id into i from student where id=j; if i is not null then dbms_output.put_line('exists'); end if; exception when no_data_found then dbms_output.put_line(i||' does not exists'); end; 

我已经使用过这个,它不需要一个完整的表扫描,看看是否存在。 速度超快

 UPDATE TableName SET column=value WHERE column=value IF @@ROWCOUNT=0 BEGIN --Do work END 

以下是确定数据库中是否存在logging的最简单,最快捷的方法。好东西可以在所有关系数据库中使用

 SELECT distinct 1 products.id FROM products WHERE products.id = ?; 
  DECLARE @USER TABLE(SM_Tbl_Name nvarchar(100),SM_Col_Namenvarchar(100),SM_Disp_Name nvarchar(200),SM_Data_Type nvarchar(100),SM_Is_Required BIT, SM_UI_Tag nvarchar(100),SM_Master_Table nvarchar(100),SM_Text_Field,nvarchar(100),SM_Value_Field,nvarchar(100),SM_Where_Cond nvarchar(400),SM_Is_Readonly BIT,SM_Isactive BIT,SM_Status VARCHAR(1)) INSERT INTO @USER(SM_Tbl_Name,SM_Col_Name,SM_Disp_Name,SM_Data_Type,SM_Is_Required,SM_UI_Tag,SM_Master_Table,SM_Text_Field,SM_Value_Field,SM_Where_Cond,SM_Is_Readonly,SM_Isactive,SM_Status) SELECT 'SADM_Users' as SM_Tbl_Name,'Flex_3'as SM_Col_Name,'Salesman Status Principal' as SM_Disp_Name,'Int' as SM_Data_Type,'0' as SM_Is_Required,'DropDown' as SM_UI_Tag,'ADM_LOVs' as SM_Master_Table,'ALOV_Name' as SM_Text_Field,'ALOV_Id' as SM_Value_Field, 'ALOV_Type=''SALESMAN_STATUS_PRINCIPAL''' as SM_Where_Cond,'0' as SM_Is_Readonly,'1' as SM_Isactive,'I' as SM_Status UNION SELECT 'SADM_Users' as SM_Tbl_Name,'Flex_4'as SM_Col_Name,'Salesman Status Channel' as SM_Disp_Name,'Int' as SM_Data_Type,'0' as SM_Is_Required,'DropDown' as SM_UI_Tag,'ADM_LOVs' as SM_Master_Table,'ALOV_Name' as SM_Text_Field,'ALOV_Id' as SM_Value_Field, 'ALOV_Type=''SALESMAN_STATUS_CHANNEL''' as SM_Where_Cond,'0' as SM_Is_Readonly,'1' as SM_Isactive,'I' as SM_Status UNION SELECT 'SADM_Users' as SM_Tbl_Name,'Flex_5'as SM_Col_Name,'Supervisor Status Principal' as SM_Disp_Name,'Int' as SM_Data_Type,'0' as SM_Is_Required,'DropDown' as SM_UI_Tag,'ADM_LOVs' as SM_Master_Table,'ALOV_Name' as SM_Text_Field,'ALOV_Id' as SM_Value_Field, 'ALOV_Type=''SUPERVISOR_STATUS_PRINCIPAL''' as SM_Where_Cond,'0' as SM_Is_Readonly,'1' as SM_Isactive,'I' as SM_Status UNION SELECT 'SADM_Users' as SM_Tbl_Name,'Flex_6'as SM_Col_Name,'Supervisor Status Channel' as SM_Disp_Name,'Int' as SM_Data_Type,'0' as SM_Is_Required,'DropDown' as SM_UI_Tag,'ADM_LOVs' as SM_Master_Table,'ALOV_Name' as SM_Text_Field,'ALOV_Id' as SM_Value_Field, 'ALOV_Type=''SUPERVISOR_STATUS_CHANNEL''' as SM_Where_Cond,'0' as SM_Is_Readonly,'1' as SM_Isactive,'I' as SM_Status INSERT INTO [dbo].[SADM_Flex_Ext_Def](SM_Tenant_Id,SM_Bu_Id,SM_Tbl_Name,SM_Col_Name,SM_Disp_Name,SM_Data_Type,SM_Is_Required,SM_UI_Tag,SM_Master_Table,SM_Text_Field,SM_Value_Field,SM_Where_Cond,SM_Is_Readonly,SM_Isactive,SM_Status) SELECT 1 as SM_Tenant_Id,1 as SM_Bu_Id,T.SM_Tbl_Name,T.SM_Col_Name,T.SM_Disp_Name,T.SM_Data_Type,T.SM_Is_Required,T.SM_UI_Tag,T.SM_Master_Table,T.SM_Text_Field,T.SM_Value_Field,T.SM_Where_Cond,T.SM_Is_Readonly,T.SM_Isactive,T.SM_Status FROM @USER as T left join SADM_Flex_Ext_Def as TT on TT.SM_Tenant_Id=97 and TT.SM_Bu_Id=41 and TT.SM_Col_Name=T.SM_Col_Name and TT.SM_Tbl_Name=T.SM_Tbl_Name WHERE TT.SM_Id IS NULL PRINT 'SADM_Flex_Ext_Def - Insert: ' + CONVERT(VARCHAR, @@ROWCOUNT) GO