6

I just had a situation where I had to include a column in an index. This required dropping the index, and recreating it. This got me thinking, surely that's a lot of unnecessary work.

Say I had instead created the new index first, then dropped the old one. Let's also assume that I have some way of indicating that the old index will not be dropped until after the new one has been created.

Would the server get any performance benefit by using the old index while creating the new one?

asked Nov 27, 2017 at 20:56
4
  • Which DBMS product are you using? Postgres? Oracle? "SQL" is just a query language, not the name of a specific database product. Commented Nov 27, 2017 at 21:34
  • Yes, I know. I figured an answer to this question would be database-agnostic Commented Nov 27, 2017 at 21:48
  • No it's most definitely not Commented Nov 27, 2017 at 22:31
  • 2
    I've found some funny behavior with index creation that you might be interested in. Commented Nov 27, 2017 at 23:00

1 Answer 1

9

You can do

CREATE TABLE T(A INT, B INT, C INT)
INSERT INTO T 
SELECT 1,1,1
FROM master..spt_values
GO
CREATE INDEX ix ON T(A);
GO
--This won't benefit from existing index as noncovering
CREATE INDEX ix ON T(A) INCLUDE (B) WITH(DROP_EXISTING = ON);
GO
--This can benefit from existing index
CREATE INDEX ix ON T(A) WITH(DROP_EXISTING = ON);
GO
DROP TABLE T;

There is no particular advantage of doing so in the first case.

Whilst the existing index provides the desired sort order it will not be used for this as it does not contain the newly added column B and so it would require a lookup back to the base table for each row. The plan shows a table scan and sort.

enter image description here

The use of DROP_EXISTING here could provide benefit if you were removing an included column though - as in the second DROP_EXISTING example (as the original index would both have the desired order and cover all columns).

The plan for this shows that the original index was read to create the replacement.

enter image description here

answered Nov 27, 2017 at 21:11
2
  • Ooh neat, I've never used WITH(DROP_EXISTING = ON) before. Does the cost of a row lookup really outweigh the benefit of the index scan (rather than a full table sort)? Commented Nov 27, 2017 at 21:55
  • @AndrewWilliamson its the cost of a row lookup for every single row in the table. and yes that is costed as more than a scan and sort. Theres quite a lot of overhead to each lookup even if the pages to be read are already in memory and obv more so if physical IO is required. Commented Nov 28, 2017 at 6:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.