r/mysql • u/VelvetUndrground • Jun 20 '16
More efficient method to check for duplicate database entries?
I am saving organization information in a database, and if duplicate information is found, I want to update the overlapping rows. Some of the unique supported fields are emails, phone numbers, organization names, and latitude/longitude positions. One of the obvious issues I've had to deal with, however, is knowing whether or not an organization is a duplicate of an already saved entry. Consequently, I came up with some basic combinations of unique information that distinguishes one company from another:
- phone number + organization_names
- organization_names + location
- email hash + website hash
- phone + email hash
- phone + website hash
- email hash + location
- email hash + organization name
- website hash + location
- website hash + organization_name
My workflow, as of now is as follows:
- Determine if any of the above unique combinations are satisfied by the new information
- Loop through each combination and create an MySQL query to check the database for duplicate entries
Use PHP's
multi_queryto run semicolon separatedselectstatements for the above queries. These queries look like this:SELECT id, jaro_winkler_similarity(normalized, "ORGANIZATION_NAME") AS organization_name_similarity0 FROM profiles LEFT JOIN phones USING(id) LEFT JOIN (SELECT id, normalized FROM organization_names WHERE normalized LIKE "%ORGANIZATION%" OR normalized LIKE "%NAME%" AS likeMatches USING(id) WHERE (number="6316870100" OR number="123") HAVING organization_name_similarity0 > 85; SELECT id, jaro_winkler_similarity(normalized, "ORGANIZATION_NAME") AS organization_name_similarity0 FROM profiles LEFT JOIN (SELECT id, normalized FROM organization_names WHERE normalized LIKE "%ORGANIZATION%" OR normalized LIKE "%NAME%" AS likeMatches USING(id) LEFT JOIN locations USING(id) WHERE normalized_house_number="110" AND normalized_street_name="WASHINGTON" AND zip="00501" HAVING organization_name_similarity0 > 85; SELECT id, jaro_winkler_similarity(normalized, "ORGANIZATION_NAME") AS organization_name_similarity0, (3959 * acos(cos(radians(40.82127)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-73.051872)) + sin (radians(40.82127)) * sin(radians(latitude)))) AS coordinate_distance0 FROM profiles LEFT JOIN (SELECT id, normalized FROM organization_names WHERE normalized LIKE "%ORGANIZATION%" OR normalized LIKE "%NAME%" AS likeMatches USING(id) LEFT JOIN locations USING(id) HAVING organization_name_similarity0 > 85 AND coordinate_distance0 < 0.1; SELECT id FROM profiles LEFT JOIN emails USING(id) LEFT JOIN websites USING(id) WHERE (hash="93e6b0eff1d85b8b177fc44ce66cd1871dcb89b8") AND (hash="433833d572dc47f8576468a384ef8539a15f4031"); SELECT id FROM profiles LEFT JOIN phones USING(id) LEFT JOIN emails USING(id) WHERE (number="6316870100" OR number="123") AND (hash="93e6b0eff1d85b8b177fc44ce66cd1871dcb89b8"); SELECT id FROM profiles LEFT JOIN phones USING(id) LEFT JOIN websites USING(id) WHERE (number="6316870100" OR number="123") AND (hash="433833d572dc47f8576468a384ef8539a15f4031"); SELECT id FROM profiles LEFT JOIN emails USING(id) LEFT JOIN locations USING(id) WHERE (hash="93e6b0eff1d85b8b177fc44ce66cd1871dcb89b8") AND normalized_house_number="110" AND normalized_street_name="WASHINGTON" AND zip="00501"; SELECT id, (3959 * acos(cos(radians(40.82127)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-73.051872)) + sin (radians(40.82127)) * sin(radians(latitude)))) AS coordinate_distance0 FROM profiles LEFT JOIN emails USING(id) LEFT JOIN locations USING(id) WHERE (hash="93e6b0eff1d85b8b177fc44ce66cd1871dcb89b8") HAVING coordinate_distance0 < 0.1; SELECT id, jaro_winkler_similarity(normalized, "ORGANIZATION_NAME") AS organization_name_similarity0 FROM profiles LEFT JOIN emails USING(id) LEFT JOIN (SELECT id, normalized FROM organization_names WHERE normalized LIKE "%ORGANIZATION%" OR normalized LIKE "%NAME%" OR normalized LIKE "%firm%" OR normalized LIKE "%pc%") AS likeMatches USING(id) WHERE (hash="93e6b0eff1d85b8b177fc44ce66cd1871dcb89b8") HAVING organization_name_similarity0 > 85; SELECT id FROM profiles LEFT JOIN websites USING(id) LEFT JOIN locations USING(id) WHERE (hash="433833d572dc47f8576468a384ef8539a15f4031") AND normalized_house_number="110" AND normalized_street_name="WASHINGTON" AND zip="00501"; SELECT id, (3959 * acos(cos(radians(40.82127)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-73.051872)) + sin (radians(40.82127)) * sin(radians(latitude)))) AS coordinate_distance0 FROM profiles LEFT JOIN websites USING(id) LEFT JOIN locations USING(id) WHERE (hash="433833d572dc47f8576468a384ef8539a15f4031") HAVING coordinate_distance0 < 0.1; SELECT id, jaro_winkler_similarity(normalized, "ORGANIZATION_NAME") AS organization_name_similarity0 FROM profiles LEFT JOIN websites USING(id) LEFT JOIN (SELECT id, normalized FROM organization_names WHERE normalized LIKE "%ORGANIZATION%" OR normalized LIKE "%NAME%" AS likeMatches USING(id) WHERE (hash="433833d572dc47f8576468a384ef8539a15f4031") HAVING organization_name_similarity0 > 85;- If a match is found, grab the associated company id and add new information
- If no matches are found, add a new company id and add the new information to the database
This, to me, seems incredibly superfluous but more importantly is slow. I'd like to condense my MySQL queries (I'm slightly ashamed by them, there has to be a better way to do this) into something more succinct, but I can't think of an appropriate way to do so. Similarly, I can't break if a match is found or a match isn't found, because data might be saved using a different unique combination which needs to be checked as well.
2
u/mcstafford Jun 20 '16
Your first query is missing a close paren, uses a having clause without a group by and has an AS reference within the WHERE statement. That doesn't seem workable.
You're depending solely upon the database to enforce cleanliness of the data you're going to have a bad time. Yes, it can enforce uniqueness, but the app developers and users must also own a portion of the process.
2
u/zero_iq Jun 21 '16 edited Jun 21 '16
Pre-compute message digests (e.g. md5, sha1, sha2) for all combinations 1-9 (and any others you come up with) and store them all as rows in a dedicated company signature table, mapping from digest primary key to company ID. That will allow you to quickly find matches.
When you have new data, compute the digests 1-9 as appropriate on the new item(s), and look in the signature table to see if there's a match. Boom, you have your matching company ID in just 1-9 primary key lookups.
Unfortunately, you will have to keep the signature table up-to-date when you update any information in the tables used to compute them. This may not be trivial, depending on your application architecture. Triggers and stored procedures may help with this task, or if you have a clean database interface in your app that might be a good place to enforce this. MySQL doesn't make this sort of thing easy. (In more advanced databases you could use function indexes or incrementally-updated materialized views for this instead of a separate table, but sadly MySQL doesn't support them. If you have the option of using another rdbms this could save you a lot of time).
Another (possibly easier, if you can use a library) option would be to compute a bloom filter in your application so you can quickly determine if new items are definitely not a match, and only perform your queries when the bloom filter reports their might be a match. You can configure the bloom filter for whatever false-positive rate you find acceptable.
EDIT: you might be able to use Flexviews to maintain the signature table.