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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:35:04+00:00 2026-06-16T16:35:04+00:00

and happy new year! I have a model which will hold some hundreds of

  • 0

and happy new year!

I have a model which will hold some hundreds of thousands of records. The model looks like this:

class Transaction(models.Model):
    user = models.ForeignKey(User)
    client = models.ForeignKey(Client)
    amount = models.FloatField()

I want to know all clients a certain user is dealing with. To get the unique client ids, I could use the Django ORM alone:

Transaction.objects.filter(user=the_user).distinct('client_id').values_list('client_id', flat=True)

or do the following:

set(Transaction.objects.filter(user=the_user).values_list('client_id', flat=True))

Both will produce the same result. But which one will be quicker given the large set of records? I know distinct is a relatively slow operation on databases, but how it compares to python’s set()?

Finally, if it goes down to databases, my options are MySql and PostgreSql for production. Would there be any difference between the two of them for this specific operation?

  • 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-16T16:35:05+00:00Added an answer on June 16, 2026 at 4:35 pm

    I usually use the orm functions, it’s more readable and it runs on database level, so is your RDBMS who actually computes the distinct values, and you have your result in a single step.

    You can accomplish the same with a python set, but you need to first bring all your data set, and then apply a set(). So you have to perform a two step operation to do the same.

    In your first case (orm) you only have the I/O overhead, and in the second case, you have the I/O overhead + a function call, so I would go with the ORM’s distinct.

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

Sidebar

Related Questions

Hello all and a Happy New Year SITUATION: I have some tables in MySQL
Happy New Year Everyone! I have this existing php code I did when I
Happy new year :) I have a Chrome extension which is a popup that
First of all, Happy New Year to anybody out there reading this! I have
Happy New Year SO users! I have an issue that I hope someone can
Happy new year! I'm facing a issue with class-path of web application. I have
Happy New Year :-) I have the following JSON object: { \OutcomeSummaryID\:105, \DeliveryDetailsID\:9, \AttemptedDeliveryModesIDList\:[1,5],
first of all an Happy New Year to all :) I have found that
First of all-many season-greetings and a happy new year to all of you! Have
I write news for my website and format it like this: [h1]News[h1] [red]Happy New

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.