[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.8

A better way to check for duplicate before INSERT?

Asked by TunaMaxx in MySQL Server

Tags: duplicate, insert, before, check

Hello,

  I have a script that I use to parse flat log files and INSERT the info into a mySQL table. There are frequently very similar log entries, but for the most part, each is unique. Obviously I want to log the similar entries but skip anything that may have already been INSERTED. (For reasons beyond my control, random duplicate entries show up in subsequent log files about 5% of the time.)

  What I have been doing is a "SELECT * WHERE..."  on a few key columns (all of which are indexed - that sped things up A LOT!) to look for a match. If it finds one, obviously it skips the INSERT statement. However, when no match is found, the new information is added.

  Here (buried in some PHP seudo-code) is pretty much what I have been using to now:

/* start pseudo-code ****************************************************/
// $dupe will look at the DB and see if there is already an entry that matches what we are about INSERT
  $dupe = "SELECT * FROM x WHERE this = '$that' AND this_too = '$that_too' AND this_one = '$that_one'";

  $result = mysql_query( $dupe ) or die ( 'Unable to check for duplicates.' );
  $num = mysql_numrows( $result );

if ( $num != 0 ) {
    // A matching log entry was found - do not insert a duplicate value.
    echo "Duplicate.<br>\n";

} else {

    // Insert new info
    $sql = "INSERT INTO x_print (
                id,
                this,
                this_too,
                this_one,
                blah_1,
                blah_2
                blah_3,
                etc
          ) VALUES (
                '',
                '$that',
                '$that_too',
                '$that_one',
                '$blah_1',
                '$blah_2',
                '$blah_3',
                '$etc'
                )";

     if (!mysql_query ($sql)) {
       echo mysql_error($Link);
       die('The query could not be executed!');
     }
     echo "Inserted!<br>\n";
 }
/* end seudo-code ****************************************************/

  This method is AT LEAST 10x slower than the INSERT statements alone. Some of these log files have 100,000+ lines, and will generate 5,000 to 10,000 rows. Therefore, that's 5,000 to 10,000 SELECT statements too.

   Is there something fundementaly wrong with how I am going about this?

  Is there a better way?

Thanks,

Tony
[+][-]12/08/04 07:29 PM, ID: 12779953Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: MySQL Server
Tags: duplicate, insert, before, check
Sign Up Now!
Solution Provided By: arantius
Participating Experts: 2
Solution Grade: A
 
[+][-]12/08/04 06:35 PM, ID: 12779746Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/08/04 06:36 PM, ID: 12779753Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/08/04 06:45 PM, ID: 12779800Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/08/04 07:24 PM, ID: 12779939Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/08/04 08:46 PM, ID: 12780205Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/08/04 09:04 PM, ID: 12780258Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/08/04 09:06 PM, ID: 12780261Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/09/04 01:09 AM, ID: 12781075Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/09/04 06:13 AM, ID: 12783291Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93