0

I have a database table with two columns:

CREATE TABLE Main(Id TEXT, Count INTEGER)

I'm currently using something like bellow code for adding a number to a column if it already exists :

query = SELECT * FROM Main WHERE Id=some_word LIMIT 1

if(query has some elements){
     UPDATE Main SET Count=query.count+word_count WHERE Id=some_word
}
else{
     INSERT INTO table_name SELECT some_word AS Id , word_Count AS Count 
     //some more UNION SELECT ...
}

How can I make those statements faster ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
uchar
  • 2,552
  • 4
  • 29
  • 50

1 Answers1

0

You can use INSERT ... ON DUPLICATE KEY UPDATE. For more information, look here.

Community
  • 1
  • 1
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54