【发布时间】:2019-05-12 18:40:10
【问题描述】:
我们有实时跟踪应用程序,其中我们将跟踪点插入到一个名为Tracking 的表中。我们还有另一个名为ExistingAddressPointTable 的表,其中有送货地址。当送货员在该地址的 200 米半径范围内时,我们正在考虑将特定地址送达,并将送货条目插入称为Delivery(多对多关系)表的非空间表中。
我们在每分钟 20-30 秒后进行 API 调用,因此设备在此期间始终发送 10-15 个地址点。我在存储过程中实现了这个解决方案,但问题是即使同时 20 个交付男孩会话 Azure SQL Server CPU 使用率达到 100%。我想支持这个系统同时进行 1000 个会话。
我们在 UAT 环境中使用具有 50 个 DTU 限制的弹性池中的 Azure SQL Server 数据库。我们的客户在同一个弹性池中已经有 5 个其他 250 GB 的数据库。对于生产,我们也有 100 个 DTU 的限制。
有没有办法实现Deliveryboy通过不同的移动设备进行追踪?
使用的技术:.net core、Web API 和 Azure SQL Server 2017,空间表上的空间索引(Tracking & ExistingAddressPointTable)已经应用。非常感谢任何帮助。
PSB代码供参考
CREATE Procedure [dbo].[InsertTrackingPoint]
@points AddressPointType READONLY,
@name NVARCHAR(150),
@isDisConnected BIT,
@jobId BIGINT -- with current scenario of implementation.This is not required
AS
BEGIN
Declare @routeId bigint;
Declare @point nvarchar(700);
DECLARE @totalRecords INT
DECLARE @I INT
DECLARE @trackingId bigint
Declare @deliveryBoyId bigint;
DECLARE @route As TABLE(Id int Identity,RouteId bigint,JobId bigint);
Select Top 1 @deliveryBoyId=Id from DeliveryPerson with(Nolock) where --Take DeliveryPerson Id FROM DeliveryPerson Table to insert breadcrumb against particular Id
(DeliveryPerson.Name LIKE @name) OR ( DeliveryPerson.Email LIKE @name ) AND IsActive=1
Insert into @route
Select Id As RouteId,JobId from Route With(Nolock) where CarrierID=@deliveryBoyId AND JobId in( Select Id From Job where EndDate>=Convert(date,GETDATE())AND IsActive=1)
AND TotalAddressPoint>0
order by Id desc --Inserting all the active jobs assigned to that carrier and also checking if addresspoint count is greater than 0.It means we are taking those Jobs whose addressPoint count is greater than 0.
Declare @J INT;
DECLARE @totalRouteCount INT;
SET @J=1;--Iterator variable
SELECT @totalRecords = COUNT(Point) FROM @points--taking count of all the addresspoints coming from Mobile device.
print @totalRecords
print @totalRouteCount
SELECT @totalRouteCount=Count(*) FROM @route --taking count of all the active Jobs/Routes of Jobs assigned to carrier
print @totalRouteCount
DECLARE @addressPoint geography;
DECLARE @speed bigint;
DECLARE @direction nvarchar(100);
DECLARE @capturedFrom nvarchar(100);
DECLARE @accuracy decimal;
DECLARE @isDisconnectedPoint bit
SET @I=1;
WHILE (@I <= @totalRecords)
BEGIN
Select @addressPoint=GEOGRAPHY::STGeomFromText (points.Point,4326),@speed=points.Speed,@direction=points.Direction,
@capturedFrom=points.CapturedFrom,@accuracy=points.Accuracy ,
@isDisconnectedPoint= points.IsDisconnect,@accuracy=points.Accuracy from @points points where points.Id=@I
Insert into Tracking(CarrierId,Point,Speed,Direction,CapturedFrom,CapturedAt,Accuracy,IsDisconnect,CreatedOn,IsActive) --inserting mobile addresspoint to Tracking table.
Values(@deliveryBoyId,
@addressPoint,
(@speed*2.237)
,@direction,
@capturedFrom ,
CONVERT(VARCHAR(23), (Select points.CapturedAt from @points points where points.Id=@I), 121),
@accuracy,
@isDisconnectedPoint
,GETDATE()
,1
)
SELECT TOP 1 @trackingId=Id from Tracking order by Id desc
if(@totalRouteCount>0)
BEGIN
SET @j=1;
Insert into Delivery(AddressPointId,TrackingId,RouteId,JobId,CreatedOn,IsActive)
Select (address.Id),@trackingId,rJob.RouteId,rJob.JobId,GetDate(),1
FROM ExistingAddressPointTable address JOIN @route rJob ON rJob.RouteId=address.RouteJobID
where address.point.STDistance(@addressPoint)<200
AND address.IsDelivered=0--Non spatial table but still this is 2nd highest CPU usage query.Frankly speaking,I dont know why.
update addres
set addres.IsDelivered =1
from ExistingAddressPointTable addres inner join @routeJob rout on addres.RouteId = rout.RouteId
where addres.point.STDistance(@addressPoint)<200 --Table have 15 millions record now.In azure server this query takes huge CPU spike
AND addres.IsDelivered=0
END
SET @I=@I+1;
END
IF(@isDisConnected=1)--Updating delivery boy status to deactivate mode to his last tracking record.
BEGIN
Select TOP 1 @trackingId=Id from Tracking where DeliveryBoyId=@deliveryBoyId AND IsActive=1 Order by Id desc
Update Tracking set IsDisconnect=1 where Id=@trackingId
END
END
【问题讨论】:
-
你为什么在这里使用
WHILE? SQL Server excel 是基于集合的方法,而不是迭代方法。你有充分的理由让你的服务器一次处理一个(痛苦的)行吗? -
@Larnu 我正在从移动设备获取一堆地址点。我正在迭代那些
-
DMV 和查询存储对这个过程的 CPU 利用率和其中的查询有什么看法?
-
@DavidBrowne-Microsoft 如何检查。您的指导将对我有所帮助。我检查了 azure 查询性能洞察力,因为我看到更新现有地址点查询和插入交付查询占用了高 dtu 使用率
-
一些缩进和格式化以使代码更具可读性不会有坏处! .....
标签: sql-server sqlperformance sqlgeography