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;
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.
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
useridand onitemidI 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 ontbladdeditemthe multi-column query begins withitemidwhile on thetblprivateitemthe columns are reversed:Here is the schema I used:
I filled each table with 2 million entries of random data. I made some assumptions:
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
Here’s the explain:
Version 2 – filter up front
Result: 0.015 seconds to return 1297 rows
Here’s the explain: