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

  • Home
  • SEARCH
  • 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 9180273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:00:26+00:00 2026-06-17T18:00:26+00:00

I use databases all of the time within various solutions, but never really design

  • 0

I use databases all of the time within various solutions, but never really design them. Therefore, I’m pretty new to the finer points of SQL. I have a database that has many tables, but 2 main ones:
Item – references lots of other tables for data that doesn’t change with revisions and
Entry – references lots of other tables for data that does change with revisions
Each item will therefore have 1 or more revision (with all associated data).

I want to select all data for all revisions (and consequently items) with a view. I’ve structured a single query as follows:

CREATE VIEW Single_Query
AS
SELECT  i.REF_CODE AS REF
gp.GROUP_NAME AS Group
v.VERSION_NUMBER AS Version
GROUP_CONCAT(DISTINCT(cy.COUNTRY_DESCRIPTION) SEPARATOR ', ') AS Country
ey.PUB_DATE AS Published
GROUP_CONCAT(DISTINCT(ct.CONTRIBUTOR_NAME) SEPARATOR ', ') AS Author
i.ISBN_CODE AS ISBN
GROUP_CONCAT(DISTINCT CONCAT(cn.COMPONENT_NUMBER, _utf8', ',cn.COMPONENT_DESCRIPTION) SEPARATOR '; ') AS Contents
ey.NOTES AS Notes
i.PRICE AS Price
cl.COLOUR_DESCRIPTION AS Colour
FROM entry AS ey
JOIN item AS i ON ey.ITEM_ID = i.ITEM_ID
JOIN group AS gp ON i.GROUP_ID = gp.GROUP_ID
JOIN version AS v ON ey.VERSION_ID = v.VERSION_ID
JOIN link_country_item AS lci ON i.ITEM_ID = lci.ITEM_ID
JOIN country AS cy ON lci.COUNTRY_ID = cy.COUNTRY_ID
LEFT JOIN link_entry_contributor AS lec ON ey.ENTRY_ID = lec.ENTRY_ID
LEFT JOIN contributor AS ct ON lec.CONTRIBUTOR_ID = ct.CONTRIBUTOR_ID
JOIN contents AS cn ON i.ITEM_ID = cn.ITEM_ID
JOIN colour AS cl ON ey.COLOUR_ID = cl.COLOUR_ID
GROUP BY REF_CODE, VERSION_NUMBER

This takes about 29 seconds to complete, with only a few entries! However, if I run a script to create temporary tables for the item data (and all referenced data) and the entry data (and all referenced data), then select the whole lot from the 2 temporary tables with a single JOIN on ITEM_ID, then it takes less than half a second to complete.

Note that there are lots of other referenced tables (I’ve omitted them for clarity).

If it helps, the item table references the group, setting (through the many-many link_country_item table) and contents tables.

The entry table references the version, contributor (through the many-many link_entry_contributor table) and colour tables.

The problem is that I don’t really want to use temporary tables all of the time as I can’t use them in a view (or at least I haven’t found out how). Is there a more elegant way of splitting the SQL in a View so that it runs faster in this scenario?

Many Thanks in advance

Iain

  • 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-17T18:00:28+00:00Added an answer on June 17, 2026 at 6:00 pm

    I’ve slightly restructured the querying order, and see your comment about the REF_CODE. So you are correct, the original index answer wouldn’t cover for you. However, with the item table in the primary position, if that has an index on (REF_CODE, ITEM_ID) should help the performance, as you are STARTING with the table, and a corresponding index for your GROUP BY clause.

    CREATE VIEW Single_Query
    AS
    SELECT STRAIGHT_JOIN
          i.REF_CODE AS REF
          gp.GROUP_NAME AS Group
          v.VERSION_NUMBER AS Version
          GROUP_CONCAT(DISTINCT(cy.COUNTRY_DESCRIPTION) SEPARATOR ', ') AS Country
          ey.PUB_DATE AS Published
          GROUP_CONCAT(DISTINCT(ct.CONTRIBUTOR_NAME) SEPARATOR ', ') AS Author
          i.ISBN_CODE AS ISBN
          GROUP_CONCAT(DISTINCT CONCAT(cn.COMPONENT_NUMBER, _utf8', ',cn.COMPONENT_DESCRIPTION) SEPARATOR '; ') AS Contents
          ey.NOTES AS Notes
          i.PRICE AS Price
          cl.COLOUR_DESCRIPTION AS Colour
       FROM 
          item AS i 
             JOIN entry ey
                ON i.ITEM_ID = ey.ITEM_ID
                JOIN version AS v  
                   ON ey.VERSION_ID = v.VERSION_ID
                JOIN colour AS cl 
                   ON ey.COLOUR_ID = cl.COLOUR_ID
                LEFT JOIN link_entry_contributor AS lec 
                   ON ey.ENTRY_ID = lec.ENTRY_ID
                   LEFT JOIN contributor AS ct 
                      ON lec.CONTRIBUTOR_ID = ct.CONTRIBUTOR_ID
             JOIN group AS gp 
                ON i.GROUP_ID = gp.GROUP_ID
             JOIN link_country_item AS lci 
                ON i.ITEM_ID = lci.ITEM_ID
                JOIN country AS cy 
                   ON lci.COUNTRY_ID = cy.COUNTRY_ID
             JOIN contents AS cn 
                ON i.ITEM_ID = cn.ITEM_ID
       GROUP BY 
          REF_CODE, 
          VERSION_NUMBER
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use schemas in my databases, but other than the security benefits and the
At a tutorial on using SQLite databases. Within this tutorial, it suggested to use
I use the downloadable Geonames database for all countries, states, counties and cities in
In my app I use a static database to store all counties and census
I'm trying to use my own database in an Android application. Most all the
i want to update all records in my database(mongodb),I tried to use command below
What is the difference between object-oriented and document databases? I didn't use object-oriented databases,
Redis is conceptually different from traditional SQL databases that I use, and I'm trying
I'm using soci for making my app interact with databases and currently I use
I'm looking how to perform date/time math within an HQL query. Specifically, how do

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.