- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I agree, although it's obvious that whoever posted this story didn't quite understand that.
Also, what kind of crappy DB setup are they using that "almost brings down the server" on a mere 13000 compares? I understand if the db rejects the statement as too long, but 13000 comparisons on an id field should be barely noticeable.
Admin
Another great mind
Admin
And another great mind
Admin
Probably because idiots used text for PK, and didn't know about casting...
Admin
Isn't there some kind of upper limit on how long a SELECT statement can be, or how many WHERE conditions it can contain?
Admin
If so, start to UNION more stuff in!
Admin
Ouch! This one literally took my breath away :-(
Admin
Hey, quiet guys! If they find out about sequences and autonumber fields, what happens to the poor guy in Kazakhstan who updates the query every time a new customer is added? Are you trying to put him out of a job?
Admin
This is a very typical situation where you are not allowed to add a column to a database table because it will break things.
The alternative options should be to add another table, with customer_id as the key and then join it with the other table, but even then creating a new table is considered nearly taboo in some places.
The query is likely to have been script-generated someway rather than hand-written.
It is of course highly inefficient compared to using an extra column or table with join.
Admin
I ran into a mildly amusing case of this once. We had a table with an "exported" field, defined as boolean. Normally almost all the records are exported and only a few new ones are not. The query to find the unexported records said "where exported != true". This took forever to run. I discovered that it was doing a sequential search of the entire table. I changed it to "where exported = false" and it ran in a millisecond, because now it used the index.
Admin
Of course if all you want is the next available customer_id, use an identity column as the primary key.
Admin
Of course if all you want is the next available customer_id, use an identity column as the primary key.
Admin
ORM would have solved this problem completely and wholly. No need for silly sql to compound problems where none should exist only in the first place.
Admin
Admin
I guess we'll just have to agree to disagree on that one.
Admin
For this query, no, but...... there are legitimate times the last_insert_id function in mySQL can break / not act as expected. For example, try this: have a table with an auto-generated id. Put 3 items in there, one by one. Run last_insert_id and you will get 3. Now use one line to insert three more rows (as opposed to individually as before). Now run last_insert_id. What do you expect? 6. What do you actually get from the function? 4.
Thats because that function only returns the FIRST Id it generated for your connection for multiple inserts in the same line - not the last, not a listing, but only the first. If you were relying on the built-in functionality your code would be borked.
To me, more surprising than people not using the built-in functions are the people who assume the built in functions always act as expected in typical situations.
Admin
Admin
It is critical that they leave space incase they need to insert a customer, but they will only know where when it happens.
Admin
The real WTF is that (at least in previous versions) you could insert into it which would break about everything (a SELECT 4*3 FROM DUAL would now return two rows).
Admin
dual perversion
http://awads.net/wp/2005/11/08/insert-into-dual/
and THE solution:
DECLARE
CURSOR C IS SELECT Customer_Id FROM Customer ORDER BY Custormer_Id;
R C%ROWTYPE;
ID VARCHAR2(500);
BEGIN
OPEN C; -- OPEN & FETCH & WHILE & CLOSE rather than FOR...LOOP (reading comprehension) FETCH C INTO R; WHILE C%FOUND LOOP
END LOOP;
CLOSE C;
INSERT INTO Customer_Max_Id VALUES (ID);
END; /
And then, fetch just ONE!! REGISTRY with your java app this silly table...
Admin
sorry... commit forgotten
Admin
I guess those wondering why you would ever want to do this have never worked on a legacy system. In the days before disk space was cheap, people used to use ID numbers with as small a range as possible. If the programs are tightly coupled to the database then it's often easier to implement a way of re-using old numbers than to increase the field size throughout the whole system. Particularly on something like customer number which will be everywhere! I bet this is why certain numbers (like 12) are 'missing' from the query.
Having said that there are more scalable ways to achieve this. I'm just not at all surprised that code like this exists and auto_increment doesn't solve all problems in the real world where cost to implement is a factor.
Admin
I once worked on a database with seemingly random number assigned to client, it made no sense why they would not use auto id, then one day i had an epiphany and realized they were the clients phone numbers, none in the development crew had any idea, when i exposed this to the sale team they answered something along the line of; but of course how else would we remember all those numbers...
Admin
Now that's just sad. Be brage Ruben ! Good luck
Admin
yes people steal in ireland too in this case an ATM - see the police officer? ...and they didn't use the digger as a getaway car either so sorry dudes nothing hilariously stupid happened on this occasion in ireland
Admin
Don't mock this technique, it works 99% of time. Not bad odds.
Admin
argh, they're not even sparse... not that it would matter...