存储过程优化
最近,遇到一个项目,每秒大概有2000次,对2000张表的插入操作。每1次的数据量是200个字节,并且表的数量是不确定的,是随着设备的新增而增加的。我写了如下一个存储过程:
create proc add_GPSInfo
@tableName varchar(30),
@sLongitude varchar(50),
@sLatitude varchar(50),
@sStatus int,
@sDataType int,
@sHeading real,
@sSpeed real,
@sGpsTime varchar(30),
@sMiles varchar(10)
AS
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@TableName+']') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
exec('INSERT INTO ' +@tableName+ ' (Longitude,Latitude,Status,DataType,Heading,Speed,GpsTime,Miles) VALUES ('''+@sLongitude+''','''+@sLatitude+''','+@sStatus+','+@sDataType+','+@sHeading+','+@sSpeed+','''+@sGpsTime+''','+@sMiles+')' );
end
else
begin exec(
'CREATE TABLE [dbo].['+@tableName+'] (
[Longitude] [float] NULL ,
[Latitude] [float] NULL ,
[Status] [int] NULL ,
[DataType] [int] NULL ,
[Heading] [real] NULL ,
[Speed] [real] NULL ,
[GpsTime] [datetime] NULL ,
[Miles] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY]'
);
exec('INSERT INTO ' +@tableName+ ' (Longitude,Latitude,Status,DataType,Heading,Speed,GpsTime,Miles) VALUES ('''+@sLongitude+''','''+@sLatitude+''','+@sStatus+','+@sDataType+','+@sHeading+','+@sSpeed+','''+@sGpsTime+''','+@sMiles+')' );
end
GO
在这里,请教一下各位,如果不该表思路,在SQL语句层面上,这个存储过程还可以怎么优化一下呢?