Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 105441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:26:23+00:00 2026-05-11T01:26:23+00:00

Part of the system I’m working on at the moment involves a log in

  • 0

Part of the system I’m working on at the moment involves a log in mysql, with counts being frequently updated.

The data being inserted is of the format:

   date    | name | count | -----------+------+-------+ 2009-01-12 | alan |   5   | 2009-01-12 | dave |   2   | 2009-01-12 | mary |   1   | 

This data is parsed regularly from a flat file, summarised as above in preparation for a db insert/update – the unique key on the database is the (date, name) pair.

Previously, this system would check the existing table for any records for a given date and name pair before deciding on an update or insert.

The problem we’re having is, as this table grows, the response time isn’t getting any better, and we want to reduce the number of queries as much as possible.

The system was recently updated to run a INSERT ... ON DUPLICATE KEY UPDATE query, which has reduced the number of selects marginally, but our common case by some distance is the update.

I’m wondering if anyone knows of a mysql function that’s essentially INSERT ... ON DUPLICATE KEY UPDATE in reverse, i.e. will try to update a row, if none match then perform the insert?


Edit

I didn’t make it too clear above, what I would like to do when I have the record ('2009-01-12','alan','5') for example, is:

UPDATE table SET count = count+5 WHERE date = '2009-01-12' and name = 'alan'; 

and if the above fails, insert the above data. The need to increment a counter is why REPLACE won’t work. Replace performs a delete & insert, and doesn’t let you refer to the row being deleted, so count = count + 5 wouldn’t increment the previous count value for by 5.

@jasoncohen – the INSERT ... ON DUPLICATE KEY UPDATE does do the job, but I’m asking if there’s a more optimal way to do this.

Sorry for any confusion resulting from the poor original phrasing!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:26:24+00:00Added an answer on May 11, 2026 at 1:26 am

    It’s just the same. With ‘UPDATE … ON NO KEY INSERT’, the database engine will still have to check first if there is something to update. Hence no need for a separate construct even if update is most common

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code server part: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;
I've developed a web application in PHP and MySQL. One part of the system
As part of a system I am working on we have put a layer
I am working in a new part of an existing system. What are the
Currently, I am working on a new version control system as part of a
My issue with an old customer plus outdated system.Part of issue. The 3 tables
I'm developing the server part of a system that has to send messages to
As part of one our system's we enable a user in the backoffice to
As a part of our application I need to write simple workflow system which
I currently use a System.IO.FileSystemWatcher as part of a roll your own message queue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.