On delete cascade with TOP never returns

65 Views Asked by At

In Azure SQL I have the following tables: orders (small) -- shipments (bigger) -- destinations (huge)

create table orders
(
  id                        bigint    not null,
  constraint order_pk primary key (id),
  delivery_date             date      not null,
  latest                    bit       not null,
  created_on                datetime2 not null,
)
create index orders_house_keeping_idx on orders(created_on, latest);

create table shipments
(
  id               bigint    not null,
  constraint shipment_pk primary key (id),
  order_id         bigint    not null,
  constraint shipment_parent_order_fk foreign key (order_id) references orders (id),
  delivery_runtime tinyint   not null,
  created_on       datetime2 not null,
)
alter table shipments
  add constraint shipment_parent_order_fk foreign key (order_id) references orders (id) on delete cascade;
create index shipments_order_id_fk_idx on shipments (order_id, delivery_runtime);

create table destinations
(
  id             bigint       not null,
  constraint destination_pk primary key (id),
  order_id         bigint    not null,
  prefix         varchar(255) not null,
  shipment_id    bigint       not null,
  constraint destination_parent_shipment_fk foreign key (shipment_id) references shipments (id),
  created_on     datetime2    not null,
)
alter table destinations
  add constraint destination_parent_shipment_fk foreign key (shipment_id) references shipments (id) on delete cascade;
alter table destinations
  add constraint destination_order_id_fk foreign key (order_id) references orders (id);
create index destinations_order_id_fk_idx on destinations(order_id)

If I execute the following delete (cascaded)

delete top(2) o from orders o         
where o.created_on < '2023-08-25' and o.latest = 0

the query returns within the following times:

delete top(1) o from kfc.orders o ..... always 5 seconds to execute
delete top(2) o from kfc.orders o ..... always 5 seconds to execute
delete top(3) o from kfc.orders o ..... query never returns?!
delete top(4), top(5), top(6) ..... query never returns?!

What happens here?! Why top(3+) never returns?

  • There is no relevant cpu, ram, dataIO change visible on the db server
  • Playing around with the OPTION (MAXDOP 0) has no effect
  • I don't even get a query execution plan for a top(3+) query

For a o.latest = 1 query, the execution returns immediately for top(29) but fails to return vor top(30+) ?

delete top(29) o from orders o        
where o.created_on < '2023-08-25' and o.latest = 1
0

There are 0 best solutions below