473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Need help refining lengthy re-iterative (looping) query

Hello, everyone!

Here's are the basics:

1. The query looks at all positions that are active and haven't been filled.
2. It then has to look at every single position and determine three things:
Does the person running the query (via an ASP page) have certain rights?
a. Recruit rights
b. Enlist rights
c. Take rights

This works fine for 100 or less positions. But I recently added 5000 positions to the table, and of course the query takes FOREVER to run because it's looping upon itself (a big NO NO).

I've indexed the table and it helped a bit, but I think a view or stored procedure might be in order. Unfortunately, I know little about either, and as you can tell my querying skills are lacking in the sophistication department.

Also, none of the fields can be combined into a single field, hence the Ors in each section. A person could have Recruit/Enlist/Take rights based upon at least 5 fields per right.

Thoughts?

======================= CODE ==========================

sSQL = "SELECT * FROM positions"
sSQL = sSQL & " WHERE active=1"
sSQL = sSQL & " AND filled_date is NULL"
sSQL = sSQL & " AND filled_by is NULL"

Set conn = OpenConn()
Set rsPositions = conn.Execute(sSQL)

Dim j
j = 0

%>

<html>
<head>
</head>
<body>
<div class="mc">
<table border="0" width="100%">

<%


Do while not rsPositions.EOF

nPositionID = rsPositions("position_id")
sTitle = rsPositions("title")
sDescription = rsPositions("description")
dTargetHireDate = FormatDateTime(rsPositions("target_hire_date"),2)

// Recruit Link SQL

if inStr(rsPositions("recruit_by_security"),cSecurity ) > 0 or _
inStr(rsPositions("recruit_by_member"),cUserID) > 0 or _
inStr(rsPositions("recruit_by_state"),cState) > 0 or _
inStr(rsPositions("recruit_by_county"),cCounty) > 0 or _
inStr(rsPositions("recruit_by_wardprecinct"),cWard Precinct) > 0 or _
inStr(rsPositions("recruit_by_zip"),cZip) > 0 then
ShowRecruitLink = "yes"
else
ShowRecruitLink = ""
end if


// Controlled Enlist Link SQL

Set rsEnlist = conn.execute("SELECT enlist_by_member_control FROM action_recruitment_positions WHERE position_id = " & rsPositions(0))
if inStr(rsEnlist(0),cUserID) > 0 then
ShowEnlistLink = "yes"
else
ShowEnlistLink = ""
end if


// Self Enlist Link SQL

if inStr(rsPositions("enlist_by_security"),cSecurity) > 0 or _
inStr(rsPositions("enlist_by_member"),cUserID) > 0 or _
rsPositions("enlist_by_state") = cState or _
rsPositions("enlist_by_county") = cCounty or _
rsPositions("enlist_by_wardprecinct") = cWardPrecinct or _
inStr(rsPositions("enlist_by_zip"),cZip) > 0 then
ShowSelfEnlistLink = "yes"
else
ShowSelfEnlistLink = ""
end if


if ShowRecruitLink = "yes" or ShowEnlistLink = "yes" or ShowSelfEnlistLink = "yes" then

%>

<tr>

<td class="mc">
<b><%=sTitle%></b><br />
<%=sDescription%><br />
Target Hire Date: <%=dTargetHireDate%> <br />
</td>
<td class="mc" align="right" valign="top" align="center">
<%if ShowRecruitLink = "yes" then %>
<a href="#" onclick="window.open('../action_recruitment_invite.asp?action=recruit&posit ion_id=<%=nPositionID%>&invitee_id=<%=cUserID%>', 'recruit', 'toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');">Recruit</a>
<%end if %>
<%if ShowEnlistLink = "yes" then %>
<br /><a href="#" onclick="window.open('../action_recruitment_invite.asp?action=enlist&positi on_id=<%=nPositionID%>&invitee_id=<%=cUserID%>', 'recruit', 'toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');">Enlist</a>
<%end if %>
<%if (ShowSelfEnlistLink = "yes" or ShowSelfEnlistLinkByGeoPol = "yes") then
j = j + 1
%>
<form name="form<%=j%>" action="available_positions.asp">
<input type="hidden" name="position_id" value="<%=nPositionID%>" />
<input type="hidden" name="filled_date" value="<%=date()%>" />
<input type="hidden" name="filled_by" value="<%=cUserID%>" />
<input type="hidden" name="action" value="takePosition" />
<br /><a href="#" onclick="submitStatus(document.form<%=j%>);">Take</a>
</form>
<%end if %>
</td>
</tr>

<%
end if

rsPositions.MoveNext

Loop

%>
</table>
</div>
</body>
</html>

<%
rsPositions.Close
set rsPositions = Nothing

conn.Close
set conn = Nothing
%>

======================= CODE ==========================
Jan 3 '07 #1
5 1908
I thought I might add some information to help explain how this system works:

There are three rights in the recruitment system: Recruit, Enlist, or Take. When a user logs into the system, the system knows certain characteristics about the user: their security level, their member ID, state, county, ward/precinct, and zip code.

So, a typical user might have the following settings:

member_id = 482
security_level = 5
state = MI
county = 81
ward/precinct = 7880-5
zip = 48108

Each position has columns that correspond to each right (Recruit/Enlist/Take) and to each characteristic. So, position 555 has the following columns:

recruit_by_security = null
recruit_by_member = null
recruit_by_state = null
recruit_by_county = null
recruit_by_wardprecinct = 28880-1
recruit_by_zip = 48108

enlist_by_security = 7
enlist_by_member = 890,827,482
enlist_by_state = null
enlist_by_county = null
enlist_by_wardprecinct = 7880-5
enlist_by_zip = null

