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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:01:37+00:00 2026-05-16T12:01:37+00:00

I need to determine if user has already visited a page, for tracking unique

  • 0

I need to determine if user has already visited a page, for tracking unique page views.

I have already implemented some HTTP header cache, but now I need to optimize the SQL queries.

The visit is unique, when:

  • pair: page_id + user_id is found in the visit table
  • or pair: page_id + session_id is found
  • or: page_id + [ip + useragent] – (this is a topic for another discussion, whether it should be only ip or ip+useragent)

So I have a table tracking user visits:

visit:
    page_id
    user_id
    session_id
    useragent
    ip
    created_at
    updated_at

Now on each user visit (which does not hit cache) I will update a row if it exists. If there are any affected rows, I will insert new visit to the table.

This are one or two queries (assuming the cache will work, mostly two queries), but the number of rows is limited somehow. Maybe it would be better to store all the visits and then clean up the database within e.g. a month?

The questions are:

  • how should be the visit table constructed (keys, indexes, relations to user and page_views table). Some of the important fields may be null (e.g. user_id), what about indexes then? Do I need a multi column primary key?
  • which would be the fastest sql query to find the unique user?
  • is this sane approach?

I use PostgreSQL and PDO (Doctrine ORM).
All my sessions are stored in the same DB.

  • 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-16T12:01:38+00:00Added an answer on May 16, 2026 at 12:01 pm

    Personally I would not put this in the request-response path. I would log the the raw data in a table (or push it on a queue) and let a background task/thread/cron job deal with that.

    The queue (or the message passing table) should then just contain pageid, userip, sessionid, useragen,ip.

    Absolute timings are less important now as long as the background task can keep up. since a single thread will now do the heavy lifting it will not create conflicting locks when updating the unique pageviews tables.

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

Sidebar

Related Questions

Visual Studio c# Xml File Permissions I need to find a way to determine
I need to find a way that the target of a header() redirect can
I need direct-to-printer functionality for my website, with the ability to distinguish a physical
Okay, so I've done some looking around and I see how you are SUPPOSED
I'm not sure how to title the question I have so I can't be
I have a List of 2D points. What's an efficient way of iterating through
I have a system where, essentially, users are able to put in 3 different
(This algorithm is for an iPhone app I am working on, if that helps
I'm developing a tool which shows data from a database in a hierarchical manner.
If someone could just talk to me and maybe point me in a direction.

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.