Question

I have two tables (I'll list only fields that I want to search)

MySQL version: 5.0.96-community

Table 1: Clients (860 rows)
ID_CLIENT (varcahar 10)
ClientName (text)

Table 2: Details (22380 rows)
ID_CLIENT (varchar 10)
Details (varchar 1000)

The Details table can have multiple rows from the same client.

I need to search into those two tables and retrive the ids of clients that match a search value.

If i have a search value "blue" it has to match CLientName (ex the Blueberries Corp), or the Details in the second table (ex "it has a blue logo)

The result should be a list of client id's that match the criteria.

If I make a query for one table, it takes a decent time

#0.002 sec
select a.`ID_CLIENT` from clienti a
where 
a.`ClientName` LIKE '%blue%' 


#0.1 sec
SELECT b.`ID_CLIENT` FROM Details b
WHERE b.`Details` LIKE  '%blue%'
GROUP BY b.`GUID_CLIENT` 

But if I try to join those two queries it takes ages.

My questions(s)

  1. What's the best way of doing what I need here, to get a list of ID-s based on the search result from both tables

  2. What to change to improve search performance in the Details table, I know that %..% is not fast, but I need partial matches too.

Edit (based on the answers)

#~0.2 sec
(SELECT a.`ID_CLIENT` FROM `clienti` a  where a.`ClientName` like '%blue%')  
union
(SELECT distinct b.`ID_CLIENT` FROM `Details` b where b.`Details` like '%blue%')

It returns a list of IDs from both tables filtred by the search value.

Edit 2: final query

And with that list of ids I can filter the client table, to get only the clients that are in boths tables based on their id

select cl.`ID_CLIENT`, `ClientName`, `OtherField`
from clients cl 
join 
((SELECT a.`ID_CLIENT` FROM `clients` a  where a.`ClientName` like '%blue%')
union
(SELECT distinct b.`ID_Client` FROM `Details` b where b.`Detail` like '%blue%' )) rez 
on cl.`ID_CLIENT` = rez.`ID_CLIENT` 
Was it helpful?

Solution

If your two queries work, just use union:

select a.`ID_CLIENT`
from clienti a
where  a.`ClientName` LIKE '%blue%' 
union
SELECT b.`ID_CLIENT`
FROM Details b
WHERE b.`Details` LIKE '%blue%';

The union will remove all duplicates, so you don't need a separate group by query.

Why are the two search strings different for the two tables? The question suggests searching for blue in both of them.

If the individual queries don't perform well, you might need to switch to a full text index.

OTHER TIPS

If both queries are functioning as you wish, simply union the results together. This is much faster than 'or' on a query that cannot effectively use indexes and it will also allow you to remove duplicates in the same statement.

Divide et impera: create two subqueries.

In the first subquery, left join Clients with Details based on client_id, filtering on row values where clientname like '%xxxxx%'

Then another subquery, left joining details with clients ( but keep the same order of fields in output projection), filtering on details text field. Then create a union query from the two subqueries, and finally create a select distinct * from this union

Final schema:

Select distinct * from (subquery1 union subquery2)

This seems to be a really slow and silly "manual query plan optimization", just give a try and let we know of it works!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top