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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:59:13+00:00 2026-06-03T02:59:13+00:00

I’m trying to figure out a way to gather a dataset without having to

  • 0

I’m trying to figure out a way to gather a dataset without having to loop 700,000 mysql queries.

I have two tables

users with

id autoincrement, 
time timestamp, 
username varchar(200), 
email varchar(100), 
ip varchar(20)

and uniq_ip with

ip unique varchar(20), 
most_recent datetime, 
count (int)

users has 25 million rows and records the activity of users as they work on the site. uniq_ip has a list of all the IP numbers and how many times it’s listed in users (on trigger update).

At the moment, while daydream coding, I get a list of all the IPs from uniq_ip and loop them to get the most recent 2000 records for each of those IPs. As uniq_ip has 700,000 rows, this loop is really nasty, making 700,000 queries total, using

select * from users where ip = '$outerloopip' order by `time` desc limit 2000;

I’m trying to get a single query that will grab the most recent 2000 listings for each of the IPs. If 1.2.3.4 is listed 10,000 times, I just want the most recent 2000, based on the time field.

Any ideas how to do it in one query?

  • 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-03T02:59:15+00:00Added an answer on June 3, 2026 at 2:59 am

    I’m sorry about previous answer and re-read and applied updated query. I missed and thought you wanted only most recent 2000 IP addresses. Anyhow, this one does ALL IP addresses and limits the total records per IP to 2,000 entries with most recent at the top. I would ensure you have an index on

    (IP,TIME DESC)

    Then, try this query. The critical thing I missed to clarify. The HAVING clause is applied AFTER any group-by or order-by clause. So the data is pre-returned in proper order of IP address and date/time DESCENDING, then the @sql variables are applied. Once the record is qualified and READY to be added to the final result set, the HAVING clause is applied. At THAT moment, it looks at the sequence counter and says… if its greater than 2000, throw it out and move on to the next record.

    By my original query, it was saving everything, then cycling through a second time and kicking out those greater than 2000 which was probably why it was blowing your disk space away.

    select
          U.*,
          @LastSeq := IF( @LastIP = U.IP, @LastSeq +1, 1 ) as IPSequence,
          @LastIP := U.IP as carryForNextRecord
       from 
          ( select @LastIP := '', @LastSeq := 0 ) sqlvars,
          Users U
       order by
          U.IP,
          U.time DESC
       having 
          IPSequence <= 2000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and

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.