Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need for speed

Status
Not open for further replies.

Ricjd

Programmer
Sep 12, 2002
104
GB
Hey there.

I have been working all day to get this script down in load up time. I have got it form 21 secs to 3.5 secs (yes i am that sad that I measured it). Anyway even though 3.5 secs is nearly acceptable I still want to get it down further. What it does is display bookings in an inline frame. The code below is from the inline frame. If you fancy like a challenge or something then have a look at the code below and try and find a way of getting down maybe an extra second or so. Any ideas more than welcome.

Many many thanks to anyone who even looks at this code and hope to hear from you soon.

P.S. You might be better copying and pasting this into your preferred editor

[tt]<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<TITLE> Change Accomodation Details </TITLE>
<META NAME=&quot;Generator&quot; CONTENT=&quot;EditPlus&quot;>
<META NAME=&quot;Author&quot; CONTENT=&quot;&quot;>
<META NAME=&quot;Keywords&quot; CONTENT=&quot;&quot;>
<META NAME=&quot;Description&quot; CONTENT=&quot;&quot;>
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot;href=&quot;main.css&quot;>

</HEAD>

<BODY topmargin=&quot;0&quot; leftmargin=&quot;0&quot;>


<%

sub setColour(dayNow, monthNow, yearNow) 'sub routine to set the colour of the week days

wDay = weekday(cdate(monthNow & &quot;/&quot; & dayNow & &quot;/&quot; & yearNow))
if wDay = 1 or wDay = 7 then
response.write(&quot;<font color = '#FF0000'>&quot; & dayNow)
response.write(&quot;<br>&quot; & WeekdayName(wDay, true) & &quot;</font>&quot;)
else
response.write(dayNow)
response.write(&quot;<br>&quot; & WeekdayName(wDay, true))
end if

end sub

dim prop, propArray, monthView, yearView 'request all strings and set up varibles
monthView = Request.QueryString(&quot;monthView&quot;)
yearView = Request.QueryString(&quot;yearView&quot;)

props = Request.QueryString(&quot;props&quot;)
propArray = split(props,&quot;_&quot;)

startDate = monthView & &quot;/1/&quot; & yearView
finnishDate = monthView + 1 & &quot;/1/&quot; & yearView

for u = 1 to ubound(propArray)

sql = sql & &quot;bookingStatus = 'book' AND bookingStart >= #&quot; & startDate & &quot;# AND bookingProp = &quot; & propArray(u) & &quot; OR bookingStatus = 'book' AND bookingFinish < #&quot; & finnishDate & &quot;# AND bookingProp = &quot; & propArray(u) & &quot; OR &quot;
sql2 = sql2 & &quot;bookingStatus = 'book' AND bookingStart < #&quot; & startDate & &quot;# AND bookingProp = &quot; & propArray(u) & &quot; AND bookingFinish > #&quot; & startDate & &quot;# OR &quot;

next

sql = (left(sql,(len(sql) - 4)))
sql2 = (left(sql2,(len(sql2) - 4)))

if monthView = &quot;6&quot; or monthView = &quot;9&quot; or monthView = &quot;11&quot; or monthView = &quot;4&quot; then 'get days in month
daysInMonth = 30
elseif monthView = &quot;2&quot; then
daysInMonth = 28
else
daysInMonth = 31
end if

Set bookRS = Server.CreateObject(&quot;ADODB.recordset&quot;)
Set bookConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
bookConn.open &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & server.mappath(&quot;db\cpRental.mdb&quot;) & &quot;;&quot;
strSQL = &quot;SELECT bookingID, bookingStart, bookingFinish, bookingProp, bookingClient FROM booking WHERE &quot; & sql & &quot; ORDER BY bookingStart&quot;
bookRS.Open strSQL, bookConn

response.write(&quot;<TABLE class='view'>&quot;) 'start table

