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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:33:16+00:00 2026-05-25T10:33:16+00:00

I have a MySql query that take a very long time to run (about

  • 0

I have a MySql query that take a very long time to run (about 7 seconds). The problem seems to be with the OR in this part of the query: “(tblprivateitem.userid=?userid OR tblprivateitem.userid=1)”. If I skip the “OR tblprivateitem.userid=1” part it takes only 0.01 seconds. As I need that part I need to find a way to optimize this query. Any ideas?

QUERY:

SELECT 
    tbladdeditem.addeditemid,
    tblprivateitem.iitemid,
    tblprivateitem.itemid
FROM tbladdeditem 
INNER JOIN tblprivateitem 
    ON tblprivateitem.itemid=tbladdeditem.itemid 
        AND (tblprivateitem.userid=?userid OR tblprivateitem.userid=1)
WHERE tbladdeditem.userid=?userid

EXPLAIN:

id    select_type    table            type    possible_keys    key    key_len    ref                    rows    extra
1     SIMPLE         tbladdeditem     ref     userid           userid 4          const                  293     Using where
1     SIMPLE         tblprivateitem   ref     userid,itemid    itemid 4          tbladdeditem.itemid    2       Using where

TABLES:

tbladdeditem contains 1 100 000 rows:

CREATE TABLE `tbladdeditem` (
    `addeditemid` int(11) NOT NULL auto_increment,
    `itemid` int(11) default NULL,
    `userid` mediumint(9) default NULL,
    PRIMARY KEY  (`addeditemid`),
    KEY `userid` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

tblprivateitem contains 2 700 000 rows:

CREATE TABLE `tblprivateitem` (
    `privateitemid` int(11) NOT NULL auto_increment,
    `userid` mediumint(9) default '1',
    `itemid` int(10) NOT NULL,
    `iitemid` mediumint(9) default NULL,
    PRIMARY KEY  (`privateitemid`),
    KEY `userid` (`userid`),
    KEY `itemid` (`itemid`) //Changed this index to only use itemid instead
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • 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-25T10:33:17+00:00Added an answer on May 25, 2026 at 10:33 am

    UPDATE

    I made my queries and schema match your original question exactly, multi-column key and all. The only possible difference is that I populated each table with two million entries. My query (your query) runs in 0.15 seconds.

    delimiter $$
    set @userid = 6
    $$
    SELECT 
        tbladdeditem.addeditemid,    tblprivateitem.iitemid,    tblprivateitem.itemid
    FROM tbladdeditem 
    INNER JOIN tblprivateitem 
        ON tblprivateitem.itemid=tbladdeditem.itemid 
            AND (tblprivateitem.userid=@userid or tblprivateitem.userid = 1)
    WHERE tbladdeditem.userid=@userid
    

    I have the same explain that you do, and with my data, my query return over a thousand matches without any issue at all. Being completely at a loss, as you really shouldn’t be having these issues — is it possible you are running a very limiting version of MySQL? Are you running 64-bit? Plenty of memory?

    I had made the assumption that your query wasn’t performing well, and when mine was, assumed I had fixed you problem. So now I eat crow. I’ll post some of the avenues I went down. But I’m telling you, your query the way you posted it originally works just fine. I can only imagine your MySQL thrashing to the hard drive or something. Sorry I couldn’t be more help.

    PREVIOUS RESPONSE (Which is also an update)

    I broke down and recreated your problem in my own database. After trying independent indexes on userid and on itemid I was unable to get the query below a few seconds, so I set up very specific multi-column keys as directed by the query. Notice on tbladdeditem the multi-column query begins with itemid while on the tblprivateitem the columns are reversed:

    Here is the schema I used:

    CREATE TABLE `tbladdeditem` (
      `addeditemid` int(11) NOT NULL AUTO_INCREMENT,
      `itemid` int(11) NOT NULL,
      `userid` mediumint(9) NOT NULL,
      PRIMARY KEY (`addeditemid`),
      KEY `userid` (`userid`),
      KEY `i_and_u` (`itemid`,`userid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    CREATE TABLE `tblprivateitem` (
      `privateitemid` int(11) NOT NULL AUTO_INCREMENT,
      `userid` mediumint(9) NOT NULL DEFAULT '1',
      `itemid` int(10) NOT NULL,
      `iitemid` mediumint(9) NOT NULL,
      PRIMARY KEY (`privateitemid`),
      KEY `userid` (`userid`),
      KEY `itemid` (`itemid`),
      KEY `u_and_i` (`userid`,`itemid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    I filled each table with 2 million entries of random data. I made some assumptions:

    • userid varies from 1 to 2000
    • itemid varies between 1 and 10000

    This gives each user about a thousand entries in each table.

    Here are two versions of the query (I’m using workbench for my editor):

    Version 1 – do all the filtering on the join.

    Result: 0.016 seconds to return 1297 rows

    delimiter $$
    set @userid = 3
    $$
    SELECT 
        a.addeditemid,
        p.iitemid,
        p.itemid
    FROM tblprivateitem as p
    INNER JOIN tbladdeditem as a
        ON (p.userid in (1, @userid))
            AND p.itemid = a.itemid 
            AND a.userid = @userid
    $$
    

    Here’s the explain:

    EXPLAIN: 
    id select_type table type  key     ref  rows extra
    1  SIMPLE      p     range u_and_i      2150 Using where; Using index
    1  SIMPLE      a     ref   i_and_u      1    Using where; Using index
    

    Version 2 – filter up front

    Result: 0.015 seconds to return 1297 rows

    delimiter $$
    set @userid = 3
    $$
    SELECT 
        a.addeditemid,
        p.iitemid,
        p.itemid
    from 
      (select userid, itemid, iitemid from tblprivateitem 
          where userid in (1, @userid)) as p
      join tbladdeditem as a on p.userid = a.userid and a.itemid = p.itemid;
    where a.userid = @userid
    $$
    

    Here’s the explain:

    id select_type table      type  key     ref               rows extra
    1  PRIMARY     <derived2> ALL   null    null              2152
    1  PRIMARY     a          ref   i_and_u p.itemid,const    1    Using where; Using index
    2  DERIVED     p1         range u_and_i                   2150 Using where
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem with this mysql query. It take vmore as 1 day
i have use mysql database . my problem is that i run a query
I have a mysql db query that I'd like to take and condense into
I am using in C# MYsql .I have query that works if I run
I have this MySQL query that I'd like to optimize: SELECT min(`topic_id`) FROM `gallery_topics`
I have a MySQL query that looks like this: UPDATE `Table` SET `Column` =
I have a MySQL query that returns a result with a single column of
I have a MySQL query that I use to retrieve random rows from a
I have a mysql query that targets a single column in a single row
I'm running an MySQL query that returns results based on location. However I have

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.