4

My website has a search procedure that runs very slowly. One thing that slows it down is the 8 table join it has to do (It also has a WHERE clause on ~6 search parameters). I've tried to make the query faster using various methods such as adding indexes, but these have not helped.

One Idea I have is to cache the result of the 8 table join. I could create a temporary table of the join, and make the search procedure query this table. I could update the table every 10 minutes or so.

Using pseudo code, I would change my procedure to look like this:

IF CachedTable is NULL or CachedTable is older than 10 minutes
    DROP TABLE CachedTable
    CREATE TABLE CachedTable as (select * from .....)
ENDIF

Select * from CachedTable Where Name = @SearchName
                            AND EmailAddress = @SearchEmailAddress

Is this a working strategy? I don't really know what syntax I would need to pull this off, or what I would need to lock to stop things from breaking if two queries happen at the same time.

Also, it might take quite a long time to make a new CachedTable each time, so I thought of trying something like double buffering in computer graphics:

IF CachedTabled is NULL
    CREATE TABLE CachedTable as (select * from ...)
ELSE IF CachedTable is older than 10 minutes
    -- Somehow do this asynchronously, so that the next time a search comes
    -- through the new table is used?
    ASYNCHRONOUS (
        CREATE TABLE BufferedCachedTable as (select * from ...)
        DROP TABLE CachedTable
        RENAME TABLE BufferedCachedTable as CachedTable
    )

Select * from CachedTable Where Name = @SearchName
                            AND EmailAddress = @SearchEmailAddress

Does this make any sense? If so, how would I achieve it? If not, what should I do instead? I tried using indexed views, but this resulted in weird errors, so I want something like this that I can have more control over (Also, something I can potentially spin off onto a different server in the future.)

Also, what about indexes and so on for tables created like this?

This may be obvious from the question, but I don't know that much about SQL or the options I have available.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Oliver
  • 11,297
  • 18
  • 71
  • 121
  • You'll need to script the indexes, constraints etc. They don't come with the new table for free when you use `SELECT INTO` etc. – Aaron Bertrand Feb 13 '12 at 17:23

1 Answers1

7

You can use multiple schemas (you should always specify schema!) and play switch-a-roo as I demonstrated in this question. Basically you need two additional schemas (one to hold a copy of the table temporarily, and one to hold the cached copy).

CREATE SCHEMA cache AUTHORIZATION dbo;
CREATE SCHEMA hold  AUTHORIZATION dbo;

Now, create a mimic of the table in the cache schema:

SELECT * INTO cache.CachedTable FROM dbo.CachedTable WHERE 1 = 0;
-- then create any indexes etc.

Now when it comes time to refresh the data:

-- step 1:
TRUNCATE TABLE cache.CachedTable;
-- (if you need to maintain FKs you may need to delete)
INSERT INTO cache.CachedTable SELECT ...

-- step 2:
-- this transaction will be almost instantaneous, 
-- since it is a metadata operation only: 

BEGIN TRANSACTION;
  ALTER SCHEMA hold  TRANSFER dbo.Cachedtable;
  ALTER SCHEMA dbo   TRANSFER cache.CachedTable;
  ALTER SCHEMA cache TRANSFER hold.CachedTable;
COMMIT TRANSACTION;
Community
  • 1
  • 1
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • Thanks, this looks pretty nifty. I don't appear to be using schemas at the moment (everything is just dbo.[tableName]). – Oliver Feb 13 '12 at 17:24
  • You should still be specifying `dbo.CachedTable` in your query, not `CachedTable` - https://sqlblog.org/blogs/aaron_bertrand/archive/2009/10/11/bad-habits-to-kick-avoiding-the-schema-prefix.aspx – Aaron Bertrand Feb 13 '12 at 17:25
  • I've got this working now and it works really well, thanks for your help. – Oliver Feb 15 '12 at 13:24
  • +1 Never thought of schema-switching (and I use schemas a lot). See http://stackoverflow.com/a/6268753/27535 for where I suggested synonyms or ALTER TABLE SWITCH – gbn Feb 15 '12 at 13:51