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
  • 704,970
  • 168
  • 1,303
  • 1,425
uchar
  • 2,466
  • 4
  • 25
  • 49

1 Answers1

0

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

Community
  • 1
  • 1
Nick Louloudakis
  • 5,509
  • 4
  • 33
  • 50