Hello~
I have a page that is running off of a fairly large database (5000 or so
records), and all of this code works, but it is pretty slow. I was wondering if
anyone could give me some tips on how to speed it up? Thanks!
<h4><cfoutput><a href="/forms/enrgp/web-links/index.cfm">Web Links</a> »
All</cfoutput></h4>
<br />
<hr />
<br />
<cfoutput>
<cfset myList = ("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z")>
<cfloop list="#myList#" index="alphabet">
<cfquery name="nav" datasource="nrweblinks">
SELECT
linkName
FROM
weblink
WHERE
linkName LIKE '#alphabet#%'
</cfquery>
<cfif nav.recordCount GTE 1>
<a href="###alphabet#">#alphabet#</a> |
</cfif>
</cfloop>
<br />
<hr>
<br />
<cfloop list="#myList#" index="alphabet">
<cfquery name="links" datasource="nrweblinks">
SELECT
*
FROM
weblink
WHERE
linkName LIKE '#alphabet#%'
ORDER BY
linkName
</cfquery>
<cfif links.recordCount GTE 1>
<h4><a name="#alphabet#"></a><strong>#alphabet#</strong></a></h4>
<p>
<ul>
<cfloop query="links">
<li><a href="#link#" target="_blank">#linkName# »</a>
<cfif linkDescription neq ''><br />#linkDescription#</cfif>
<cfif regionID neq ''>
<cfquery name="getRegion" datasource="nrweblinks">
SELECT
regionName,
regionID
FROM
region
WHERE
regionID = #links.regionID#
</cfquery>
<br /><strong>Region:</strong> <a
href="/forms/enrgp/web-links/region.cfm?regionID=#getRegion.regionID#">#getRegio
n.regionName#</a></cfif>
<cfif countryID neq ''>
<cfquery name="getCountry" datasource="nrweblinks">
SELECT
countryName,
countryID
FROM
country
WHERE
countryID = #links.countryID#
</cfquery>
<br /><strong>Country:</strong> <a
href="/forms/enrgp/web-links/region.cfm?regionID=#getRegion.regionID####getCount
ry.countryID#">#getCountry.countryName#</a></cfif>
<cfif categoryID neq ''>
<cfquery name="getCategory" datasource="nrweblinks">
SELECT
categoryName,
categoryID
FROM
category
WHERE
categoryID = #links.categoryID#
</cfquery>
<br /><strong>Category:</strong> <a
href="/forms/enrgp/web-links/category.cfm?categoryID=#getCategory.categoryID#">#
getCategory.categoryName#</a></cfif>
<cfif subcategoryID neq ''>
<cfquery name="getSubcategory" datasource="nrweblinks">
SELECT
subcategoryName,
subcategoryID
FROM
subcategory
WHERE
subcategoryID = #links.subcategoryID#
</cfquery>
<br /><strong>Subcategory:</strong> <a
href="/forms/enrgp/web-links/category.cfm?categoryID=#getCategory.categoryID####
subcategoryID#">#getSubcategory.subcategoryName#</a></cfif>
</cfloop>
</ul>
</p>
<br />
<p><a href="##top">back to top »</a></p>
<hr />
<br />
<cfelse>
</cfif>
</cfloop>
</cfoutput>
Dan Bracuk - 24 Jul 2008 22:50 GMT
I only the part of your code that was visible without horizontal scrolling,
and, based on that, don't do queries inside loops.
If you don't know what the sql keyword "in" does, I have heard good things
about the book, Teach Yourself SQL in 10 Minutes by Ben Forta.
Ian Skinner - 24 Jul 2008 23:06 GMT
> Hello~
>
> I have a page that is running off of a fairly large database (5000 or so
> records), and all of this code works, but it is pretty slow. I was wondering if
> anyone could give me some tips on how to speed it up? Thanks!
Enable debugging and run this page. Look at the debugging information
paying close attention what on the page is taking the time to run.
HINT: I guarantee this page could be sped up considerable running one
query rather then who knows how many it currently does.
A couple of helpful topics to look up. Joining tables and the group
attribute of the <cfoutput...> tag.
Ian Skinner - 24 Jul 2008 23:10 GMT
> Hello~
>
> I have a page that is running off of a fairly large database (5000 or so
> records), and all of this code works, but it is pretty slow. I was wondering if
> anyone could give me some tips on how to speed it up? Thanks!
Enable debugging and run this page. Look at the debugging information
paying close attention what on the page is taking the time to run.
HINT: I guarantee this page could be sped up considerable running one
query rather then who knows how many it currently does.
A couple of helpful topics to look up. Joining tables and the group
attribute of the <cfoutput...> tag.
semi star gazer - 25 Jul 2008 17:41 GMT
OK, I think I've made a lot of progress! The only piece of code I can't figure
out is how to put the alphabetical anchors into the outputted list of links.
Any suggestions? Thanks!
KC
<cfquery name="links" datasource="nrweblinks">
SELECT
wl.linkName AS lName,
wl.link AS URL,
wl.linkDescription AS description,
wl.regionID AS numRegionID,
wl.countryID AS numCtryID,
wl.categoryID AS numCatID,
wl.subcategoryID AS numSubcatID,
cat.categoryName AS catName,
subcat.subcategoryName AS subcatName,
reg.regionName AS regName,
ctry.countryName AS ctryName
FROM
(((( weblink AS wl
LEFT OUTER JOIN category AS cat
ON wl.categoryID = cat.categoryID)
LEFT OUTER JOIN subcategory AS subcat
ON wl.subcategoryID = subcat.subcategoryID)
LEFT OUTER JOIN region AS reg
ON wl.regionID = reg.regionID)
LEFT OUTER JOIN country AS ctry
ON wl.countryID = ctry.countryID)
ORDER BY
linkName
</cfquery>
<cfoutput>
<ul>
<cfloop query="links">
<li>
<a href="#URL#" target="_blank">#lName# »</a>
<cfif description neq ''>
<br />#description#</cfif>
<cfif numRegionID neq ''>
<br /><strong>Region:</strong> <a
href="/forms/enrgp/web-links/region.cfm?regionID=#numRegionID#">#regName#</a></c
fif>
<cfif numCtryID neq ''>
<br /><strong>Country:</strong> <a
href="/forms/enrgp/web-links/region.cfm?regionID=#numRegionID####numCtryID#">#ct
ryName#</a></cfif>
<cfif numCatID neq ''>
<br /><strong>Category:</strong> <a
href="/forms/enrgp/web-links/category.cfm?categoryID=#numCatID#">#catName#</a></
cfif>
<cfif numSubcatID neq ''>
<br /><strong>Subcategory:</strong> <a
href="/forms/enrgp/web-links/category.cfm?categoryID=#numCatID####numSubcatID#">
#subcatName#</a></cfif>
</li>
</cfloop>
</ul>
</cfoutput>
Ian Skinner - 25 Jul 2008 20:20 GMT
I can think of a couple of ideas.
One: You could do some SQL that would create a column with the the
letters in it for your output.
Two: You could do something using the left(string,1) function in your
current loop.
Three: You could do something a bit like your original loop over a list
of letters and use query-of-query or query array notation to break up
the output.
HTH
Ian
semi star gazer - 29 Jul 2008 19:52 GMT
Could you maybe show some examples of how to do any of these options? I did
something very simple, added <a name="#Left(lName, 1)#"> next to the looped
[LI], but that adds an anchor to every single outputted link, and I have no way
to separate the groups of links, or add back to top buttons. Thanks!
Dan Bracuk - 29 Jul 2008 20:16 GMT
sql to create a column with only the 1st letter is db specific, but this syntax works with at least a couple of them.
select substr(fieldname, 1, 1) as firstletter
semi star gazer - 29 Jul 2008 20:45 GMT
OK, here is what I came up with, using loops again. This definitely works, and
I am pretty happy with it, but any further suggestions are always welcome.
Thanks everyone!
KC
<cfquery name="nav" datasource="nrweblinks">
SELECT DISTINCT
Left(linkName, 1) AS lName
FROM
weblink
GROUP BY
linkName
</cfquery>
<cfoutput query="nav">
<cfif nav.recordCount GTE 1>
<a href="###lName#">#lName#</a> |
</cfif>
</cfoutput>
<br /><hr><br />
<cfloop query="nav">
<cfset myList = ('#lName#')>
<cfloop list="#myList#" index="alphabet">
<cfquery name="links" datasource="nrweblinks">
SELECT
wl.linkName AS lName,
wl.link AS URL,
wl.linkDescription AS description,
wl.regionID AS numRegionID,
wl.countryID AS numCtryID,
wl.categoryID AS numCatID,
wl.subcategoryID AS numSubcatID,
cat.categoryName AS catName,
subcat.subcategoryName AS subcatName,
reg.regionName AS regName,
ctry.countryName AS ctryName
FROM
(((( weblink AS wl
LEFT OUTER JOIN category AS cat
ON wl.categoryID = cat.categoryID)
LEFT OUTER JOIN subcategory AS subcat
ON wl.subcategoryID = subcat.subcategoryID)
LEFT OUTER JOIN region AS reg
ON wl.regionID = reg.regionID)
LEFT OUTER JOIN country AS ctry
ON wl.countryID = ctry.countryID)
WHERE
linkName LIKE '#alphabet#%'
ORDER BY
linkName
</cfquery>
<cfoutput>
<p><a name="#alphabet#"></a><strong>#alphabet#</strong></a></p>
<ul>
<cfloop query="links">
<li>
<a name="#Left(lName, 1)#"></a><a href="#URL#" target="_blank">#lName#
»</a>
<cfif description neq ''>
<br />#description#</cfif>
<cfif numRegionID neq ''>
<br /><strong>Region:</strong> <a
href="/forms/enrgp/web-links/region.cfm?regionID=#numRegionID#">#regName#</a>
</cfif>
<cfif numCtryID neq ''>
<br /><strong>Country:</strong> <a
href="/forms/enrgp/web-links/region.cfm?regionID=#numRegionID####numCtryID#">#ct
ryName#</a>
</cfif>
<cfif numCatID neq ''>
<br /><strong>Category:</strong> <a
href="/forms/enrgp/web-links/category.cfm?categoryID=#numCatID#">#catName#</a>
</cfif>
<cfif numSubcatID neq ''>
<br /><strong>Subcategory:</strong> <a
href="/forms/enrgp/web-links/category.cfm?categoryID=#numCatID####numSubcatID#">
#subcatName#</a>
</cfif>
</li>
</cfloop>
</ul>
<br />
<p><a href="##top">back to top »</a></p>
<hr />
<br />
</cfoutput>
</cfloop>
</cfloop>
Ian Skinner - 29 Jul 2008 21:09 GMT
> OK, here is what I came up with, using loops again. This definitely works, and
> I am pretty happy with it, but any further suggestions are always welcome.
> Thanks everyone!
You have reduced the number of queries considerably here, but you could
get it down to one if you wanted to reduce the number of calls made to
the database and eliminate the need to do 'LIKE' comparisons in your
looped query. Both tend to be costly in performance.
<cfquery name="links" datasource="nrweblinks">
SELECT
wl.linkName AS lName,
wl.link AS URL,
wl.linkDescription AS description,
wl.regionID AS numRegionID,
wl.countryID AS numCtryID,
wl.categoryID AS numCatID,
wl.subcategoryID AS numSubcatID,
cat.categoryName AS catName,
subcat.subcategoryName AS subcatName,
reg.regionName AS regName,
ctry.countryName AS ctryNamem
*** NEW LINE *** LEFT(LINKEDNAME,1) AS LETTER *** NEW LINE ***
FROM
(((( weblink AS wl
LEFT OUTER JOIN category AS cat
ON wl.categoryID = cat.categoryID)
LEFT OUTER JOIN subcategory AS subcat
ON wl.subcategoryID = subcat.subcategoryID)
LEFT OUTER JOIN region AS reg
ON wl.regionID = reg.regionID)
LEFT OUTER JOIN country AS ctry
ON wl.countryID = ctry.countryID)
*** NOTE REMOVAL OF WHERE CLAUSE ***
ORDER BY
*** NEW LINE *** LEFT(LINKEDNAME,1) *** NEW LINE ***
linkName
</cfquery>
<cfoutput query="nrweblinks" group="letter">
<h1>#letter#</h1>
<cfoutput>
#lName#<br/>
</cfoutput>
<hr/>
</cfoutput>
I'll leave it to you to modify this simple output into your desired
interface.
semi star gazer - 30 Jul 2008 17:28 GMT
This is perfect, thank you so much!