最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql server - When to use OPTIMIZE_FOR_SEQUENTIAL_KEY? - Stack Overflow

programmeradmin0浏览0评论

I have a table (lets call it Packages) that grows continuously and its basically feed by ordersId's, a FK from the Orders table.

The thing is that due to the concurrency nature of the order process, where we only generate a package AFTER you pay your order, the table grows "not straightly"...
If you query the Packages table now, the last orderId on the table will be 12345, and if you query again in a couple of minutes it WILL BE a bigger number, like 12350, but that doesn't mean that the records were inserted in order... perhaps 12350 was the first person to pay and the next one will be 12346...

Is this scenario/table a good candidate for OPTIMIZE_FOR_SEQUENTIAL_KEY?

I have a table (lets call it Packages) that grows continuously and its basically feed by ordersId's, a FK from the Orders table.

The thing is that due to the concurrency nature of the order process, where we only generate a package AFTER you pay your order, the table grows "not straightly"...
If you query the Packages table now, the last orderId on the table will be 12345, and if you query again in a couple of minutes it WILL BE a bigger number, like 12350, but that doesn't mean that the records were inserted in order... perhaps 12350 was the first person to pay and the next one will be 12346...

Is this scenario/table a good candidate for OPTIMIZE_FOR_SEQUENTIAL_KEY?

Share Improve this question edited Feb 3 at 18:29 Dale K 27.5k15 gold badges58 silver badges83 bronze badges asked Feb 3 at 18:15 LeonardoLeonardo 11.4k14 gold badges72 silver badges184 bronze badges 2
  • How many rows fit on a single 8kb page of the table, and how far apart are inserted rows likely to be? Makes sense only when the last page is always the one being inserted. But if you fit 20 rows on the page, and you are often 40 or 60 rows apart then you won;t be on the same page, so the optimization doesn't help you. – Charlieface Commented Feb 3 at 19:05
  • Are you seeing the latch contention issue that this option was introduced to mitigate? – Martin Smith Commented Feb 3 at 19:06
Add a comment  | 

1 Answer 1

Reset to default 4

Is this scenario/table a good candidate for OPTIMIZE_FOR_SEQUENTIAL_KEY?

No. Listen to Pam Lahoud:

You should only use this option if you have a very heavily contentious workload – one where the number of threads inserting into the index is much higher than the number of schedulers – on a clustered index with a sequential key

Behind the Scenes on OPTIMIZE_FOR_SEQUENTIAL_KEY

This is, of course, in addition to seeing significant PAGELATCH_EX wait time. General troubleshooting doc for this is Resolve last-page insert PAGELATCH_EX contention in SQL Server.

发布评论

评论列表(0)

  1. 暂无评论