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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:15:22+00:00 2026-06-17T08:15:22+00:00

I have one table that keeps track of links that each user has clicked

  • 0

I have one table that keeps track of links that each user has clicked and I have another table with links. Here is each table structure:

Links:
id | link | value | date_added

Clicked:
user_id | link_id | date_clicked

Right now this is the code I am using to make my search happen and it works, I just want to know if there is a more efficient way of doing it, since the clicked links table is going to get very large very fast.

$history_query = mysql_query("SELECT * FROM clicked_links WHERE user_id = '$id'") or die(mysql_error());
$history_array = array();
while ($h = mysql_fetch_array($history_query)) {
    $history_array[] = $h['link_id'];
}
$clicked = implode(',', $history_array);

$link_query = mysql_query("SELECT * FROM chip_links WHERE id NOT IN ($clicked) ORDER BY value DESC") or die(mysql_error());
while ($r = mysql_fetch_array($link_query)) {
    echo "<div id='claim{$r['id']}' style='text-align: center; font-weight: bold; font-size: 18px; float: left; width: 183px;'>
    <a href='{$r['link']}' id='{$r['id']}' class='collect' target='_blank'>
    Claim {$r['value']} points!
    </a>
    </div>";
}
  • 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-17T08:15:23+00:00Added an answer on June 17, 2026 at 8:15 am

    It will be more efficient to run a single query to get the resultset, rather than running separate queries.

    You don’t need to return all the link_id values, put them in an array, put the array into a string, and push that string into another query, and shuffle it back to the database… the database already has that.

    This query will return a resultset equivalent to your current $link_query, without the need for the $history_query or $history_array.

    SELECT l.id
         , l.link
         , l.value
      FROM chip_links l
     WHERE l.id NOT IN
           ( SELECT c.link_id
               FROM clicked_links c
              WHERE c.user_id = '$id'
                AND c.link_id IS NOT NULL
           )
     ORDER BY l.value DESC
    

    If you don’t have some sort of guarantee that link_id in the clicked_links table IS NOT NULL, you’ll want to include a link_id IS NOT NULL predicate in that subquery, because the query won’t return any rows if a link_id value is NULL. (This is a well-known and avoidable issue with NOT IN (subquery) constructs.

    It’s likely that MySQL will optimize that into a (hopefully more efficient but) equivalent NOT EXISTS correlated subquery, like this:

    SELECT l.id
         , l.link
         , l.value
      FROM chip_links l
     WHERE NOT EXISTS 
           ( SELECT 1 
               FROM clicked_links c
              WHERE c.user_id = '$id'
               AND c.link_id = l.id
           )
     ORDER BY l.value DESC
    

    For best performance, though, you probably want to use the anti-join pattern.

    The LEFT JOIN operation basically looks for matching rows, and the IS NOT NULL predicate will throw out rows that match, so what you get back is rows from chip_links where there is no “matching” row from clicked_links.

    The MySQL optimizer usually generates the most efficient plan with a query like this:

    SELECT l.id
         , l.link
         , l.value
      FROM chip_links l
      LEFT
      JOIN clicked_links c
        ON c.link_id = l.id
       AND c.user_id = '$id'
     WHERE c.link_id IS NULL
     ORDER
        BY l.value DESC
    

    For good performance on large sets, you’ll also likely want indexes

    ... ON clicked_links (user_id, link_id)
    
    ... ON chip_links (value, id, link)
    

    That should allow the query to be satisfied entirely from the indexes, and without the need for a sort operation. The EXPLAIN output will include “Using index”, and will not include “Using filesort”).

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

Sidebar

Related Questions

I have one table that has sales records and another table that has additional
My database has a table that keeps track of the department and user id.
I have one table that has two fields - ID1 and ID2 ID1 can
Here is my situation: I have one table that contains a list of drugs
Lets say I have one table called REVIEWS This table has Reviews that customers
I was making one table that have two foreign key to another tables. But
I have a table that has two datetime columns (one for start time and
I have a simple app that has a list of clients in one table
I have a table called Jobs that keeps track of jobs and their next
I have an sqlite table in my game that keeps track of scores. How

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.