我已经在 SQL Server 2016 中的新空间库 SqlGeography
上运行了一些测试,根据 Microsoft 的说法,它应该比以前的版本快很多:
SQL Server 2016 – It Just Runs Faster: Native Spatial Implementation(s). Apply SQL Server 2016 and a breadth of methods and spatial activities are faster and scale better. There are no application or database changes just the SQL Server binary updates showing dramatic improvement.
但是,测试表明新库比旧库慢。
我已经使用 Microsoft 发布的 Nugets Microsoft.SqlServer.Types
在 C# 中对其进行了测试。我已经针对版本 14 (SQL Server 2016) 测试了版本 11。
我应该怎么做才能让新的空间库表现得更好?
小测试的源码是:
var line1 = CreateLine(56, -4, 58, 16);
var line2 = CreateLine(58, -4, 56, 16);
for (int i = 0; i < 50000; i++)
{
var intersection = line1.STIntersects(line2);
var contains = line1.STBuffer(1000).STContains(line1);
}
public static SqlGeography CreateLine(double fromLat, double fromLon, double toLat, double toLon)
{
SqlGeographyBuilder constructed = new SqlGeographyBuilder();
constructed.SetSrid(4326);
constructed.BeginGeography(OpenGisGeographyType.LineString);
constructed.BeginFigure(fromLat, fromLon);
constructed.AddLine(toLat, toLon);
constructed.EndFigure();
constructed.EndGeography();
var line = constructed.ConstructedGeography;
return line;
}
最佳答案
Microsoft 在这篇文章中写道,Microsoft.SqlServer.Types 在 SQL Server 2016 的 T-SQL 代码中不再使用。 https://blogs.msdn.microsoft.com/psssql/2016/03/03/sql-2016-it-just-runs-faster-native-spatial-implementations/
它在 SQL 2014 中的工作原理:
As the SQL Server Spatial data types have matured we uncovered the Unmanaged (SQL Server) to Managed (Microsoft.SqlServer.Types) to SqlServerSpatial###.dll (unmanaged) transitions (PInvoke and PUnInvoke) may become a scalability bottleneck
在 SQL 2016 中:
(T-SQL) SQL Server 2016 invokes the native implementation of the methods, avoiding the Unmanaged to Managed to Unmanaged transitions, improving performance.
SQL 2016 似乎直接使用了 SqlServerSpatial###.dll 并且仅在 T-SQL 代码中提高了性能
关于c# - SqlGeography 空间操作缓慢 - SQL Server 2016,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43818613/