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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:10:03+00:00 2026-05-13T09:10:03+00:00

From Objective-C i’m used to Reference Counting / Retain Counting (same thing). I love

  • 0

From Objective-C i’m used to Reference Counting / Retain Counting (same thing). I love this concept, and it’s plain simple.

So I thought, why not apply this to a database? The problem is:

Imagine these tables: users, photos. A user can own a photo, a user can like a photo of himself or someone else, and a user can recommend a photo. In any case there’s a reference created to the photo.

Now the big problem is: Lets say I want to get rid of any photo that belongs to nobody anymore. So a photo that is not owned by any user, is not liked by any user, and is not recommended by any user, has to be deleted. I have no idea how to do that with the plain database functionality 😉 well, maybe some hundred joins? I don’t know. Database noob.

So the idea I’d like to verify from DB experts: Imagine I add a reference_count field to every table which can be referenced (requires some thought upon setup, of course). As soon as I define a relationship from any table_a to any table_b, and table_a is supposed to be the parent or master (strong reference), I increase the reference_count of the linked row by 1.

Let’s say 150 users like a photo and one owns it. So it’s reference_count is 151. Now the owner gives it up but 150 others still scream “oh nooo, no no no!! it’s ours! we like it so muuucch!!”. The system looks once every midnight through all these tables and attempts to delete every row which has a reference count of 0. So after some days, the users get bored of it and remove their “I like it” flag. Every time this happens, the reference_count gets reduced by 1. At the end, it’s 0, and the next midnight when everyone is asleep, a cron job deletes it.

Of course, it must be configurable, because it must not always be the case that the photo has to get deleted after nobody references that anymore. I would solve this by setting it’s reference_count initially to 1, not 0.

This means of course a lot of additional db calls, I guess. So what do you think? Or are there better solutions for that?

  • 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-13T09:10:03+00:00Added an answer on May 13, 2026 at 9:10 am

    You can do just the following:

    DELETE
    FROM    photos
    WHERE   id NOT IN
            (
            SELECT  photo_id
            FROM    photos_users_like
            )
            AND id NOT IN (
            SELECT  photo_id
            FROM    photos_users_made
            )
            AND id NOT IN (
            SELECT  photo_id
            FROM    photos_users_recommended
            )
    

    If you index your photo_ids in all tables, the NOT INs will be optimized by MySQL so that the predicates will return FALSE when the engine finds but a single matching record in the corresponding tables and there will be no need in reference counts.

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

Sidebar

Ask A Question

Stats

  • Questions 460k
  • Answers 460k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer http://moazzam-khan.com/blog/?p=490 image uploading + parameters only parameters + text very… May 15, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer You can't. I'm not sure if there even is a… May 15, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer Everyone who did the test concluded that using single quotes… May 15, 2026 at 11:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.