SELECT MIN(id)
FROM mytable
WHERE lastname IN('Smit','Lee')
GROUP BY name
Here, for optimise this query, index needed (will be useful) column lastname right?
and column name also needed index? or only indexed lastname is enough?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Both the proposed indexes should help in your situation, however that depends a lot on the data you have and how costly certain operations are considered by the database engine, depending on if you have duplicates for id, which I suspect you don’t and that’s why it’s called id, but if you do an 2-column index on
lastname, idorname, idwould also help. Ultimately the query optimizer will decide on the strategy to use to fetch data and that will certainly affect many things…I propose you try the following and see which is faster
lastname– likelyname– a bit less likelylastname, name– most likelyname, lastname– least likelyAgain it really depends on the data but I marked each of those in the order in which I think they should help most for large amounts of data with unique id’s, if your id’s are not unique try combinations with indexes on id as well.