Best way to test if a row exists in a MySQL table

后端 未结 12 2218
小蘑菇
小蘑菇 2020-11-22 06:11

I\'m trying to find out if a row exists in a table. Using MySQL, is it better to do a query like this:

SELECT COUNT(*) AS total FROM table1 WHERE ...
         


        
12条回答
  •  醉梦人生
    2020-11-22 06:34

    In my research, I can find the result getting on following speed.

    select * from table where condition=value
    (1 total, Query took 0.0052 sec)
    
    select exists(select * from table where condition=value)
    (1 total, Query took 0.0008 sec)
    
    select count(*) from table where condition=value limit 1) 
    (1 total, Query took 0.0007 sec)
    
    select exists(select * from table where condition=value limit 1)
    (1 total, Query took 0.0006 sec) 
    

提交回复
热议问题