here is the query I am stuck with:
SELECT *
FROM customers
WHERE salesmanid = @salesrep
OR telephonenum IN (SELECT telephonenum
开发者_JS百科 FROM salesmancustomers
WHERE salesmanname = @salesrepname)
ORDER BY customernum
It is SLOW and crushing my CPU at 99%. I know an index would help but not sure what kind or if it should be 2 indexes or 1 with both columns included.
Three indexes probably each on a single column. This is assuming that your queries are all quite selective relative to the size of the tables.
It would help if you told us what your table schemas are along with details of existing indexes (Your PKs will get a clustered index by default if you don't specify otherwise) and some details about size of tables / selectivity.
Customers
SalesmanId
TelephoneNum
SalesmanCustomers
SalesmanName
Take a look at the Query Execution Plan and see if there are any table scans going on. This will help you identify what indexes you need.
I suppose that in addition to columns suggested by @Martin, an index on CustomerNum is also required since its used in order by clause.
If you have lots of records, the OrderBy is something which takes a lot of time. You can also try to run query without the orderby and see how much time it takes.
精彩评论