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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:11:32+00:00 2026-05-23T10:11:32+00:00

In my database I have a pair of tables ( tableA and tableB ),

  • 0

In my database I have a pair of tables (tableA and tableB), both of which have foreign keys referencing the same column ID in tableC. The SQL I was able to use was

SELECT *
FROM tableA
INNER JOIN tableB on tableA.ID=tableB.ID
WHERE tableB.year=2011

The query

SELECT *
FROM tableA
INNER JOIN tableB on tableA.ID=tableB.ID AND tableB.year=2011

Did the same thing

In Django I tried to do this with the code

subquery=tableB.objects.filter(year=2011).values_list(id, flat=True)
results=tableA.objects.filter(id__in=list(subquery))

as suggested in the documentation. I know this is a little different because it only returns columns in tableA, but I only use those anyway. The Django code seemed slow, I think partly because of the set membership test for every row of tableA, which is very large. Is there a way of making this faster in Django without using raw SQL (which I obviously can use if I need to)?

  • 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-23T10:11:33+00:00Added an answer on May 23, 2026 at 10:11 am

    It is slow because it is creating a query that probably looks something like

    SELECT * FROM TableA WHERE ID IN (SELECT ID FROM TableB WHERE year=2011)
    

    Like @Udi said, you’d be better off if you could “django”-ize the models by adding the Foreign keys some how. Otherwise, use the RawSQL. That’s what it’s there for.

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

Sidebar

Related Questions

I currently have multiple tables in my database which consist of the same 'basic
I have the following tables in my database that have a many-to-many relationship, which
I have 2 related database tables which in simplified form look like this Product(
I have a class which uses both ADO.NET and LINQ to access a pair
I have a database (NexusDB (supposedly SQL-92 compliant)) which contains and Item table, a
I heard that decision tables in relational database have been researched a lot in
I have database with many tables. In the first table, I have a field
In my database I have tables that define types for example Table: Publication Types
When I have tables in my database that have PK/FK relationships (int) and when
I have a pair of tables that have a one-to-one relationship. I have a

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.