UPDATE all or UPDATE WHERE?

Discussion in 'MySQL' started by thuankkk, Jun 19, 2009.

  1. #1
    Hi,

    I have a table with a column that contains only 0 or 1. e.g.
    username .... drank coffee
    me ...............0
    Tom .............1
    John .............0

    I set a cron job to update set all 1s into 0.

    I don't add index on column drankcoffee, which query should i use:
    UPDATE userboard SET drankcoffee=0;
    Code (markup):
    or
    UPDATE userboard SET drankcoffee=0 WHERE drankcoffee=1;
    Code (markup):
     
    thuankkk, Jun 19, 2009 IP
  2. buldozerceto

    buldozerceto Active Member

    Messages:
    1,137
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #2
    I think that the first query will be OK.
     
    buldozerceto, Jun 19, 2009 IP
  3. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #3
    Why?

    I think the first query will make mysql crawl all tables and update all records, one by one.
    The second query: search all tables, but update only some records.
     
    thuankkk, Jun 19, 2009 IP
  4. Tribalmaniac

    Tribalmaniac Peon

    Messages:
    7
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In my opinion, definitely the second one. Depending on the size of your database you should be trying to lower any stress on it so I'd go for the second as it should update less rows, as you said :)
     
    Tribalmaniac, Jun 20, 2009 IP
    thuankkk likes this.
  5. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    I would use the second one for efficiency. Not to mention, if you decide you want other numbers (2,4,5 what have you) to be values as well, the first one will always update every entry.

    Regards,
    Dennis M.
     
    Dennis M., Jun 20, 2009 IP
    thuankkk likes this.
  6. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #6
    Thank you very much :) Rep + :)
     
    thuankkk, Jun 20, 2009 IP
  7. gauravajitsaria

    gauravajitsaria Peon

    Messages:
    309
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The second one is better in regard to the amount of work that will have to be done. Its always better to reduce the work load and get the work done in the most efficient and the best way.
     
    gauravajitsaria, Jun 24, 2009 IP
  8. Romocop

    Romocop Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The first query will work.
     
    Romocop, Jun 25, 2009 IP
  9. Wrighty

    Wrighty Peon

    Messages:
    199
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Both WILL work.

    But use the 2nd one :) Querying as little rows as possible makes the query faster! :)
     
    Wrighty, Jun 25, 2009 IP
  10. ice_cube

    ice_cube Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Both of them worked. But the 2nd one is better, faster because it just change some query. If you have little data, you can not figure out the difference, but when you have thousand data, it will show you the difference
     
    ice_cube, Jun 26, 2009 IP
  11. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #11
    wow, suggestions all over the board. Guess some of you missed that he doesn't have an index on the column so specifying won't matter, it's still going to do a table scan. Also, it takes more effort to check and replace than to simply replace.

    Point being unless the optimizer is doing a trick with it the first query is just as good, if not slightly better, than the second.
     
    Goramba, Jun 27, 2009 IP
  12. ramesh.kumar304

    ramesh.kumar304 Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I think second will work.
     
    ramesh.kumar304, Jun 29, 2009 IP
  13. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You can use both, Unless you have other requirements like minimize disk access / minimize speed / minimize CPU load / best for concurrent usage.
    If you have those requirements, then you can run a benchmark which of the two is better in terms of your requirements.
    Then, if you can't explain them, you could ask the intellect on this board to explain the difference.

    At the moment this thread doesn't contribute to anything, since you don't specify the requirements for which it should be better, and you did not even perform some internal benchmarks.
     
    chisara, Jun 29, 2009 IP