I have a query to return the ids of failed logins that are newer than a supplied date, and also newer than the last successful login.Table:########################################################                   user_attributes                   ######################################################### id # u_id #        label      #  attribute_datetime ######################################################### 1  #  33  #    failed_login   # 2012-05-21 09:03:00 ## 2  #  33  # succcessful_login # 2012-05-21 09:04:00 ########################################################Query:SELECT  count(id)FROM    `user_attributes` as uaWHERE   u_id = 33AND label = 'failed_login'AND ua.attribute_datetime > '2012-05-21 03:04:00'AND ua.attribute_datetime > (    SELECT  attribute_datetime    FROM    `user_attributes`    WHERE   u_id = 33    AND label = 'successful_login');Returns:############### result_set ################     id     ################            ###############Now if we add another failed login:########################################################                   user_attributes                   ######################################################### id # u_id #        label      #  attribute_datetime ######################################################### 3  #  33  #    failed_login   # 2012-05-21 09:10:00 ########################################################And run the query again it should return this:############### result_set ################     id     ################     3      ###############Is there a better way to write this query? I am looking to improve the speed of the query as this is going to run every time a user logs in, and the table is going to be large if not huge.
Return the ids of failed logins
performance;sql;mysql
The code looks good to me as well except if you are looking for the ids of the failed logins you would not want to count the ids (SELECT count(id)) but instead just select the id (SELECT id).