我有3个插入存储过程,每个SP在2个不同的表中插入数据
Table 1 Table 2
idPerson idProduct
name productName
phoneNumber productdescription
FK-idProduct
Run Code Online (Sandbox Code Playgroud)
对于表2,表1 SP的SP
create procedure test1 create procedure test2
WITH WITH
EXECUTE as caller EXECUTE as caller
AS AS
declare declare
@idPerson int, @idProduct int,
@name varchar(20), @productName varchar(50),
@phone varchar(20) @productoDescription varchar(50)
SET nocount on; SET nocount on;
Begin Begin
insert into table1( insert into table2(
idPerson, idProduct,
name, productName,
phone) productDescription)
values( values(
@idPerson, @idProduct,
@name, @productName,
@phone) @productDescription)
end end
Run Code Online (Sandbox Code Playgroud)
我需要从存储过程测试1调用存储过程测试2并将FK-ID插入表1中
我有一个sql查询,运行超快,大约一秒,当不使用变量,如:
WHERE id BETWEEN 5461094 and 5461097
Run Code Online (Sandbox Code Playgroud)
但是当我有:
declare @firstId int
declare @lastId int
set @firstId = 5461094
set @lastId = 5461097
...
WHERE id BETWEEN @firstId and @lastId
Run Code Online (Sandbox Code Playgroud)
...查询运行速度很慢,仅在几分钟后完成.为什么会这样?我需要使用变量.我可以做任何改进以避免这种性能问题吗?