# mysql join optimization question

**URL:** <https://forums.percona.com/t/mysql-join-optimization-question/328>\
**Category:** Other MySQL® Questions\
**Created:** [May 18, 2007, 9:48pm UTC](https://forums.percona.com/t/mysql-join-optimization-question/328 "2007-05-18T21:48:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![btuchten](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@btuchten](https://forums.percona.com/u/btuchten)\
**Post date:** [May 18, 2007, 9:48pm UTC](https://forums.percona.com/t/mysql-join-optimization-question/328/1 "2007-05-18T21:48:33Z")

</div>

Hi,

Could someone take a look at this and tell me why it might be slower than I expected? The search of course is slow the first time, and after that it gets cached ( and much faster ). But I am looking to speed up the initial search.

mysql\> EXPLAIN select \* FROM unit\_history LEFT OUTER JOIN unit\_auth\_list ON `unit_history`.`serial`=`unit_auth_list`.`serial` where 1;±—±------------±---------------±-------±--------------±-------±--------±-----------------------------±-------±------+| id | select\_type | table | type | possible\_keys | key | key\_len | ref | rows | Extra |±—±------------±---------------±-------±--------------±-------±--------±-----------------------------±-------±------+| 1 | SIMPLE | unit\_history | ALL | NULL | NULL | NULL | NULL | 172814 | | | 1 | SIMPLE | unit\_auth\_list | eq\_ref | serial | serial | 22 | mgmt\_db.unit\_history.serial | 1 | | ±—±------------±---------------±-------±--------------±-------±--------±-----------------------------±-------±------+2 rows in set (0.00 sec)mysql\> select count(_) FROM `unit_history` LEFT OUTER JOIN `unit_auth_list` ON `unit_history`.`serial`=`unit_auth_list`.`serial` WHERE (`unit_auth_list`.`major_name` LIKE ‘%testsearch%’ ) ORDER BY `unit_auth_list`.`major_name` DESC ,`unit_history`.`date`;±---------+| count(_) |±---------+| 33772 | ±---------+1 row in set (9.89 sec)mysql\> SELECT \* FROM `unit_history` LEFT OUTER JOIN `unit_auth_list` ON `unit_history`.`serial`=`unit_auth_list`.`serial` WHERE (`unit_auth_list`.`major_name` LIKE ‘%testsearch%’ ) ORDER BY `unit_auth_list`.`major_name` DESC ,`unit_history`.`date` DESC LIMIT 0,30…output snipped out…30 rows in set (17.25 sec)

Thanks!

-Brian

---

<div class="post-metadata">

**Author:** ![sterin](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@sterin](https://forums.percona.com/u/sterin)\
**Post date:** [May 19, 2007, 3:54am UTC](https://forums.percona.com/t/mysql-join-optimization-question/328/2 "2007-05-19T03:54:02Z")

</div>

In your first COUNT(_) query:  
1.  
You can start by dropping the ORDER BY clause. COUNT(_) is counting the nr of records and doesn’t care about if they are ordered or not.

1. 

Do you really want a LEFT JOIN on this query?  
The way you have written it means that the result is essentially an inner join. The fact that you put the condition major\_name which is in the right table in the WHERE clause is what forces this. The problem is that if you use LEFT JOIN then you are forcing mysql to use the join order first table(unit\_history)-\>second table(unit\_auth\_list).  
While your WHERE is defining that you want to find records WHERE unit\_auth\_list.major\_name LIKE ‘%teststring%’.

1. 

a LIKE ‘%teststring’ with a wildcard in the beginning is never good performance because an index can’t be used properly if you have a wildcard in the beginning. So don’t expect miracles with that type of query.

My suggestion (but you will have test that it gives you the desired result):  
The first query:

SELECT COUNT(\*)FROM unit\_auth\_listWHERE major\_name LIKE ‘%teststring%’

To get the total number of records.

And the data query:

SELECT \*FROM unit\_history uhINNER JOIN unit\_auth\_list ual ON uh.serial = ual.serialWHERE ual.major\_name LIKE '%testsearch%'ORDER BY ual.major\_name DESC ,uh.dateLIMIT 0,30

Make sure that you have a combined index on table unit\_auth\_list on the columns(major\_name, date).  
And that you have an index on table unit\_history on column (serial).  
That way mysql can start the execution of the query with table unit\_auth\_list and then find the corresponding records in unit\_history. And that should save you some time.

BTW: don’t use ‘date’ as the column name for a DATE column. Since DATE is a column type the word date is reserved in the SQL language. Mysql as basically the only DBMS is allowing it. But you do yourself a favour if you learn now the reserved words in SQL.

---

<div class="post-metadata">

**Author:** ![btuchten](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@btuchten](https://forums.percona.com/u/btuchten)\
**Post date:** [May 19, 2007, 9:31am UTC](https://forums.percona.com/t/mysql-join-optimization-question/328/3 "2007-05-19T09:31:08Z")

</div>

Thanks for the reply!

I was able to get my searches way down from 12 seconds to less than 1 second. Woot!

Thanks again!

-Brian
