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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:38:26+00:00 2026-06-14T07:38:26+00:00

UPDATE: I am using the sql query shown in my question in production, but

  • 0

UPDATE: I am using the sql query shown in my question in production, but you are welcome to read the entire thread if you want to see an alternate approach to this, using sql with a UNION

I’ve experimented and made a result set to be used in a content search, but I want to make sure it’s performance is the best it can be.

I have a table named SECTIONS which holds 2 levels of sections, i.e. level 1 (a section) and level 2 (a subsection), in an Adjacency List model

SECTIONS: id, parent_id, name

I query that table twice to get columns in the arrangement

sec_id, sec_name, subsec_id, subsec_name

( this is so I can create uri links like /section_id/subsection_id )

Now I join a separate table named PAGES where a page can be related to a section or a subsection (both not both) through the field section_id

-- columns to return
SELECT
s.id as section_id,
s.name as section_name,
ss.id as subsection_id,
ss.parent_id as subsection_parent_id,
ss.name as subsection_name,
p.section_id as page_section_id,
p.name as page_name

-- join SECTIONS into Sections and SubSections
FROM 
( select id, name from sections where parent_id=0 ) as s

LEFT JOIN
( select id, parent_id, name from sections where parent_id!=0 ) as ss

ON
ss.parent_id = s.id

-- now join to PAGES table
JOIN 
( select id, section_id, name from pages where active=1 ) as p

ON
(
p.section_id = s.id
OR
p.section_id = ss.id 
)
-- need to use GROUP BY to eliminate duplicate pages
GROUP BY p.id

I get duplicate pages in the result set, so I use GROUP BY pages.id to remove the duplicates, but it degrades performance a little.

Can you suggest a better way to eliminate duplicates?

I’ve thought of creating a column in the SECTIONS join that holds the Section ID OR the Subsection ID (depending on the type of row – section or subsection), and then use that to relate to the PAGES section_id, so there would not be duplicate rows, but I can’t figure out how to do it.

Thanks

  • 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-06-14T07:38:27+00:00Added an answer on June 14, 2026 at 7:38 am

    This is gonna be long 🙁

    Note that I didn’t use this approach in the end because it’s performance was worse the my original attempt using GROUP BY

    I had to modify the data table design for the PAGES table to include a new column to hold the id of the subsection that the page belonged to, so now the PAGES table has columns that indicate the section it belongs to, and the subsection also. That structure modification was only for testing and I did not use it in the final version.

    Here is the query I created using the concept of a UNION between 2 queries.

    SELECT
    * 
    FROM
      pages AS p
    JOIN
    -- create derived table of sections and subsections
      ( -- separate query to get sections (parent id = 0 )
        SELECT 
            s.id AS page_sec_id,
            s.id AS sec_id,
            s.name AS sec_name,
            NULL AS subsec_id,
            NULL AS subsec_name,
            s.parent_id AS parent_id
        FROM
            sections AS s
        WHERE
            s.parent_id = 0
       UNION
        -- separate query to get subsection (parent id != 0)
        SELECT
            ss.id AS page_sec_id,
            ss.parent_id AS sec_id,
            -- need to get section name, so had to use weird subquery
            (SELECT name FROM sections WHERE parent_id =0 AND id = ss.parent_id) AS sec_name,
            ss.id AS subsec_id,
            ss.name AS subsec_name,
            ss.parent_id AS parent_id
        FROM
            sections AS ss
        WHERE
            ss.parent_id != 0
       )  AS sss
    
    ON
        -- specify how PAGES table is joined to this derived table of sections and subsections
    
        -- pages linked to sections only
            ( p.section_id = sss.sec_id AND p.subsection_id = 0 AND sss.parent_id = 0)
            OR
        -- pages linked to subsections only
            ( p.section_id = sss.sec_id AND p.subsection_id = sss.subsec_id )
    

    This UNION query used
    0.0388 seconds
    for 5 rows of Pages and 4 rows of sections/subsections,
    versus the original query which used
    0.0017 seconds,
    so I stuck with the original as shown above in my question. BTW in my dev environment mysql is running on a P3 Katmai 450 Mhz 256 RAM to force me to write efficient queries 🙂

    Thanks for reading, if you have additional thoughts & comments please add them.

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

Sidebar

Related Questions

I'm trying to flip a bit field in SQL Server using an update query,
What are the sql exceptions will thrown by using the following function: Read Update
**Update: Title should have read: Sql Query - Unique item with latest entry Hi
****Update:** using the Rank() over partition syntax available in MS SQL Server 2005 does
I am using SQL Server 2008 R2, I have a script to update the
I am using SQL Server 2000. When I try to update a column of
How can I update an entity effectively in hibernate just as by using SQL.
using Visual.Web.Developer.2010.Express; using SQL.Server.Management.Studio.2008.R2; What I'm ultimately trying to do is update a sql
I'm trying to update an NText field in SQL 2000 using Classic ASP. Here
I am using the following pattern to update my SQL Server records using Linq

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.