MySQL 检查登录详细信息(用户名和密码)的最佳查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13581168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Optimal query for checking login details(username and password)
提问by Sajith
Which is optimal query for check whether the username ans password present in MySql database.
这是检查 MySql 数据库中是否存在用户名和密码的最佳查询。
1)SELECT * FROM login_details WHERE username='username' AND password='password'
2)SELECT count(*)FROM login_details WHERE username='username' AND password='password'
3)SELECT count(Username)FROM login_details WHERE username='username' AND password='password'
4)SELECT 1 FROM login_details WHERE username='username' AND password='password'
Thanks
谢谢
回答by eggyal
Hopefully 'password'
is actually a salted hash of the user's password, and you're passing both literals from your application as parameters to a prepared statement.
希望'password'
实际上是用户密码的加盐散列,并且您将应用程序中的两个文字作为参数传递给准备好的语句。
Assuming that you have an index on (username, password)
, the "optimum" would be:
假设您在 上有一个索引(username, password)
,“最佳”将是:
SELECT EXISTS (
SELECT * FROM login_details WHERE username = ? AND password = ?
)
This way, MySQL stops searchingonce it encounters a single result.
这样,一旦遇到单个结果,MySQL 就会停止搜索。