SQL INSERT mas evita duplicatas
Eu quero fazer algumas inserções rápidas, mas evite duplicatas em uma tabela. Para argumentar, vamos chamar de MarketPrices, tenho experimentado duas maneiras de fazer isso, mas não tenho certeza de como fazer o benchmark, que será mais rápido.
INSERT INTO MarketPrices (SecurityCode, BuyPrice, SellPrice, IsMarketOpen)
SELECT @SecurityCode, @BuyPrice, @SellPrice, @IsMarketOpen
EXCEPT
SELECT SecurityCode, BuyPrice, SellPrice, j.bool as IsActive FROM MarketPrices
CROSS JOIN (SELECT 0 as bool UNION SELECT 1 as bool ) as j
OU
DECLARE @MktId int
SET @MktId = (SELECT SecurityId FROM MarketPrices
where SecurityCode = @SecurityCode
and BuyPrice=@BuyPrice
and SellPrice = @SellPrice)
IF (@MktId is NULL)
BEGIN
INSERT INTO MarketPrices (SecurityCode, BuyPrice, SellPrice, IsMarketOpen)
VALUES
(@SecurityCode,@BuyPrice, @SellPrice, @IsMarketOpen)
END
Assuma isso@whatever
é um parâmetro de entrada no procedimento armazenado.
Eu quero ser capaz de inserir um novo registro para cada SecurityCode quando o BuyPrice ou SellPrice ou ambos são diferentes de todas as outras ocorrências anteriores. Eu não me importo com IsMarketOpen.
Existe alguma coisa incrivelmente estúpida sobre as abordagens acima? É um mais rápido que o outro?