473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Search and write, or write and recover?

The problem: I need to generate a 'unique string' for each row in a
table. I already use auto_increment for system dependencies between tables.

What is the best approach one of these or another?

After generating a candidate 'unique string' the two strategies that
came to mind are:

1. to then search the table's column to see if it is already assigned;
locking the table for write while searching and writing the new row, or

2. set the column to UNIQUE when defining the table. Just go ahead and
write the new row if you get a "non-unique" exception, generate another
'unique string' and try again.

I've tried both on a small XP laptop and get "lock timeout exceptions"
rather quickly using #1. But replace those with lots of re-writes when
there starts to get "collisions" of 'unique string's.

Any recommendations on approaches?

Thanks in advance,
Roy
Apr 4 '06 #1
6 1087
Roy Epperson wrote:
I've tried both on a small XP laptop and get "lock timeout exceptions"
rather quickly using #1. But replace those with lots of re-writes when
there starts to get "collisions" of 'unique string's.

Any recommendations on approaches?


Use a better algorithm for generating unique strings. There are hashing
algorithms that are quite unlikely to generate collisions over a normal
sized set of data.

Try MD5() or SHA1(), which are both referenced with builtin functions in
MySQL.

http://dev.mysql.com/doc/refman/5.0/...functions.html

Regards,
Bill K.
Apr 4 '06 #2
You are more likely to succeed if you use the data itself as a unique
key. If you are using a primary key, that would be considered a
"unique string". If this is coming in from a web server and you need
an additional unique id, you can always use the host generated
UNIQUE_ID.

using an auto-generated key on multiple tables can be very dangereous
if you do not have the proper referential integrity configured. In most
cases I have seen, the auto-incremented field is mostly useless on
child tables. That being said, the field should exist in both parent
and child tables, but should only be auto-increment in the parent - and
value must exist in Parent before inserting into a child -- know as a
Foreign Key.

PK example:

username -- should be the only part of a PK and should be unique by
itself - you can only have one username "myusername" You can have an
auto-increment userid#, but should NOT be a part of this PK.

Depending on what this is for, you should more than likely require the
services of a DBA that knows what he is doing... The great thing about
databases is that "anyone" can build one. The worst thing about
databases is that "anyone" can build one. The problem with most
database apps is that "anyone" built it.

You will never know how your db/app is going to perform until after
deployment and NO amount of hardware/file tuning will EVER correct a
bad database design. "But it worked in test (with only 50 rows) now,
with 1M rows performance really stinks"... DUH!

Apr 4 '06 #3
Thanks for the pointer. However, the unique key is not based on any
data in the table; just "picked out of the air". I don't understand how
MD5 or SHA1 might be used to generate a unique key without some input
data that is varing?
Bill Karwin wrote:
Roy Epperson wrote:
I've tried both on a small XP laptop and get "lock timeout exceptions"
rather quickly using #1. But replace those with lots of re-writes when
there starts to get "collisions" of 'unique string's.

Any recommendations on approaches?

Use a better algorithm for generating unique strings. There are hashing
algorithms that are quite unlikely to generate collisions over a normal
sized set of data.

Try MD5() or SHA1(), which are both referenced with builtin functions in
MySQL.

http://dev.mysql.com/doc/refman/5.0/...functions.html

Regards,
Bill K.

Apr 5 '06 #4
Thanks for the comments.. I understand your points on keys. My "unique
key" is random set of non-whitespace to externally identify some info in
the db whose contents are not yet known when the record to the
information is created.
on*******@firstdbasource.com wrote:
You are more likely to succeed if you use the data itself as a unique
key. If you are using a primary key, that would be considered a
"unique string". If this is coming in from a web server and you need
an additional unique id, you can always use the host generated
UNIQUE_ID.

using an auto-generated key on multiple tables can be very dangereous
if you do not have the proper referential integrity configured. In most
cases I have seen, the auto-incremented field is mostly useless on
child tables. That being said, the field should exist in both parent
and child tables, but should only be auto-increment in the parent - and
value must exist in Parent before inserting into a child -- know as a
Foreign Key.

PK example:

username -- should be the only part of a PK and should be unique by
itself - you can only have one username "myusername" You can have an
auto-increment userid#, but should NOT be a part of this PK.

Depending on what this is for, you should more than likely require the
services of a DBA that knows what he is doing... The great thing about
databases is that "anyone" can build one. The worst thing about
databases is that "anyone" can build one. The problem with most
database apps is that "anyone" built it.

You will never know how your db/app is going to perform until after
deployment and NO amount of hardware/file tuning will EVER correct a
bad database design. "But it worked in test (with only 50 rows) now,
with 1M rows performance really stinks"... DUH!

Apr 5 '06 #5
Roy Epperson wrote:
Thanks for the pointer. However, the unique key is not based on any
data in the table; just "picked out of the air". I don't understand how
MD5 or SHA1 might be used to generate a unique key without some input
data that is varing?


Aha, thanks, I misunderstood what you were doing.

You might want to check out the UUID() function, which is designed to
generate a pseudo-random 128-bit number as a hex string, with a very low
probability of collisions.

http://dev.mysql.com/doc/refman/5.0/...functions.html

Regards,
Bill K.
Apr 5 '06 #6
Better pointer!! Thanks Bill. I never thought to look there..

Bill Karwin wrote:
Roy Epperson wrote:
Thanks for the pointer. However, the unique key is not based on any
data in the table; just "picked out of the air". I don't understand
how MD5 or SHA1 might be used to generate a unique key without some
input data that is varing?

Aha, thanks, I misunderstood what you were doing.

You might want to check out the UUID() function, which is designed to
generate a pseudo-random 128-bit number as a hex string, with a very low
probability of collisions.

http://dev.mysql.com/doc/refman/5.0/...functions.html

Regards,
Bill K.

Apr 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: WKC | last post by:
Recently, one of our database's mdf and ldf was corrupted. We were able to bring back the database with the capability of importing and querying the data. However, the data is not the full list. ...
1
by: Dave Townsend | last post by:
Hi, Can anybody help me with the following piece of code? The purpose behind the code is to parse HTML files, strip out the tags and return the text between tags. This is part of a larger...
16
by: nephish | last post by:
Hey there, kinda newbie question here. i know how to read the lines of a txt file. i know how to write a txt file. but how do i overwrite a line value with another value ? i mean, how do go...
5
by: Prem K Mehrotra | last post by:
I come from Oracle background. In Oracle, when one wants to do a point in time recovery, one can specify recover database until timestmap. Oracle's database maps to a db2 subsystem, i.e., in...
3
by: apple | last post by:
UDB v8 fp 6a on AIX 5.1.0.0 Below is a manual incremental recover from compressed backup datasets. With external compress backup datasets, can it be coded to do an automatic incremental recover?...
13
by: Ray Muforosky | last post by:
Hello all: Task: I want to do file search, using the "conatining text" option from a web page. How do I search for a file on my local drive containing a certain string, from a web page. That...
6
by: gel | last post by:
I would like to write some data recovery software as a learning thing. The sort of thing that you would use to recover data from a currupt HDD or floppy etc. I would like to be pointed in the...
16
by: Kent Feiler | last post by:
If I understand the general direction of recent posts, the idea is to improve the quality of html/css by soliciting help from the various browsers. Browsers can certainly detect problems but they...
0
by: mike_dba | last post by:
I have been testing the db2 recover command on a DB2 V8.2 Linux database. The database contains a single partition. I am not archiving logs but retaining them on disk. the backup image is to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.