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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:20:44+00:00 2026-05-20T05:20:44+00:00

I have 2 tables, tracklist and track , where tracklist has many tracks .

  • 0

I have 2 tables, tracklist and track, where tracklist has many tracks. At some points, I will receive user input which refers to a list of tracks, and I need to either create that tracklist, or return an existing tracklist (this is because tracklists are meant to be entirely transparent to users).

My naive solution to this was to find all tracklists with n tracks, and join track against tracklist n times, checking each join against the user input data. For example, with 2 tracks:

SELECT tracklist.id FROM tracklist
  JOIN track t1 ON tracklist.id = t1.tracklist
  JOIN track_name tn1 ON t1.name = tn1.id
  JOIN track t2 ON tracklist.id = t2.tracklist
  JOIN track_name tn2 ON t2.name = tn2.id
 WHERE tracklist.track_count = '20'
   AND (t1.position = 1 AND tn1.name = 'Pancakes' AND t1.artist_credit = '42' AND t1.recording = 1)
   AND (t2.position = 2 AND tn2.name = 'Waffles' AND t2.artist_credit = '9001' AND t2.recording = 2)

However, this really doesn’t scale well to large tracklists. My very rudimentary timing shows this can take >500ms for 10 track tracklists, and ~7s for tracklists with 100 tracks. While the latter is an edge case, whatever algorithm I use needs to be able to scale at least up to this.

I’m stuck on other solutions however. The only other thing I can think of is to select all tracklists with n tracks, and all their tracks, and then do the comparison in application code. However, I’d really like to keep this on the database server if I can.


Here is the schema I am working with:

CREATE TABLE track
(
    id                  SERIAL,
    recording           INTEGER NOT NULL, -- references recording.id
    tracklist           INTEGER NOT NULL, -- references tracklist.id
    position            INTEGER NOT NULL,
    name                INTEGER NOT NULL, -- references track_name.id
    artist_credit       INTEGER NOT NULL, -- references artist_credit.id
    length              INTEGER CHECK (length IS NULL OR length > 0),
    edits_pending       INTEGER NOT NULL DEFAULT 0,
    last_updated        TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

CREATE TABLE track_name (
    id                  SERIAL,
    name                VARCHAR NOT NULL
);

CREATE TABLE tracklist
(
    id                  SERIAL,
    track_count         INTEGER NOT NULL DEFAULT 0,
    last_updated        TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

Any suggestions?

  • 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-20T05:20:45+00:00Added an answer on May 20, 2026 at 5:20 am
      SELECT DISTINCT tracklist
        FROM track t0
        WHERE 
          (SELECT COUNT(DISTINCT tracklist)
            FROM track t1
              WHERE 
                (
                  (t1.id='test1.id')
                  OR 
                  (t1.id='test2.id')
    
                  ......
                  OR 
                  (t1.id='testn.id')
                )
          = 1);
    
      -- This is OK if you have the track ids for this query.
      -- If you do not then you need to replace each of the t1.id='testm.id' statements
      -- with:
      --      t1.recording='testm.recording' AND
      --      t1.tracklist='testm.tracklist' AND
      --      t1.position='testm.position' AND
      --      t1.name='testm.name' AND
      --      t1.artist_credit='testm.artist_credit' AND
      --      t1.length='testm.length' AND
      --      t1.edits_pending='testm.edits_pending' AND
      --      t1.last_updated='testm.last_updated'
    

    As I may not have the syntax exactly correct, and have had no opportunity to test it, a written description of what I am trying to achieve is next:

    I build up a query returning the list of tracks that you have. Once I have built this query I am checking whether the tracklists for these tracks are all the same. If they are, ie there is only one tracklist in the query, then this is the tracklist you require. If there are no tracklists in the query, or there is more than one, then the set of tracks you have do not correspond to any single existing tracklist, so you need to create a new tracklist. This query does not deal with the actual creation, if it proves necessary. I am not sure how it will deal with degenerate cases – there are no tracks at all in the query; or there are no tracklists listed for any of the tracks.

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

Sidebar

Related Questions

I have tables: user, comment and post, say: A user can have many posts
I have two tables : tracklist(trackid artist event date) and radded(trackid, user) with relation
I have tables Products, Sales and ProductsInStores. ProductsInStores table keeps track of how many
hi i have tables like this: Persons: person_id, name then i have many langauge
I have tables with latin1_swedish_ci column. I've done tests and inserted into them some
I have tables A, B, C, where A represents items which can have zero
I have tables item and store (it's a store management system). item table has
I have tables Users, Widgets and Layouts. Users have many-to-many relationship with Widgets via
I have tables of data samples, with a timestamp and some data. Each table
I have tables like Customer , Purchase etc which sometimes have associated documents with

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.