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 |
|
MightyGorilla
Starting Member
7 Posts |
Posted - 2007-07-16 : 08:34:17
|
By refine, I mean completely rewrite. :)I have a ship-to-location table that can have records added manually throughthe application side, or automatically via EDI address updates fromcustomers. The very-lame application only allows a 4-digit string to be usedas a key for each record.In summary, this means I must generate (in some automated way) a new uniquekey for each new address, but I must work around what is already there.Example: The keys 0000, 0001, 0002, 0089, 00QR may already exist in thetable. I cannot alter the database to make the key field into a uniqueauto-incrementing field - as that will upset the front end (which is notopen-source).My existing solution (which is now painfully slow - after the number ofaddresses it had to parse increased) involves a "constant" table consistingof one column filled with the sequence of values 0-9999. It then selects theMIN() value that does NOT EXIST in the actual address table.Here is the snipet where my problem occurs:SELECT @ship_to_new = MIN(Constant)FROM dbo.GI_ShipToAddressConstantsWHERE Constant NOT IN (SELECT ShipToCode FROM SOB_CustShipToAddressWHERE Division = '00'AND CustomerNumber = @customer_numberAND ISNUMERIC(ShipToCode) = 1) As I listed above - the id is a string, not purely numeric, but at the timethe numeric constant table yeilded plenty of headroom, and would work aroundalpha values entered manually easily enough.This reeks of poor design. I know that. Any clean methods of accomplishingthis, or new approaches?Thank for your time,Travis- |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-07-16 : 08:51:57
|
| ISNUMERIC(ShipToCode)12e3would be let through by that as would a few other things.Bit surprised that is slow.Do you have an index on CustomerNumber, Division, ShipToCode in SOB_CustShipToAddresstrySELECT @ship_to_new = MIN(Constant)FROM dbo.GI_ShipToAddressConstants cleft join (SELECT distinct ShipToCode FROM SOB_CustShipToAddressWHERE Division = '00'AND CustomerNumber = @customer_numberand patindex('%[^0-9]%',ShipToCode) = 0) aon a.ShipToCode = c.Constantwhere a.ShipToCode is nullorSELECT @ship_to_new = MIN(Constant)FROM dbo.GI_ShipToAddressConstants cleft join SOB_CustShipToAddress aon a.Division = '00'AND a.CustomerNumber = @customer_numberand a.ShipToCode = right('0000' + convert(varchar(4),c.Constant), 4)where a.ShipToCode is null==========================================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. |
 |
|
|
MightyGorilla
Starting Member
7 Posts |
Posted - 2007-07-16 : 09:50:41
|
| nr, My original query typically takes 21 seconds (on our server anyway) to find the next available key. Your first attempt cut that to 13 seconds, but your second query is practically instantaneous! Excellent! I've been trying to analyze the execution plans on my queries lately - DBA is not my only hat here - but I still don't completely grasp what it's telling me. I'll have to keep studying them. :)Thanks a bunch, Travis- |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-16 : 11:26:08
|
| It is the IN part that kills your query.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|