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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:40:19+00:00 2026-06-13T03:40:19+00:00

I have a data logging app using sqlite3 to store different types of records

  • 0

I have a data logging app using sqlite3 to store different types of records that share a common header.

I put the header in one table and created separate tables for the details of each variant.

The rowid in the detail tables are the rowid of the header table. A header rowid only shows up in one of the details tables (for that variant).

I would like to fetch multiple types of records in a single query. That is, I want Sqlite to do an indexed search of the headers table to find a working set of records, and then use that set of ids to do a quick binary fetch of the variant details by rowid. So:

SELECT * FROM headers JOIN headers 
ON headers.id = variant1.id OR headers.id = variant2.id
WHERE some_header_condition

or

SELECT * FROM headers JOIN headers
ON headers.id IN (variant1.id, revariant2.id )
WHERE some_header_condition

This works, but when confronted with an OR term in the JOIN predicate, sqlite3 does a full table scan of the details tables variant1 and variant2 instead of just fetching the appropriate record by the rowid foreign key.

Something like:

0     0              2     SCAN TABLE variant2 (~5900 rows)
0     1              1     SCAN TABLE variant1 (~26588 rows)
0     2              0     SEARCH TABLE headers USING INTEGER PRIMARY KEY (rowid=?) (~2 rows)
0     0              0     EXECUTE LIST SUBQUERY 1

I can force binary searches by doing tricks like:

SELECT header.f1, variant1.f, NULL FROM header JOIN header.id = variant1.id ...
UNION ALL
SELECT header.f1, NULL, variant2.f FROM header JOIN header.id = variant2.id ...

But then the header table is accessed twice.

I could also imagine selecting the header.id(s) to a temporary table and using that to grab details using the ids IN it.

OR…I could just de-normalize the whole mess.

But all these workarounds are very inconvenient. So my question is, is there a nice JOIN query that can pick up these variants in one go with no table scans?

  • 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-13T03:40:20+00:00Added an answer on June 13, 2026 at 3:40 am

    Try using outer joins:

    SELECT *
    FROM headers
      LEFT JOIN variant1 ON headers.id = variant1.id
      LEFT JOIN variant2 ON headers.id = variant2.id
    WHERE headers...
    

    resulting in a plan like this:

    sele  order  from  deta
    ----  -----  ----  ----
    0     0      0     SEARCH TABLE headers USING INTEGER PRIMARY KEY (rowid>? AND rowid<?) (~31250 rows)
    0     1      1     SEARCH TABLE variant1 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
    0     2      2     SEARCH TABLE variant2 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little logging app (written in wxPython) that receives data from a
I have data from two different sources that I need to combine. Some data
I have a standard Rails app that uses a database, sqlite3 and the webrick
I have a app with a kind of rest api that I'm using to
I have a suite of App Engine tests that are called using nose .
The log tab is visible in DDMS and I have been logging data using
I'm working on a gym logging app using Core Data model. I'm trying to
I have a Bike computer app that logs data while the user is riding.
We have a project where data is written to a logging table. Now, when
I have a datalogging application (c#/.net) that logs data to a SQLite database. This

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.