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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:17:19+00:00 2026-06-16T01:17:19+00:00

I have a file-sharing site where my users are interested in clicks on their

  • 0

I have a file-sharing site where my users are interested in clicks on their files. Each click is stored as a new row in the clicks table.

Usually, they want to know how many clicks they got on a certain date range:

$statement = $db->prepare("SELECT COUNT(DISTINCT ip) FROM clicks WHERE user_id=? AND time BETWEEN ? AND ?");
$statement->execute(array($user_id, $from_date, $to_date));

Additionally, they can also see the number of clicks for a certain file:

$statement = $db->prepare("SELECT COUNT(DISTINCT ip) FROM clicks WHERE file_id=? AND time BETWEEN ? AND ?");
$statement->execute(array($file_id, $from_date, $to_date));

The problem with these queries, is that user_id and file_id are not keys for this table (they are not unique). Instead, a simple ‘id’ column is the primary key but it never plays into any of the queries.

I have been researching clustered indexes but I cannot figure out how to implement it in this case.

As the clicks table is growing pretty large (5-6 million rows) these queries are taking longer (and I plan for this table to get a lot bigger). I read that partitioning might be what I need to do?

Do I need to make a clustered key, partition the table, or both?

For reference, the clicks structure:

id time user_id ip file_id

  • 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-16T01:17:20+00:00Added an answer on June 16, 2026 at 1:17 am

    You don’t need to change the clustered index.

    I would suggest creating these indexes:

    ALTER TABLE clicks ADD INDEX (file_id, time, ip),
                       ADD INDEX (user_id, time, ip);
    

    By including the ip in the index definition, each queries should be able to get all the information needed from the index structure itself. This is called a covering index. Then the query won’t need to touch the table at all, so it doesn’t matter which columns comprise the clustered index for the table.

    If you use EXPLAIN to analyze the query, you should see “Using index” in the Extra field, and this indicates the query is getting the benefit of a covering index.

    I don’t think partitioning will help in this case, because MySQL partitioning requires that the partition columns must be part of any primary key / unique key of the table.

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

Sidebar

Related Questions

I have a simple file sharing application. Users are sending data among each other
I have file sharing via iTunes working in my application (reference What's New in
I wish to build a file sharing site and store the files in S3.
I have a music sharing site and realize that the files on my site
When creating an uploading sharing site, is it possible that users may upload their
I am creating an app for uploading and sharing files between users. I have
I have files that I want to delete. Connection can be from file sharing,
I have to make a peer to peer file sharing app on android platform
OK I have so much questions regarding my file sharing application that I don't
I have Windows File sharing enabled on an OS X 10.4 computer. It's accessible

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.