response.write(&quot;<TR>&quot;)'write days ion month
for i = 1 to daysInMonth
response.write(&quot;<TD width='60' height='30' class='date' nowrap>&quot;)
call setColour(i, monthView, yearView)
response.write(&quot;</TD>&quot;)
next
response.write(&quot;</TR>&quot;)

for u = 1 to ubound(propArray)

apartmentFree = &quot;F&quot;

Set book2RS = Server.CreateObject(&quot;ADODB.recordset&quot;)
Set book2Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
book2Conn.open &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & server.mappath(&quot;db\cpRental.mdb&quot;) & &quot;;&quot;
strSQL = &quot;SELECT bookingID, bookingStart, bookingFinish, bookingProp, bookingClient FROM booking WHERE &quot; & sql2 & &quot; ORDER BY bookingStart&quot;
book2RS.Open strSQL, book2Conn

Do While not book2RS.EOF
if cstr(book2RS(&quot;bookingProp&quot;)) = cstr(propArray(u)) then
Set guestRS = Server.CreateObject(&quot;ADODB.recordset&quot;)
Set guestConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
guestConn.open &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & server.mappath(&quot;db\cpRental.mdb&quot;) & &quot;;&quot;
strSQL = &quot;SELECT GuestSurname FROM Guest WHERE GuestID = &quot; & book2RS(&quot;bookingClient&quot;)
guestRS.Open strSQL, guestConn

apartmentFree = guestRS(&quot;GuestSurname&quot;)
bookingID = book2RS(&quot;bookingID&quot;)

guestRS.Close
guestConn.close
set guestRS=Nothing
set guestConn=Nothing
end if
book2RS.MoveNext
loop

book2RS.Close
book2Conn.close
set book2Conn=Nothing
set book2RS=Nothing

response.write(&quot;<TR>&quot;)
for i = 1 to daysInMonth
response.write(&quot;<TD width='60' height='30' class='cell' nowrap>&quot;)

Do While not bookRS.EOF

if cstr(bookRS(&quot;bookingProp&quot;)) = cstr(propArray(u)) then

if monthView & &quot;/&quot; & (i) & &quot;/&quot; & yearView = cstr(bookRS(&quot;bookingFinish&quot;)) then
apartmentFree = &quot;F&quot;
elseif monthView & &quot;/&quot; & (i) & &quot;/&quot; & yearView = cstr(bookRS(&quot;bookingStart&quot;)) then
Set guestRS = Server.CreateObject(&quot;ADODB.recordset&quot;)
Set guestConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
guestConn.open &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & server.mappath(&quot;db\cpRental.mdb&quot;) & &quot;;&quot;
strSQL = &quot;SELECT GuestSurname FROM Guest WHERE GuestID = &quot; & bookRS(&quot;bookingClient&quot;)
guestRS.Open strSQL, guestConn

apartmentFree = guestRS(&quot;GuestSurname&quot;)
bookingID = bookRS(&quot;bookingID&quot;)

guestRS.Close
guestConn.close
set guestRS=Nothing
set guestConn=Nothing
end if

end if

bookRS.MoveNext
loop
bookRS.MoveFirst
if apartmentFree = &quot;F&quot; then
response.write(&quot;<A HREF='confirm.asp?startdate=&quot; & monthView & &quot;/&quot; & (i) & &quot;/&quot; & yearView & &quot;?=&quot; & propArray(u) &&quot;' TARGET='_top'><font color='#FF0000'>Free</font></A>&quot;)
else
response.write(&quot;<A HREF='default.asp?Lang=Eng&Page=occupent&booking=&quot; & bookingID & &quot;' TARGET='_top'>&quot; & left(apartmentFree,10) & &quot;</A>&quot;)
end if

response.write(&quot;</TD>&quot;)
next
response.write(&quot;</TR>&quot;)
next

bookRS.Close
bookConn.close
set bookConn=Nothing
set bookRS=Nothing

