r/mysql • • Jun 25 '16

Efficient database design for auto-incrementing, grouped, sets of data?

With the very helpful advice of /u/zero_iq, we figured out a better way to save unique company data by using unique hashes for each entity. Basically, there are multiple hashes associated with a company:

hash1: ABC Corp
hash2: ABC Corp
...
hash6: XYZ Corp
hash7: XYZ Corp

Consequently, I'm attempting to create a hashes table that looks something like this:

hash (unique primary key), company_id (auto increments with the *group*)

For example, if I inserted the above example information, I'd like it to be reflected in the hashes table like so:

hash company_id
hash1 1
hash2 1
hash6 2
hash7 2
hash8 2
hash13 3
hash14 3

From there, things get a bit more tricky. Say I want to add some new hashes, all for the same company but I don't know if the company is in the tables or not. To decide that, I use the following logic:

  1. If none of the new hashes exist in the hashes table, I want to create a new company_id for that group and use that id for these hashes and in subsequent insert statements.
  2. If, however, any of the new hashes already exist in the hashes table, I want to merge all of the company_id's into a new value in the hashes table and any other table that has rows referencing that id. Finally, I want to add any additional data using the new company_id.

Here's a quick test case. Say I have these hashes to add for a single company:

hash1  | ?
hash6  | ?
hash17 | ?
hash18 | ?

The result, after inserting them, should be:

hash company_id
hash13 3
hash14 3
hash2 4 -- company_id modified 1->4, hash2 appeared in the new hashes
hash6 4 -- company_id modified 2->4, hash6 appeared in the new hashes
hash1 4 -- company_id modified, (due to hash2)
hash7 4 -- company_id modified, (due to hash6)
hash8 4 -- company_id modified, (due to hash6)
hash17 4 -- added, new hash
hash18 4 -- added, new hash

Regardless of if this is possible, does this design make any sense? Part of me thinks I'm going to have to do a portion of this with PHP but I am trying to make this as efficient/fast as possible (I will be handling a lot of data with whatever method is chosen). As of now, my workflow is something like this:

  1. Select all company_ids where the hash equals any of the existing hashes
  2. Select the largest company_id from the hash table
  3. If no company_id's exist from step 1, insert appropriate rows with an company_id of max(company_id) + 1
  4. If one or more company_id exists, change all rows with those company_ids to an company_id of max(company_id) + 1
  5. Perform steps 3 and 4 on any other tables that reference the company_id, writing most of the logic in PHP

As you can see, my method neglects MySQL almost entirely. I have a feeling I can use ON DUPLICATE KEY effectively here, and I'm wondering if there's a way to skip step 5 entirely.

1 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/VelvetUndrground Jun 27 '16

Per the last question, I meant the issue would arise because I don't know how many hashes will exist for a given company. There isn't an instance where there are no hashes, because there would then be no way to distinguish the company uniquely. Consequently, I'd need to create a large number of arbitrary columns only to have half of them be null.

Also, I can't guarantee a single company_id won't have more than 4095 hashes... I think the solution needs to be oriented the other way, i.e. the original hash table setup.