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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:56:03+00:00 2026-05-31T16:56:03+00:00

I have a large download site, an i have a code that updates each

  • 0

I have a large download site, an i have a code that updates each download to keep track of how many unique person download each file and how many total person view the file.

However, the code works fine, but when I have alot of traffic, the code slows down the site, an allow mysql server to use alot of resource. Is this code optimizable ?

I would appreciate someone to do it for me, whether by inner joining them etc. Thanks

<?php
    #Gathers the client info

    $agent = $_SERVER['HTTP_USER_AGENT'];
    $brows = explode(" ",$agent);
    $ubr = "$brows[0]";
    $exptime = time() + 200; 
    $var = time();
    $uip = user_ip();

    $del = mysql_query("DELETE FROM log_hits WHERE exptime < '".$var."'");
    $hits = mysql_fetch_array(mysql_query("SELECT * FROM user_downloads WHERE id='".$file_id."'"));

    #Process unique download count
    $u_check = DB::FetchArray(DB::Query("SELECT COUNT(*) FROM log_hits where browser='".$ubr."' and uip='".$uip."' and file_id='".$file_id."'"));
    if($file_check[0]=="0")
    {
        $res3 = DB::Query("INSERT INTO log_hits SET browser='".$ubr."', uip='".$uip."', exptime='".$exptime."', file_id='".$file_id."'");
        $unique = $hits[day_unique] + 1;
    }else
    {
        $unique = $hits[day_unique];
    }

    #update regular hits to the file, 
    $week = $hits[weekly] + 1;
    $hour = $hits[this_hour] + 1;
    $todayx = $hits[today_hits] + 1;
    $total = $hits[total] + 1;
    $month = $hits[month] + 1;

    $res3 = DB::Query("UPDATE `user_downloads` SET `day_unique`='{$unique}', `weekly`='{$week}', `this_hour`='{$hour}', `month`='{$month}', `total`='{$total}' , `today_hits`='{$todayx}' WHERE `id`='".$file_id."'") or die(mysql_error());
?>
  • 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-31T16:56:04+00:00Added an answer on May 31, 2026 at 4:56 pm

    You have 5 queries just to update a more or less simple counter. In my opionion this is way to much.

    I don’t know about your amount of concurrent users, but I would suggest something like the following approach:

    • Just keep a log of the downloads similar to your current log_hits table.
    • Every hour or so run a cronjob script to generate the statistics you need and purge the log-table of old entries (or move them somewhere else, depending whether you still need them for something else).

    This would reduce your queries for each download to one. On the other hand the statistics cronjob would only use a single (though more expensive) query to do the work of a multitude of (cheaper) queries each time. Overall this should help to improve the response times of your page.

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

Sidebar

Related Questions

I have a large XML file (many MBs) that I cannot afford to download
I have a microcontroller that must download a large file from a PC serial
Many users of my site have reported problems downloading a large file (80 MB).
I have multiple winforms applications that download large sets of reference data on startup.
I have a large file I want to download from a server I have
I have a large (50 meg) download that I want to load to the
I am using the code in this question NSURLConnection download large file (>40MB) to
I developed an app that have to download very large files, 100 MB average.
I currently have a download site for my school that is based in .net.
I have a servlet that allows users to download (potentially large) zip files from

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.