-1

I am seeding a database by copying over data from another database on the same server. In one of the tables, I am doing an INSERT INTO SELECT statement to bring over a few columns. While existing and later data may have geometry, the data that I am inserting has no geometry data.

The query is exceptionally slow, and when I examine the plan, I see that major culprit is the sort that occurs after the GetPlanarGeometryTessellation_VarBinary function. Basically, SQL Server estimates 1,000 rows coming out of this function for each row I am inserting. Since my set contains hundreds of thousands of rows, the plan estimates hundreds of millions of rows going into the sort.

The worst part is the geometry values are NULL, so I am not even explicitly inserting a value into that column!

I tried using the legacy cardinality estimator to no avail. Neither does trying to explicitly define the column as NULL short circuit that part of the execution plan.

I have tried this on both SQL Server 2022 and Azure Managed Instance.

Is this just a bug in SQL's execution engine's estimation and query execution generation? Any suggestions on how I might be able to speed up such a query?

Notice the sort and clustered index insert are 79% of the query, even though I am not putting a value in the column.

CREATE TABLE MyObjects (
    ID UNIQUEIDENTIFIER,
    my_value SMALLINT,
    geom GEOMETRY,
    CONSTRAINT [PK_SMS_ManagedObject] PRIMARY KEY CLUSTERED 
([Id] ASC)
)

CREATE SPATIAL INDEX [IX_MyObjects_Geom] ON [MyObjects]
(
    [Geom]
)USING  GEOMETRY_GRID 
WITH (BOUNDING_BOX =(-90, -180, 90, 180), GRIDS =(LEVEL_1 = MEDIUM,LEVEL_2 = MEDIUM,LEVEL_3 = MEDIUM,LEVEL_4 = HIGH), 
CELLS_PER_OBJECT = 16, PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

INSERT INTO dbo.MyObjects
(
    ID,
    my_value
)
VALUES
(
    NEWID(),
    100
)

https://www.brentozar.com/pastetheplan/?id=2LLhNW30KP

11
  • It must be you have a geometry index in your target db. How slow is slow btw? I don't think tessellation is performed in reality but maybe the potential for tesselation makes the plan non-parallell or bad. Pretty sure you're gonna have to post the query and query plan. For one thing, azure doesn't support cross database queries so I very much doubt this story Commented Nov 10 at 20:21
  • We have a Managed Instance setup. Perhaps I could have mentioned that, but my main point was that the MI is running the most recent version of SQL that MS rolls out on the Azure MI. And yes, there is a spatial index on that column. Commented Nov 10 at 20:28
  • Okey, that makes more sense. What about the QP and query? I can confirm my server does this too at least :) But i suspect the performance is not due to index being there necessarily. For example, a simple insert of 2.5 million rows with NULL geometry takes 00:26 seconds on my bad comp and it doesn't perform the tessellation if you look at the runtime plan, or at least it doesnt scan any rows to do that Commented Nov 10 at 20:35
  • Please read this then edit your question to give more details. Commented Nov 10 at 20:58
  • 1
    Shame you are refusing to engage, this looked like an interesting question. Commented Nov 13 at 14:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.