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

  • Home
  • SEARCH
  • 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 8854273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:50:41+00:00 2026-06-14T13:50:41+00:00

Problem: Given a set of ~250000 integer user IDs, and about a terabyte of

  • 0

Problem: Given a set of ~250000 integer user IDs, and about a terabyte of JSON-formatted one-per-line records, load the records in which the user ID matches to a database.

Only about 1% of all the records will match the 250000 user IDs. Rather than JSON decode each record, which takes a long time, I am trying to use string matching to determine if the user ID is in the raw JSON; if it matches, then the JSON is decoded and the record checked and then inserted.

The problem is that matching one string of raw JSON against a set containing ~250k string entries is slow.

Here’s the code so far:

// get the list of integer user IDs
cur.execute('select distinct user_id from users')

// load them as text into a set
users = set([])
for result in cur.fetchall():
    users.add(str(result[0]))

// start working on f, the one-json-record-per-line text file
for line in f:
    scanned += 1
    if any(user in line for user in users):
        print "got one!"
        // decode json
        // check for correct decoded user ID match
        // do insert

I am approaching this the right way? What’s a faster method of matching these strings? At present, when looking for so many user IDs, this manages ~2 entries a second on a 3ghz machine (not so good). When the list of user IDs is very short, it manages ~200000 entries/second.

  • 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-14T13:50:42+00:00Added an answer on June 14, 2026 at 1:50 pm

    Aho-Corasick appears to be built for this purpose. There’s even a handy Python module for it (easy_install ahocorasick).

    import ahocorasick
    
    # build a match structure
    print 'init empty tree'
    tree = ahocorasick.KeywordTree()
    
    cur.execute('select distinct user_id from users')
    
    print 'add usernames to tree'
    for result in cur.fetchall():
       tree.add(str(result[0]))
    
    print 'build fsa'
    tree.make()
    
    for line in f:
         scanned += 1
         if tree.search(line) != None:
             print "got one!"
    

    This reaches closer to ~450 entries per second.

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

Sidebar

Related Questions

First the practical application that led me to the problem: Given a set of
Here is a real-world combinatorial optimization problem. We are given a large set of
Problem Given the following two tables, I'd like to select all Ids for Posts
I'm trying to solve this particular problem using c++. Problem: Given a set of
I recently had this problem on a test: given a set of points m
I need an algorithm for this problem: Given a set of n natural numbers
I have to solve the following optimization problem: Given a set of elements (E1,E2,E3,E4,E5,E6)
This is a spin-off of one of my earlier questions Problem statement: Given a
I am looking at solution for the set bit count problem (given a binary
I have a supervised learning problem where my algorithm will be given a set

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.