enlist_by_member_control = 482

So, in comparing the member who's logged in and this position, the member has the following rights:

recruit_by_zip (which would make the Recruit link appear on the webpage)
enlist_by_wardprecinct (which would make the Take link appear)
enlist_by_member_control (which would make the Enlist link appear)

So, if a member has ANY of these rights, the position appears on their page with the appropriate link.

I have given some thought to combining the columns and then delimiting the values, but would this really speed up the query? While I may not be looking at 15 columns (and only be looking at 3), isn't there a way I can keep my structure yet speed up the query?
Jan 3 '07 #2
iburyak
1,017 Expert 512MB
1. Try to create this procedure on a server and play with it first by passing different parameters.

[PHP]Create proc GetPositions
@member_id int,
@security_level int,
@state varchar(2),
@county varchar(50),
@wp varchar(50),
@zip int
as

SELECT 'ShowRecruitLin' Link, *

FROM positions a
left join action_recruitment_positions b on a.position_id = b.position_id
WHERE active=1
AND filled_date is NULL
AND filled_by is NULL
AND (recruit_by_security = @security_level or recruit_by_member = @member_id
or recruit_by_state = @state or recruit_by_county = @county or recruit_by_wardprecinct = @wp
or recruit_by_zip = @zip)
UNION
SELECT 'ShowSelfEnlistLin' Link, *
left join action_recruitment_positions b on a.position_id = -1 -- just to have the same number of columns
FROM positions a
join action_recruitment_positions b on a.position_id = b.position_id
WHERE active=1
AND filled_date is NULL
AND filled_by is NULL
AND (enlist_by_security = @security_level or enlist_by_member = @member_id
or enlist_by_state = @state or enlist_by_county = @county or enlist_by_wardprecinct = @wp
or enlist_by_zip = @zip)[/PHP]

2. to execute stored procedure you have to pass all available parameters to it

[PHP]exec GetPositions 482, 5, 'MI', 81, 7880-5, 48108[/PHP]

3. Play with result and figure out how to display it correctly on your page.


4. To pass parameters from a webpage you should concatinate correct string using variable in place like

[PHP]sSQL = "exec GetPositions " & cUserID & ", " & cSecurity & ", '" & cState & "', " & cCounty & ", " & cWardPrecinct & ", " & cZip[/PHP]

Good luck.
Jan 3 '07 #3
iburyak
1,017 Expert 512MB
Correction to procedure

[PHP]Create proc GetPositions
@member_id int,
@security_level int,
@state varchar(2),
@county varchar(50),
@wp varchar(50),
@zip int
as

SELECT 'ShowRecruitLin' Link, *

FROM positions a
left join action_recruitment_positions b on a.position_id = b.position_id
WHERE active=1
AND filled_date is NULL
AND filled_by is NULL
AND (recruit_by_security = @security_level or recruit_by_member = @member_id
or recruit_by_state = @state or recruit_by_county = @county or recruit_by_wardprecinct = @wp
or recruit_by_zip = @zip)
UNION
SELECT 'ShowSelfEnlistLin' Link, *
FROM positions a
left join action_recruitment_positions b on a.position_id = -1 -- just to have the same number of columns
WHERE active=1
AND filled_date is NULL
AND filled_by is NULL
AND (enlist_by_security = @security_level or enlist_by_member = @member_id
or enlist_by_state = @state or enlist_by_county = @county or enlist_by_wardprecinct = @wp
or enlist_by_zip = @zip)[/PHP]
Jan 3 '07 #4
Thanks, iburyak. I talked to a few friends, who pointed out the stupid "looping upon a query" error that I had made.

So what I ended up doing was querying the entire table with my WHEREs and ORs, and then running some IF-THEN statements mirroring those WHEREs and ORs, and if something was true then spit out the link.

Pretty simple, really, when I look back on it. Just a dumb looping mistake I didn't have to make.

Thanks!
Jan 5 '07 #5
iburyak
1,017 Expert 512MB
I didn't wanted to be very hard on you seeing your struggle... :)

You are welcome.
Jan 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Rithish | last post by:
Hi folks. A premature and redundant question maybe... But I have a TABLE problem. This is almost something like what I want to acheive. ----------------------------------------- | ...
10
by: Tom | last post by:
I am looking for some ideas for how to design the layout of the form for data entry and to display the data for the following situation: There are many sales associates. A sales associate can work...
4
by: Hardy Wang | last post by:
Hi, I have a win form application, when a button is clicked, a lengthy operation will be triggered. During the time program is still running, this application seems not to be able to response to...
3
by: Ed Chiu | last post by:
Hi, I have a relative lengthy .aspx page. There are buttons, datalists datagrids everywhere. When a user scrolls down to the middle of bottom of the page and click on a button, a postback...
5
by: atefshehata | last post by:
hi all, i'm using iis6 installed on win2003 , dotnet framework 1.1 . My problem is.. a page on my application have a lengthy process which takes about 4 minutes (performing database...
3
by: Ben Fidge | last post by:
Hi Our app performs a quite lengthy process when the user clicks a button. We'd like to display a little pop-up "Please Wait..." notification using an animated gif. How would I go about this?...
6
by: Cerebrus99 | last post by:
Hi all, I'm making a Windows application that does some lengthy retrieval operations from a database and possibly from a internet resource. I want to show that the operation is going on, by...
5
by: Jonah Bishop | last post by:
I'm developing a photo album web application for use on a web site, and I'm running into a problem with lengthy operations. My application allows the user to import a number of images at once into...
13
by: michael sorens | last post by:
I have a lengthy sequence of operations that are executed and reported on in a status window in a Windows Form application. Some work is done by background threads but other work is not. I am...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.