Solved

Concatenation and recordset loops - speed it up

Posted on 2014-10-09
6
132 Views
Last Modified: 2014-10-16
I have a recordset with code below that loops through each record and concatenates a string for a "<select>" option list. The problem is that the performance suffers greatly after loop through about 1000 records. How can I speed this query up and build the string as it is without suffering performance?

clisttxt = ''
while (!myQuery.eof && xrcount < 10000) {
    clisttxt += '<option value="'+myQuery.comp_companyid+'" '+iscselected+'>'+ myQuery.comp_Name+ ' </option>'
   myQuery.NextRecord();
}

clisttxt += '</optgroup></select>'
0
Comment
Question by:saturation
6 Comments
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
normally, I would recommend using the GetRows method, however since you're using jscript for your server side language, I don't know how much relief you'll get, but I still recommend trying it.

Using this page as an example, your code may look something like this:

var tempArray = myQuery.GetRows();
var recArray = tempArray.toArray();

var col = 1;
var maxCols = myQuery.Fields.Count;

var selectValue, selectText;

clisttxt = ''
for (var thisField=0; thisField<recArray.length; thisField++) {
  if( col == 1 ) 
     selectValue = recArray[thisField];

  if( col == 2 )
    selectText = recArray[thisField];

  if (col > maxCols) {
    clisttxt += '<option value="'+selectValue +'" '+iscselected+'>'+ selectText + ' </option>';
    col = 1;
  }
}

Open in new window

the above code assumes only 2 fields are being returned in your sql query
0
 

Author Comment

by:saturation
Comment Utility
I can't call GetRows() ("object doesn't support...") ...Is there another way to grab the recordset without a loop?/
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
send the options after page load with an ajax request. send only the data to the web browser and build the options with the browser
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 500 total points
Comment Utility
>The problem is that the performance suffers greatly after loop through about 1000 records.

Although jscript runs considerably slower than vbscript, 1000 records is still 1000 records and there should not be a reason to get that many rows of data to the screen and especially for a select/dropdown.   Too much data is sent to the browser and too many choices for the select statement.

If you must, I would write the data to a text file.  You can either place the data inside the option tag via a server side include, serverside ajax (xmlhttppost) or client side ajax.

The serverside include is going to be easy and straight forward.  First you have to write the data to the server.  You can write it as the exact html you you need.

You will have a separate page of pure asp that writes the file.  I have done this for blog tags and worked very nicely where there were many hundreds of tags.  In my case, I run this page every time the blog is posted.  If you have data that is created dynamically from multiple sources, then consider a scheduled task to run this page.

Here it is in vbscript using filesystem object. You will need to give the IIS_IUSRS or IIS_WPG account write permissions for the folder you are saving the text file in.  

I guarantee this will be a huge savings in time although I still think it is way too much data for a select.    

dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateTextFile("c:\intetpub\mydomain\includes\mySelect.asp",true)

do until myQuery.eof
    f.WriteLine("<option value='"&myQuery.comp_companyid& iscselected&">"& myQuery.comp_Name& " </option>")

myQuery.movenext
loop

f.close
set f=nothing
set fs=nothing

Open in new window


Another option instead of a select is to use ajax and typeahead  https://twitter.github.io/typeahead.js/ or http://jqueryui.com/autocomplete/.  These libraries will let you start typing and autocomplete the rest just like google. Probably a much better experience than picking from 1000 dropdown options.
0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
Comment Utility
Almost forgot, you need to include the file http://www.w3schools.com/asp/asp_incfiles.asp

<option>
<!--#include virtual="\mySelect\mySelect.asp"-->
</option>

Open in new window

0
 

Author Comment

by:saturation
Comment Utility
Thanks guys--give me a few days to try these out and I'll get back! I really appreciate it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now