Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 181277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:44:06+00:00 2026-05-11T14:44:06+00:00

I’m working on a search stored procedure for our existing forums. I’ve written the

  • 0

I’m working on a search stored procedure for our existing forums.

I’ve written the following code which uses standard SQL full text indexes, however I’m sure there is a better way of doing it and would like a point in the right direction.

To give some info on how it needs to work, The page has 1 search text box which when clicked will search thread titles, thread descriptions and post text and should return the results with the title matches first, then descriptions then post data.

Below is what I’ve written so far which works but is not elegant or as fast as I would like. To give an example of performance with 20K threads and 80K posts it takes about 12 seconds to search for 5 random words.

ALTER PROCEDURE [dbo].[SearchForums] (     --Input Params     @SearchText VARCHAR(200),     @GroupId INT = -1,     @ClientId INT,     --Paging Params     @CurrentPage INT,     @PageSize INT,                @OutTotalRecCount INT OUTPUT ) AS  --Create Temp Table to Store Query Data CREATE TABLE #SearchResults (     Relevance INT IDENTITY,     ThreadID INT,     PostID INT,     [Description] VARCHAR(2000),     Author BIGINT )  --Create and populate table of all GroupID's This search will return from CREATE TABLE #GroupsToSearch ( GroupId INT ) IF @GroupId = -1     BEGIN         INSERT INTO #GroupsToSearch         SELECT GroupID FROM SNetwork_Groups WHERE ClientId = @ClientId     END ELSE     BEGIN         INSERT INTO #GroupsToSearch         VALUES(@GroupId)     END  --Get Thread Titles INSERT INTO #SearchResults     SELECT          SNetwork_Threads.[ThreadId],         (SELECT NULL) AS PostId,         SNetwork_Threads.[Description],         SNetwork_Threads.[OwnerUserId]     FROM          SNetwork_Threads         INNER JOIN SNetwork_Groups ON SNetwork_Groups.GroupId = SNetwork_Threads.GroupId             WHERE          FREETEXT(SNetwork_Threads.[Description], @SearchText) AND         Snetwork_Threads.GroupID IN (SELECT GroupID FROM #GroupsToSearch) AND         SNetwork_Groups.ClientId = @ClientId   --Get Thread Descriptions INSERT INTO #SearchResults     SELECT          SNetwork_Threads.[ThreadId],         (SELECT NULL) AS PostId,         SNetwork_Threads.[Description],         SNetwork_Threads.[OwnerUserId]     FROM          SNetwork_Threads         INNER JOIN SNetwork_Groups ON SNetwork_Groups.GroupId = SNetwork_Threads.GroupId             WHERE          FREETEXT(SNetwork_Threads.[Name], @SearchText) AND         Snetwork_Threads.GroupID IN (SELECT GroupID FROM #GroupsToSearch) AND         SNetwork_Groups.ClientId = @ClientId   --Get Posts INSERT INTO #SearchResults     SELECT          SNetwork_Threads.ThreadId,         SNetwork_Posts.PostId,         SNetwork_Posts.PostText,         SNetwork_Posts.[OwnerUserId]     FROM          SNetwork_Posts          INNER JOIN SNetwork_Threads ON SNetwork_Threads.ThreadId = SNetwork_Posts.ThreadId         INNER JOIN SNetwork_Groups ON SNetwork_Groups.GroupId = SNetwork_Threads.GroupId             WHERE          FREETEXT(SNetwork_Posts.PostText, @SearchText) AND         Snetwork_Threads.GroupID IN (SELECT GroupID FROM #GroupsToSearch) AND         SNetwork_Groups.ClientId = @ClientId   --Return Paged Result Sets SELECT @OutTotalRecCount =  COUNT(*) FROM #SearchResults SELECT       #SearchResults.[ThreadID],     #SearchResults.[PostID],     #SearchResults.[Description],     #SearchResults.[Author] FROM       #SearchResults           WHERE       #SearchResults.[Relevance] >= (@CurrentPage - 1) * @PageSize + 1 AND      #SearchResults.[Relevance] <= @CurrentPage*@PageSize ORDER BY Relevance ASC   --Clean Up DROP TABLE #SearchResults DROP TABLE #GroupsToSearch 

I know its a bit long winded but just a nudge in the right direction would be well appreciated.

Incase it helps 80% of the query time is taken up when search posts and according to teh query plan is spent on ‘Clustered Index Scan’ on the posts table. I cant see anyway around this.

Thanks

Gavin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:44:06+00:00Added an answer on May 11, 2026 at 2:44 pm

    I’d really have to see an explain plan to know where the slow parts were, as I don’t see anything particularly nasty in your code. Very first thing – make sure all your indexes are in good shape, they are being used, statistics are up to date, etc.

    One other idea would be to do the search on thread title first, then use the results from that to prune the searches on thread description and post text. Similarly, use the results from the thread description search to prune the post text search.

    The basic idea here is that if you find the keywords in the thread title, why bother searching the description and posts? I realize this may not work depending on how you are presenting the search results to the user, and it may not make a huge difference, but it’s something to think about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.