Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
hubschrauber
Starting Member
16 Posts |
Posted - 2006-04-05 : 12:06:36
|
| Hello,I build a query in SQL-server 2000 but i'm not happy with the performance, it takes about 15 minutes to execute the query (4 min INSERT and 11 min UPDATE). The table tbl_total has 3 million records and an index on Contract and Item, the table contracts has 1 million records and a key on Contract and Item.How can I speed up this query, is it for example possible to put an index on @table (internal table)?Thanx in advance! DECLARE @table TABLE (Contract nvarchar(15), Item nvarchar(12), Change_date datetime) INSERT INTO @tableSELECT TOT.Contract, TOT.Item, MAX(TOT.Change_date)FROM tbl_total TOTWHERE EXISTS (SELECT 'X' FROM contracts CONTWHERE TOT.Contract = CONT.Contract AND TOT.Item = CONT.Item)GROUP BY TOT.Contract, TOT.ItemUPDATE contractsSET contracts.Change_date = TT.Change_dateFROM contracts INNER JOIN @table TT Oncontracts.Contract = TT.Contract ANDcontracts.Item = TT.Item |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-04-05 : 12:49:56
|
| I take it Contract, Item is unique in contractstry 1.UPDATE contractsSET contracts.Change_date = TT.Change_dateFROM contracts INNER JOIN ( select Contract, Item, Change_date = max(Change_date) from tbl_total TOT GROUP BY TOT.Contract, TOT.Item ) TTon contracts.Contract = TT.Contractand contracts.Item = TT.Item2.select Contract, Item, Change_date = max(Change_date)into #afrom tbl_total TOTGROUP BY TOT.Contract, TOT.Itemcreate unique index ix on #a (Contract, Item)UPDATE contractsSET contracts.Change_date = TT.Change_dateFROM contracts INNER JOIN #a TTon contracts.Contract = TT.Contractand contracts.Item = TT.Item3.DECLARE @table TABLE (Contract nvarchar(15), Item nvarchar(12), Change_date datetime, unique (Contract, Item)) INSERT INTO @tableSELECT TOT.Contract, TOT.Item, MAX(TOT.Change_date)FROM tbl_total TOTWHERE EXISTS (SELECT * FROM contracts CONTWHERE TOT.Contract = CONT.Contract AND TOT.Item = CONT.Item)GROUP BY TOT.Contract, TOT.ItemUPDATE contractsSET contracts.Change_date = TT.Change_dateFROM contracts INNER JOIN @table TT Oncontracts.Contract = TT.Contract ANDcontracts.Item = TT.Item==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|