Thank you,
I'm wondering how I can check whether these columns have indexes?
Main Topics
Browse All TopicsHello,
I'm trying to improve performance of our asp application which runs strings such as the following in SQL Server -
select distinct tbluser.username, tbluser.userid, tbluser.firstname, tbluser.surname, tbluser.email, max(entrytime) as Entrytime from tbluser JOIN tblvisit ON tblUser.UserID=tblvisit.Us
which is hideously slow as the tables have a lot of data - any tips for tidying it up and improving performance please? This one above took 1:41 mins to run in SQL Query Analyser, and just times out when passed from the asp page to the server.
Many thanks
DebbieL
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You should check the indexes.
On fields where there are a lot of different values and which are needed to search for should be indexed.
Fields on which you do not search or which have only very few different values should not be indexed.
In some cases a compound index may be necessary, check in such case also the order of the fields in the index.
Hopes this helps
1) Tables should have primary key (clustered better)
2) See the relantionships between tables. Foreign keys should be created.
3) Only use distinct keyword if you really need it.
4) Prefix all the columns with correct table name (orgname, username, firstname, surname don't have prefix)
5) If still slow, then create indexes to improve your query execution time (at least the tblvisit.entrytime looks like will need one. tbluser.username too)
I could see that you repeated one filter: (tbluser.active = 1)
It isn't necessary to write it twice! :)
Please try
select username, userid, firstname, surname, email,
(select top 1 entrytime from tblVisit where UserID = U.UserID) as Entrytime
from tbluser U
where companyid = 8 AND active = 1
AND NOT EXISTS (
select 1 from tblvisit
where UserID = U.UserID
and tblvisit.entrytime between '8/Feb/2005' and '15/Feb/2005')
order by 1
1. You don't need the distinct
2. Avoid Not IN / IN Lists and Use NOT EXISTS / EXISTS instead
3. look at the query plans generated
4. use profiler/ index analyser to tune problem queries.
select U.username, U.userid, U.firstname, U.surname, U.email
, max(entrytime) as Entrytime
from tbluser as U
Inner Join JOIN tblvisit as V
ON U.UserID=V.UserID
Inner Join tblOrganisation as O
on companyid = O.id
where U.companyid = 8 AND U.active = 1
AND Not Exists
(select userid from tblvisit as V1
where V1.entrytime between '8/Feb/2005' and '15/Feb/2005'
and V1.UserName=U.userid)
and U.active = 1 and O.active = 1
group by orgname, username, U.userid, firstname, surname, U.email
order by username
I don't believe there is a problem with using Between since SQL can easily convert that into a range select....
you should avoid joining tables via functions where possibvle...
hth
A quick suggestion to try... and the syntax is not tested. I believe the NOT IN (SELECT ... ) is executed every time for each row in the top query. Use a table variable. Also, try to not to us IN use EXISTS and NOT EXISTS. I have more but I'm not sure what docs came from other pay sites.
declare @tableVar table (tUserid [int])
Insert Into @tablevar
(select userid from tblvisit where tblvisit.entrytime between '8/Feb/2005' and '15/Feb/2005')
select distinct tbluser.username, tbluser.userid, tbluser.firstname, tbluser.surname, tbluser.email, max(entrytime) as Entrytime from tbluser JOIN tblvisit ON tblUser.UserID=tblvisit.Us
(SELECT tUserid FROM @tableVar)
and tbluser.active = 1 and tblorganisation.active = 1 group by orgname, username, tbluser.userid, firstname, surname, tbluser.email order by username
So far Hilaire's code works fastest, but has a couple of problems:
1) selecting the "top 1 entrytime from tblVisit" is not equivalent to the max entrytime, and it is the most recent visit I need. No worries, changed that code.
2) it is missing the tblorganisation information which selects only data where an organisation is active, be very grateful if this could be added to the solution?
Best wishes
Debbie
Try :
select username, userid, firstname, surname, email,
(select max (entrytime) from tblVisit where UserID = U.UserID) as Entrytime
from tblOrganisation, tbluser U
where companyid = 8 AND active = 1
AND companyid = tblorganisation.id
AND NOT EXISTS (
select * from tblvisit
where UserID = U.UserID
and tblvisit.entrytime >= '8/Feb/2005' and tblvisit.entrytime <= '15/Feb/2005')
Make sure following fields are indexed : companyid, tblorganisation.id, entrytime, userid
Business Accounts
Answer for Membership
by: ptjcbPosted on 2005-02-15 at 08:14:11ID: 13315150
You could look at the execution plan in Query Analyzer to see where the script is slow. You could also check to be sure that you have indexes in the right columns (for example, companyid, active, userID). You could also set up a trace in Profiler and check for slow-running queries and then use the Index Tuning Wizard on that trace to see if you need other indexes.