Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 to do or not to do (SP or VB) - SEARCH

Author  Topic 

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-10-07 : 11:21:06
Problem
I'm making an application with a Search Form with 9 fields for user to enter seaching criteria
Conditions
The search looks in three tables (>5000 records in each <10000) and can use any of the fields and any combination of fields.
Questions
Should I use a stored procedure, view, or VB?
Do you have any guideline that I can follow so it will be as fast as possible.

Current Situation
I made the search building the SQL Statement in VB and then requesting the data. In my machine it takes around 3-5 seconds from the moment the search button is clicked until the moment the results are displayed. This is a problem, end users usually get a response time much greater than mine so I suppose they will get a response time from 5-10secs. or more and that is not good.

I thought I should use a stored procedure to reduce the amount of data sent from the App to the SQL Server and reduce the sql compiling and uptimizing time and thus reducing the response time but if I use a stored procedure I think i will have to use dynamic SQL and then I'm not sure that I will save in compiling and uptimizing.

And about using a view I have no idea how a view can help me optimize my search




BTW I got this error while making my search in the site

Microsoft OLE DB Provider for SQL Server error '80040e31'

Timeout expired

/forums/search.asp, line 381


*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*

A candle loses nothing by lighting another candle

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2004-10-07 : 11:48:26
I'd definitely use a Stored Proc. You could create a view on your three tables and then reference this in the stored proc. It may also be an option to index this view to improve performance (although it sounds like it's taking a bit of a long time for the number of records you describe - how are the tables indexed?) You don't have to use dynamic SQL. You can do something along these lines:

SELECT Field1, Field2, etc.
FROM MyView
WHERE (Field1 = @Field1 OR @Field1 IS NULL)
AND (Field2 = @Field2 OR @Field2 IS NULL)
etc.



Mark
Go to Top of Page

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-10-07 : 12:39:59
this is the sql statement (modified) I'm using in VB

Select   gl.ID1 as ID1,
gl.ID2 as ID2,
field1 as alias1,
field2 AS EIN, field3 as alias2,
gl.field4 as aliasDate1,
case gl.field5
When '1/1/1900' then
'12/31/2099'
else gl.field5
end as aliasDate2,
glc.field6 as aliasDate3,
glc.field7 as aliasDate4,
field8 as alias3,
field9 as alias4
from tblMaster gl inner join tblDetail1 glc on
gl.ID1 = glc.ID1 and gl.ID2 = glc.ID2
inner join tblDetail2 glp on
glp.ID1 = gl.ID1 and glp.ID2 = gl.ID2
Where @VBBuildCriteria
Order by gl.ID1, gl.ID2, glc.field6, glc.field7


All three tables have PK defined and no extra indexes
tblMaster pk = ID1 char(5), ID2 char(5)
tblDetail1 pk = ID1 char(5), ID2 char(5), field6 datetime, field7 datetime
tblDetail2 pk = ID1 char(5), ID2 char(5), type tinyint

can you make sugestion about the indexes?

*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*

A candle loses nothing by lighting another candle
Go to Top of Page

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-10-08 : 10:32:26
quote:
Originally posted by mwjdavidson

You don't have to use dynamic SQL. You can do something along these lines:

SELECT Field1, Field2, etc.
FROM MyView
WHERE (Field1 = @Field1 OR @Field1 IS NULL)
AND (Field2 = @Field2 OR @Field2 IS NULL)
etc.



Mark



I got it now. When I first looked at your code I saw
WHERE (Field1 = @Field1 OR Field1 IS NULL)
AND (Field2 = @Field2 OR Field2 IS NULL)

instead of
WHERE (Field1 = @Field1 OR @Field1 IS NULL)
AND (Field2 = @Field2 OR @Field2 IS NULL)

silly me

Thanx Mark

*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*

A candle loses nothing by lighting another candle
Go to Top of Page

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-10-08 : 12:50:03
Some advice for indexes (that is the only thing I can change about the tables)

Should I use 9 parameters (clearer SP) or one large parameter in the form of
/fieldCode=fieldValue/fieldCode=fieldValue/fieldCode=fieldValue
easier to call from VB using a code like
'***Each text box control contains in it's tag property the corresponding 
' fieldCode and = sign preceded by the /
'***Criteria will be created concatenating tag and text
' of each text box control in the form that is not empty
For Each ctl In Me
If TypeOf ctl Is TextBox Then
If ctl.Text <> "" Then
criteria = criteria & ctl.Tag & ctl.Text
End If
End If
Next
'***Retrieve results from database
conn.Open cnSTR
Set rec1 = conn.excecute "mySP " & criteria




sugestions please

*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*

A candle loses nothing by lighting another candle
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-10-08 : 20:11:10
You might try:

where charindex(field1+'~'+field2+'~'+field3..field9,@searchstring) > 0

--Ken
I want to die in my sleep like my grandfather, not screaming in terror like his passengers.
Go to Top of Page

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-10-13 : 10:59:06
Done

Aparently there was something else making the search process from VB
to slow. I tried today and it was les than a sec.

But I followed Mark advise and created a SP and a View. Before implementing it I tested 3 options in the QA
The whole statement as I would send it from VB
The SP+view
The SP without view

The whole statement as I would send it from VB costed about twice what the SPs costed. The costs were 50.07%, 24.16% and 25.77% respectively.

*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*

A candle loses nothing by lighting another candle
Go to Top of Page
   

- Advertisement -