Mysql count vs mysql SELECT, which one is faster?

后端 未结 5 971
粉色の甜心
粉色の甜心 2020-12-17 16:41

If I want to do a check a name, I want to see how many rows/name exists in the \"username\" column under users table. Lets say thousands ... hundred of thousands, should I u

5条回答
  •  失恋的感觉
    2020-12-17 16:46

    COUNT(name) or COUNT(*) will be somewhat faster because they do not need to return much data. (see Andrew Shepherd's reply on the semantic difference between these two forms of COUNT, as well as COUNT() ). The focus being to "check a name", these differences matter little with the following trick: Instead than count you can also use

    SELECT username FROM users where username = 'name' LIMIT 1;
    

    Which will have the effect of checking (the existence) of the name, but returning as soon at one is found.

提交回复
热议问题