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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:02:47+00:00 2026-05-11T19:02:47+00:00

I have a MySQL 5.1 InnoDB table ( customers ) with the following structure:

  • 0

I have a MySQL 5.1 InnoDB table (customers) with the following structure:

int         record_id (PRIMARY KEY)
int         user_id (ALLOW NULL)
varchar[11] postcode (ALLOW NULL)
varchar[30] region (ALLOW NULL)
..
..
..

There are roughly 7 million rows in the table. Currently, the table is being queried like this:

SELECT * FROM customers WHERE user_id IN (32343, 45676, 12345, 98765, 66010, ...

in the actual query, currently over 560 user_ids are in the IN clause. With several million records in the table, this query is slow!

There are secondary indexes on table, the first of which being on user_id itself, which I thought would help.

I know that SELECT(*) is A Bad Thing and this will be expanded to the full list of fields required. However, the fields not listed above are more ints and doubles. There are another 50 of those being returned, but they are needed for the report.

I imagine there’s a much better way to access the data for the user_ids, but I can’t think how to do it. My initial reaction is to remove the ALLOW NULL on the user_id field, as I understand NULL handling slows down queries?

I’d be very grateful if you could point me in a more efficient direction than using the IN ( ) method.

EDIT
Ran EXPLAIN, which said:

select_type = SIMPLE 
table = customers 
type = range 
possible_keys = userid_idx 
key = userid_idx 
key_len = 5 
ref = (NULL) 
rows = 637640 
Extra = Using where 

does that help?

  • 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-11T19:02:47+00:00Added an answer on May 11, 2026 at 7:02 pm

    First, check if there is an index on USER_ID and make sure it’s used.

    You can do it with running EXPLAIN.

    Second, create a temporary table and use it in a JOIN:

    CREATE TABLE temptable (user_id INT NOT NULL)
    
    SELECT  *
    FROM    temptable t
    JOIN    customers c
    ON      c.user_id = t.user_id
    

    Third, how may rows does your query return?

    If it returns almost all rows, then it just will be slow, since it will have to pump all these millions over the connection channel, to begin with.

    NULL will not slow your query down, since the IN condition only satisfies non-NULL values which are indexed.

    Update:

    The index is used, the plan is fine except that it returns more than half a million rows.

    Do you really need to put all these 638,000 rows into the report?

    Hope its not printed: bad for rainforests, global warming and stuff.

    Speaking seriously, you seem to need either aggregation or pagination on your query.

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

Sidebar

Related Questions

I have the following MySQL (version 5.1) table (InnoDB): Username varchar(50) Sequence int(11) FileType
I have a MySQL InnoDB table laid out like so: id (int), run_id (int),
my MySQL Server (version: 5.0.51a-3ubuntu5.8) I have the following InnoDB table: CREATE TABLE `kontostaende`
I have a MySQL InnoDB table, events , with 3 rows: event_id ---> int
I have a question about MySQL InnoDB. For example: I have the following table
I have the following InnoDB table: +-----------+-----------+------+-----+-------------------+----------------+ | Field | Type | Null |
I need to perform the following on MySQL/InnoDB. Assume I have a table consists
I have a MySQL(innodb) table 'items' with the following characteristics Large number of rows,
I have a MySQL-InnoDB table with 350,000+ rows, containing a couple of things like
I have a MySQL InnoDB table on a RedHat Enterprise Linux 4 server, 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.