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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:46:56+00:00 2026-05-13T18:46:56+00:00

I’m working on an interactive contact search page (contacts are returned via ajax as

  • 0

I’m working on an interactive contact search page (contacts are returned via ajax as you type or select criteria). I want this page to be very responsive.

There is a complex set of rules to determine which contact records a given contact can see; these rules are rolled up into a user-defined function, DirectoryContactsByContact(@ContactID). I’ve optimized this function considerably but it’s still a little expensive (1-2 seconds to execute), so to improve performance I’m thinking about something like this:

  • When the page loads, cache DirectoryContactsByContact for this user as a SQL table, e.g. cache_DirectoryContactsByContact_1
  • Perform the search against the cached table (checking each time to make sure it exists)
  • After a little while (say 30 minutes) kill the cache

It’s OK if the data gets stale during this period, so I’m not concerned with invalidation.

Temporary tables don’t last between requests, so it seems like I’d need to create the cache table as a permanent table; but then I’d need to be responsible for cleaning up old caches myself, which looks non-trivial at first glance.

Are there any mechanisms within SQL Server that would make this easier? Any advice on alternative approaches?

  • 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-13T18:46:57+00:00Added an answer on May 13, 2026 at 6:46 pm

    I’ve ended up creating a rudimentary general-purpose framework for caching the results of a SQL function or view to a table.

        Public Sub CreateCacheTable(ByVal SourceView As String, ByVal FieldList As String)
            Dim CacheTable As String = GetCacheTableName(SourceView)
            If Not TableExists(CacheTable) Then
                Dim Sql As String = " Select ~FieldList~ Into ~CacheTable~ From ~SourceView~ ". _
                    Replace("~CacheTable~", CacheTable). _
                    Replace("~FieldList~", FieldList). _
                    Replace("~SourceView~", SourceView)
                ExecuteNonQuery(cs, CommandType.Text, Sql)
            End If
        End Sub
    
        Public Function GetCacheTableName(ByVal SourceView As String)
            Dim Result As String = "_c_~SourceView~". _
                Replace("~SourceView~", SourceView). _
                Replace(".", "_"). _
                Replace(",", "_"). _
                Replace("[", ""). _
                Replace("]", ""). _
                Replace("(", ""). _
                Replace(")", "")
            Return Result
        End Function
    
        Public Sub CleanupCacheTables()
            ExecuteNonQuery(cs, CommandType.StoredProcedure, "CleanupCacheTables") 
        End Sub
    

    When the page loads I do this:

            CleanupCacheTables()
            CreateCacheTable(SourceView, FieldList)
    

    For example, if SourceView is DirectoryContactsByContact(123) this creates a table named _c_DirectoryContactsByContact_123.

    Here’s the SQL for CleanupCacheTables:

    Create Procedure CleanupCacheTables as
        /* Finds all tables starting with _c_ that were created more than 30 minutes ago and drops them */
        Declare @TableName nvarchar(255)
        Declare CacheTableCursor Cursor for
            Select 
                TableName=name
            From SYS.OBJECTS
            Where Type_Desc = 'USER_TABLE'
            And Left(name,3)=  '_c_'
            And DateDiff(minute, create_date, GetDate())>30
        Open CacheTableCursor
        Fetch Next from CacheTableCursor into @TableName
        While @@FETCH_STATUS = 0 Begin
            Exec ('Drop Table ' + @TableName)
            Fetch Next from CacheTableCursor into @TableName
        End -- While
        Close CacheTableCursor
        Deallocate CacheTableCursor
    Go
    

    This is crude: There’s no invalidation, and it probably won’t scale to a lot of concurrent users and/or very large datasets. Nevertheless, in my case it’s resulted in near-instantaneous results as the user types or selects search criteria, with very little overhead.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

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.