response.write(&quot;</TABLE>&quot;)

%>

</BODY>
</HTML>[/tt]

 
One of the things I checked when trying to speed up my site, was to find out the time the sql query took and then the rendering time. I personally found the sql query took longer & was able to optimize it. This will at least give you the base time it will take to get the data back & then you can look at which part can be made more efficient.

HTH,

Jessica [ponytails2]
 
For my personal experience, best results are obtained with proper indexing and the use of stored procedures...
 
And if you don't have sql server and are using Access, you'll still get a performance boost if you put all your sql strings into stored queries within the datafile.

D'Arcy
 
Could you clarify what this page is doing?

Here is what I got from looking at it:

1) Passed information: Start date, end date, a collection of booking properties

2) Your attempting to draw a calendar that displays in the blocks (dates) if a specific piece of property (or one of a group) is free or a link if the specific property is booked

3) I got a little lost on the rest

At a guess I would say you should be able to reduce the number of trips to the database by more than half, but I would need more information in order to advise in particulars.

Perhaps a description of the database and what output your attempting to show?

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
i think you shld take the below createobject and open connection out of the for loop. you create too many unnecessary object and connection. those will slow down your performance.

Set bookRS = Server.CreateObject(&quot;ADODB.recordset&quot;)
Set bookConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
bookConn.open &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & server.mappath(&quot;db\cpRental.mdb&quot;) & &quot;;&quot;
 
Yep, too much thrashing with ADO objects and way too much string concatenation going on here.

Jeebers Cripes, build that connection string once and reuse it at least.

And maybe some examination of your queries will help you come up with something that avoids the need to make database probes inside of loops.

A bunch of separate response.write( )s can beat concatenating up a string and doing a single one.

Use VBScript date functions to figure out stuff like days in a month, don't thrash with it in script. Use Date variant data items for dates, don't play around with hunks of strings to represent date parts unless you need to.

For hem'sake use Option Explicit and &quot;call your shots&quot; (Dimension your data items).

And use preplanned queries (queries defined in the database).
 
hey. i think i am being told 3 main things.

1 index the database.

2 stored queries

3 and one databse connection

the first point i am not too sure if a can do it as it is for a live system where booking will be added everyday.

the second point, i am not using sql server but would like to. the only problem i have is that i do not know who to set it up properly. if any one could point me into the right direction of a web sie which could explain it then this would be helpful. also explain stored procudures. and another thing with stored procudures is will i be able to pass varibls to them? if this is not possible will i be able to pass varible to stored procedures in a access database, and how would i call these procedures?

third point which is what i am being old ALL over the place. my original code had 3 times as many connections and was EXTREMLY slow. i have tried putting it all in one connection string but it slowed it down to over a minute!

if anyone could offer more help i would be extremly grateful.

rick
 
Sometimes it is faster to store the recordset in a variable using getRows and closing recordset and connection right after.

There are some decent articles online if you google it you might find some things on how to optimize your code.

Also if you really do a lot of looping you might want to google &quot;unrolling&quot; as an optimization technique. I found that it helped me a lot with some of my code (JS/Java mainly but the logic can still work in VB).

Good luck.

Gary Haran
==========================
 
4) abopve should be re-examining the logic your using in your loops. Right now you have way to many calls to the database embedded inside loops which means that your going to be getting considerably slower with each set of records added.
Looking at your loops you are currently (getting #ofProps * (daysInMonth * (booksRS.RecordCount + 1)) + 1) + 1 recordsets for your heaviest page load.

If we assume they only want 2 properties, in the shortest month (feb, 28 days), and you have had a record low number of bookings (say 10), then you will have opened 311 recordsets by the time your done...
unless I missed an Exit Loop or Exit For statement somewhere.

Say they are comparing 5 properties, in december, and you have a lot of bookings around that time...


Maybe if you outline what your trying to do in that section than we can help give you some directions in rethinking the logic involved in it

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top