Quantcast
Channel: MySQL Forums - Performance
Viewing all articles
Browse latest Browse all 1205

Slow LEFT JOIN, can't figure out why (2 replies)

$
0
0
Hello,

I have a query that it taking around 20 seconds to run, but I'm having trouble figuring out why:

SELECT * FROM friends AS Friend LEFT JOIN users AS User ON (User.id = Friend.requester_id AND Friend.requester_id != 304) OR (User.id = Friend.requested_id AND Friend.requested_id != 304) WHERE (Friend.requester_id = 304 OR Friend.requested_id = 304) AND Friend.approved = 1

Here's the output of explain:

+----+-------------+--------+-------+------------------------------------------------------------------+----------+---------+-------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+------------------------------------------------------------------+----------+---------+-------+-------+-------------+
| 1 | SIMPLE | Friend | ref | requester_id,requester_id_2,requester_id_3,approved,requested_id | approved | 1 | const | 35501 | Using where |
| 1 | SIMPLE | User | range | PRIMARY | PRIMARY | 4 | NULL | 18980 | Using where |
+----+-------------+--------+-------+------------------------------------------------------------------+----------+---------+-------+-------+-------------+

I thought that looked fairly good - indexes being used, and as I understand it, ref and range are 'good' join types.

Here are my table definitions:

CREATE TABLE `friends` (
`id` int(7) unsigned NOT NULL auto_increment,
`requester_id` int(5) unsigned NOT NULL default '0',
`requested_id` int(5) unsigned NOT NULL default '0',
`approved` tinyint(1) unsigned NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `requester_id` (`requester_id`,`requested_id`,`approved`),
KEY `requester_id_2` (`requester_id`,`requested_id`,`approved`),
KEY `requester_id_3` (`requester_id`,`requested_id`,`approved`),
KEY `approved` (`approved`),
KEY `requested_id` (`requested_id`)
) ENGINE=InnoDB AUTO_INCREMENT=73790 DEFAULT CHARSET=utf8


CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`login` varchar(255) default NULL,
`slug` varchar(50) NOT NULL,
`password` varchar(255) default NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
`lastseen` datetime default NULL,
`user_group` varchar(255) NOT NULL,
`confirm_code` varchar(32) default NULL,
`master` tinyint(1) NOT NULL default '0',
`is_employer` tinyint(1) unsigned NOT NULL default '0',
`credits` int(11) unsigned NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=42071 DEFAULT CHARSET=utf8

Any ideas please?

Viewing all articles
Browse latest Browse all 1205

Latest Images

Trending Articles



Latest Images