September 21, 2012 at 5:24 am
Hi,
I have a table with 1.000.000 or more. It stores the stocks for each product on a store.
Imagine I buy 10 units of each product for each store and want to update the stock...
UPDATE StoreProducts SET Stock = Stock + 2 takes, on my test machine, about 1:20 minutes...
There's an index on ProductID including Stock so I can join with the Products table and know how many there are on all stores.
Using:
ALTER INDEX IDX_ProdutStorages_Stock ON ProdutStorages DISABLE
UPDATE ProdutStorages SET Stock = Stock + 2
ALTER INDEX IDX_ProdutStorages_Stock ON ProdutStorages REBUILD
is faster 20sec....
Is there any other way of doing this?!
Thanks,
Pedro
September 21, 2012 at 7:16 am
UPDATE StoreProducts SET Stock = Stock + 2
UPDATE ProdutStorages SET Stock = Stock + 2
How many tables are you updating?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 21, 2012 at 7:25 am
ChrisM@Work (9/21/2012)
UPDATE StoreProducts SET Stock = Stock + 2UPDATE ProdutStorages SET Stock = Stock + 2
How many tables are you updating?
I wrongly named the 2nd query... It's just StoreProducts.
September 21, 2012 at 8:25 am
Can you post the CREATE INDEX script?
If you're updating the whole table, then nonclustered indexes won't help performance and will impede performance if they too require updating.
Batching the update could help - fewer resources are required for each batch.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 21, 2012 at 8:44 am
the index is as follows:
CREATE NONCLUSTERED INDEX [IDX_ProdutStorages_Stock] ON [dbo].[ProdutStorages]
(
[Stock] ASC
)
INCLUDE ( [Product])
This index was created to determine which products had stock bellow X...
There's another index that's the reverse of that one: on Product and include Stock. That one is used on a INNER JOIN with the products table to get SUM(Stock).
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply