I have a sql query pulling from a view that is very very slow, I know it is probably the views fault; however I cannot change the view. I have this query and want to see if anyone has suggestions on how to optimize it so that I might get it to be any faster.
SELECT COUNT(id) AS recordnum, firstname, lastname
FROM view
WHERE personid=123 AND transactiondate BETWEEN '9/1/2012' AND '9/30/2012'
GROUP BY firstname, lastname
Consider an index on (personid) and/or (transactiondate) and/or (firstname, lastname), whichever will be the most selective. That is, if the personid will select 1 out of 100,000 records, an index on personid will normally suffice. If the combination of personid+transactiondate give you that selectivity, create an composite index on the two columns.
If this were your single most executed query, or if you just run it often enough, you could also INCLUDE all columns into the index, e.g.