From: Ananda Kumar Date: August 17 2010 12:21pm Subject: Re: Extremly slow Join with 'OR' List-Archive: http://lists.mysql.com/mysql/222548 Message-Id: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary=001485f78c0a263070048e03fecf --001485f78c0a263070048e03fecf Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable use UNION ALL ..instead of UNION for better performance... On Tue, Aug 17, 2010 at 5:01 PM, Johan De Meersman wrot= e: > You may want to split of your or conditions into a separate query, and us= e > UNION. > > On Tue, Aug 17, 2010 at 11:22 AM, =D0=92=D0=BB=D0=B0=D0=B4 =D0=A0 wrote: > > > The main problem - if add in Join on `OR`-condition, select become > > VERY slow. I realy > > have to use this condition. > > > > -- -------------------------------------------------------- > > -- > > -- `tree_data` > > -- > > > > CREATE TABLE IF NOT EXISTS `tree_data` ( > > `id` int(11) NOT NULL AUTO_INCREMENT, > > `pid` int(11) NOT NULL, > > PRIMARY KEY (`id`), > > ) ENGINE=3DMyISAM DEFAULT CHARSET=3Dcp1251 AUTO_INCREMENT=3D869 ; > > > > testiong on 800 rows > > > > Simple tree join query > > First QUERY is: > > > > SELECT > > `l0`.`id` AS 'l0id', > > `l0`.`pid` AS `l0pid` , > > `l1`.`id` AS 'l1id', > > `l1`.`pid` AS `l1pid` , > > `l2`.`id` AS 'l2id', > > `l2`.`pid` AS `l2pid` , > > `l3`.`id` AS 'l3id', > > `l3`.`pid` AS `l3pid` , > > `l4`.`id` AS 'l4id', > > `l4`.`pid` AS `l4pid` > > FROM `tree_data` AS `l0` > > LEFT JOIN `tree_data` as `l1` on (`l1`.`pid`=3D`l0`.`id`) > > LEFT JOIN `tree_data` as `l2` on (`l2`.`pid`=3D`l1`.`id`) > > LEFT JOIN `tree_data` as `l3` on (`l3`.`pid`=3D`l2`.`id`) > > LEFT JOIN `tree_data` as `l4` on (`l4`.`pid`=3D`l3`.`id`) > > WHERE `l0`.`pid` =3D0 > > > > id select_type table type possible_keys key key_len > ref > > rows Extra > > 1 SIMPLE l0 ref pid pid 4 const 4 > > 1 SIMPLE l1 ref pid pid 4 test.l0.id > 9 > > 1 SIMPLE l2 ref pid pid 4 test.l1.id > 9 > > 1 SIMPLE l3 ref pid pid 4 test.l2.id > 9 > > 1 SIMPLE l4 ref pid pid 4 test.l3.id > 9 > > > > time execution is 0.0069. result 207 rows > > > > looking at other query > > > > SELECT > > `l0`.`id` AS 'l0id', > > `l0`.`pid` AS `l0pid` , > > `l1`.`id` AS 'l1id', > > `l1`.`pid` AS `l1pid` , > > `l2`.`id` AS 'l2id', > > `l2`.`pid` AS `l2pid` , > > `l3`.`id` AS 'l3id', > > `l3`.`pid` AS `l3pid` , > > `l4`.`id` AS 'l4id', > > `l4`.`pid` AS `l4pid` > > FROM `tree_data` AS `l0` > > LEFT JOIN `tree_data` as `l1` on (`l1`.`pid`=3D`l0`.`id` or `l1`.`pid`= =3D100) > > LEFT JOIN `tree_data` as `l2` on (`l2`.`pid`=3D`l1`.`id` or `l2`.`pid`= =3D200) > > LEFT JOIN `tree_data` as `l3` on (`l3`.`pid`=3D`l2`.`id` or `l3`.`pid`= =3D300) > > LEFT JOIN `tree_data` as `l4` on (`l4`.`pid`=3D`l3`.`id` or `l4`.`pid`= =3D400) > > WHERE `l0`.`pid` =3D0 > > > > And now execution time is 0.1455!!! > > profiling gives: Sending data 0.137295 > > And analize: > > id select_type table type possible_keys key key_len ref > > rows Extr > > 1 SIMPLE l0 ref pid pid 4 const 4 > > 1 SIMPLE l1 ALL pid NULL NULL NULL 800 > > 1 SIMPLE l2 ALL pid NULL NULL NULL 800 > > 1 SIMPLE l3 ALL pid NULL NULL NULL 800 > > 1 SIMPLE l4 ALL pid NULL NULL NULL 800 > > > > And what will happen if there will be 10000 rows? > > > > (In real i using this `or` condition for selecting to the tree nodes > > for current element id and id can be id from other nodes.) > > > > > > -- > Bier met grenadyn > Is als mosterd by den wyn > Sy die't drinkt, is eene kwezel > Hy die't drinkt, is ras een ezel > --001485f78c0a263070048e03fecf--