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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:12:15+00:00 2026-05-24T04:12:15+00:00

I am trying to build a table that summarizes how many times pages in

  • 0

I am trying to build a table that summarizes how many times pages in our Wiki references certain macros.

I have built a temporary table called @currentpages that with 55k rows that looks like this:

DECLARE @currentpages table(
ContentID       NUMERIC(19,0) NOT NULL PRIMARY KEY,
PageTitle       VARCHAR(255) NULL,
SpaceKey            VARCHAR(255) NULL,
OriginalAuthor  VARCHAR(255) NULL,
LastChangedBy   VARCHAR(255) NULL,
LastChangedDt   VARCHAR(10) NULL,
ContentBody         TEXT NULL); 

and another table that looks like this:

DECLARE @usage table(
SpaceKey        VARCHAR(255) NOT NULL PRIMARY KEY,
Macro1      NUMERIC(19,0) NULL,
Macro2      NUMERIC(19,0) NULL,
Macro3              NUMERIC(19,0) NULL);

(I’ve simplified the above; it actually has about 40 columns like Macro1, Macro2, etc.) I’m trying to get counts of how many references there are (in @currentpages) to these various macros.

I initialize the @usage table by creating a row for each possible SpaceKey value, zeroing out all of the Macro”x” counters:

INSERT INTO @usage (SpaceKey, Macro1, Macro2, Macro3)
SELECT S.spacekey, 0, 0, 0
FROM spaces     S
ORDER BY S.spacekey

Then I run the first of several UPDATE statements to identify each macro reference:

UPDATE @usage
SET U.AdvancedSearch = C.Counter
FROM @usage                 U
INNER JOIN (SELECT SpaceKey, COUNT(*) AS Counter 
    FROM @currentpages 
    WHERE ContentBody LIKE '%{search-%' GROUP BY SpaceKey) C 
        ON U.SpaceKey = C.SpaceKey

This appears to work fine, but it runs a very long time. Is there a more efficient way to do what I’m trying to do?

This database is on SQL Server 2005.

Many thanks,
Betsy

  • 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-24T04:12:16+00:00Added an answer on May 24, 2026 at 4:12 am

    Don’t make a column per macro, but make a table like this.

    DECLARE @macrotype table(
        type      int          NOT NULL PRIMARY KEY
      , MacroName varchar(100) NOT NULL 
      , mask      varchar(100) NOT NULL 
    )
    
    DECLARE @usage table(
      SpaceKey    VARCHAR(255) NOT NULL PRIMARY KEY,
    , MacroType   int NOT NULL 
    , MacroCount  int NOT NULL 
    )
    

    Once you have that you can populate it by running

      INSERT INTO @macrotype VALUES (1, 'search', '%{search-%')
                                  , (2, 'yadayada', '%{yadayada-%')
                                  , ...
    
      DECLARE @c int, @m varchar(100)
      DECLARE c CURSOR READ_ONLY FOR SELECT type, mask FROM @macrotype
      OPEN c
    
      FETCH NEXT FROM c INTO @t, @m
    
      WHILE @@FETCH_STATUS = 0 
        BEGIN
          INSERT INTO @usage (SpaceKey, MacroType, MacroCount)
          SELECT SpaceKey
               , @t
               , COUNT(*) AS Counter 
            FROM @currentpages
           WHERE ContentBody LIKE @m
        GROUP BY SpaceKey
        FETCH NEXT FROM c INTO @t, @m
      END
      CLOSE c
      DEALLOCATE c
    

    At the end of your story you can query the @usage table to make counts in whatever form you wish.

      SELECT MacroName, count = count(*)
        FROM @usage u
        JOIN @macrotype m
          ON u.MacroType - m.type
    GROUP BY MacroName
    

    On 55k records this will run for a few minutes.

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

Sidebar

Related Questions

I'm trying to build a table that is 5x6 dynamically using PHP. Right now
I'm trying to build a CSS-based table that will be populated with information coming
I'm trying to build some JavaScript in MooTools that fetches table row markup from
I am trying to build a 2008 SSRS report so that it displays table
I am trying to build a Linq to Entities query that joins a table
I'm trying to build a prototype application that consists of a table of data
I am trying to build a data table structure that will best support the
I have a database manager class where i build a table that has two
I'm trying to build a simple HTML table form that displays values from a
I am currently trying to build a navigation that is semi built by the

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.