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 5930919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:37:16+00:00 2026-05-22T14:37:16+00:00

I am implementing a searching mechanism in a website and stumbled upon the SQL

  • 0

I am implementing a searching mechanism in a website and stumbled upon the SQL aspect of it.

The user can search for stories by any combination of these filters: story title, story tag, or the username of the story’s author. If no filter is provided, then just return all stories.

My immediate solution to this is this stored procedure:

(
@TitleFilter varchar(50) = NULL
,@TagFilter varchar(30) = NULL
,@UserFilter varchar(30) = NULL
)

SELECT
    story.Title
    ,story.AddedDTS

FROM
    Stories story
    INNER JOIN FREETEXTTABLE(Stories, Title, @TitleFilter) ft
        ON ft.[key] = story.ID
    LEFT JOIN StoryTags st
        ON st.StoryID = story.ID
    LEFT JOIN Tags tag
        ON tag.ID = st.TagID
    LEFT JOIN StoryUser su
        ON su.StoryID = story.ID
    LEFT JOIN Users u
        ON u.ID = su.UserID

WHERE
    1=1
    AND (
            (@TagFilter IS NULL AND @UserFilter IS NULL)
            OR (@TagFilter IS NOT NULL AND tag.Name = @TagFilter)
            OR (@UserFilter IS NOT NULL AND u.Username = @UserFilter)
        )

There’s a couple of problems with this, though, and I am yet to find a better approach.

First off, the Stories table has full-text search enabled, so I have to use the FREETEXTTABLE mechanism, which mandates the predicate to not be NULL, so this stored procedure will not work with @TitleFilter set to NULL.

Secondly, if I only search by title, then joining to StoryTags, Tags, StoryUsers, and Users tables are merely a useless overhead.

So the question is for both points I stated: can I conditionally omit joins to optimize the execution time?

If there’s a whole different approach to this, you are more than welcome to share it; I advocate thinking out of the box.

  • 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. Editorial Team
    Editorial Team
    2026-05-22T14:37:17+00:00Added an answer on May 22, 2026 at 2:37 pm

    Given that when @TitleFilter is null it doesn’t work with FREETEXTTABLE I would either break it up with an if statement like so.

    IF @TitleFilter is not null
    
        SELECT
    
            story.Title
            ,story.AddedDTS
    
        FROM
            Stories story
            INNER JOIN FREETEXTTABLE(Stories, Title, @TitleFilter) ft
             ON ft.[key] = story.ID
    ELSE 
    
        SELECT
            story.Title
            ,story.AddedDTS
    
        FROM
            Stories story
            LEFT JOIN StoryTags st
            ON st.StoryID = story.ID
            LEFT JOIN Tags tag
            ON tag.ID = st.TagID
            LEFT JOIN StoryUser su
            ON su.StoryID = story.ID
            LEFT JOIN Users u
            ON u.ID = su.UserID
        WHERE
    
            (@TagFilter IS NULL AND @UserFilter IS NULL)
             OR (@TagFilter IS NOT NULL AND tag.Name = @TagFilter)
            OR (@UserFilter IS NOT NULL AND u.Username = @UserFilter)
    

    Or if that offends you for some reason there’s always The Curse and Blessings of Dynamic SQL

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

Sidebar

Related Questions

I'm implementing a search in my website, and would like to support searching for
I am implementing a search functionality in a website I am building, which involves
I am implementing search bar in my iphone app to apply searching on array
When implementing a needle search of a haystack in an object-oriented way, you essentially
While implementing a design using nested generic collections, I stumbled across those limitations apparently
Our company currently implementing TSA (Time Stamp Authority) service. And now we are searching
I am implementing mobile local search in iPhone application: - (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
Which data structure can perform insertion, deletion and searching operation in O(1) time in
I am confused with building the query. I am implementing the search feature in
I am looking at implementing a CouchDB server to provide ad-hoc searching of some

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.