Background
I am currently re-writing a search engine...which I originally wrote a couple years ago in a hurry :)
Today, the search encompasses ~5 different internal business objects/tables: Users, Organizations, News Headlines, etc. So when the user types in "The Quick Brown Fox"
into the search box, he/she will get results of each type of business object.
Each business object has an associated stored procedure: spSearchUsers
, spSearchOrganizations
, etc.
The problem
Now, for each search, I am calling each stored procedure (via a BL -> Data Access Layer) multiple times for each business object type. For example, when the user searches for "The Quick Brown Fox"
, I send down the entire string unaltered, but also split the string on each single space, and search each word individually.
In this case, each stored procedure is called 5 times...a total of 25 separate database calls for a single search. I don't feel like this typical...but it still runs pretty quick. It takes roughly 4-5 seconds per search.
What I'd like to create
A single "master" stored procedure that takes the entire search string "The Quick Brown Fox"
that in turn calls each individual business object stored procedure ( spSearchUsers
, spSearchOrganizations
, etc), performs a little bit of logic, and returns 1 result set.
So 1 search... 1 result.
Can this be achieved using stored procedures, functions, and/or user-defined table types?
If you were using Ms-Sql you could create a Full Text Index that would allow you to search using all the terms without the need to alter the text. Additionally, Stored Procedures can return back multiple results sets, so you could create a new stored procedure to run each query and return all the records back, or one that calls all your previous stored procedures individually (shown below).
CREATE PROCEDURE [dbo].[DoMassiveSearch]
@searchText varchar(200) = null
AS
BEGIN
EXEC spSearchUsers @searchText
EXEC spSearchOrganizations @searchText
END
Supposing you could create a view like this
CREATE VIEW vSearchFields
as
SELECT "U" as tableName, IDUser as ID, UserName as searchField
FROM USERS
UNION
SELECT "O" as tableName, IDOrganizations as ID, OrgName as searchField
FROM Organizations
UNION
SELECT 'N' as tableName, IDNews as ID, newsText as searchField
FROM News
UNION
SELECT 'H' as tableName, IDHeadline as ID, headlineText as searchField
FROM Headlines
Now you could write a simple stored procedure that search for the text in the seachField
CREATE PROCEDURE [dbo].[searchText]
@textToSearch nvarchar(2000)
AS
BEGIN
SELECT * FROM vSearchFields WHERE searchField = @textToSearch
END
The next step will be the introduction of a function to split the input text and do a repetitive search using the text parts, or, if your db version allows (2008 and after), use a Table Valued Parameter to pass a splitted string as a table from C# code to your stored procedure.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.