This looks like a job for FULLTEXT indexes
CREATE TABLE users
(
user_id int not null,
domain_id int not null,
first_name varchar(20) not null
middle_name varchar(20) not null,
last_name varchar(20) not null,
work_place varchar (30) not null,
primary key (user_id),
FULLTEXT NamePlaceIndex (first_name,last_name,work_place)
) ENGINE=MyISAM;
Still Updating...
You should be able to query against the users table as follows:
SELECT * from users WHERE active=1 AND
MATCH(first_name,last_name,work_place)
AGAINST ('Bill Gates');
There are different ways to evaluate the searches, such as
SELECT *,MATCH(first_name,last_name,work_place) AGAINST ('Bill Gates') search_score
FROM users WHERE active=1 AND MATCH(first_name,last_name,work_place) AGAINST ('Bill Gates');
Keep in mind that FULLTEXT indexes are only available for MyISAM.
It is slated to be in InnoDB in MySQL 5.6
UPDATE 2012-03-16 13:27 EDT
Here is your original query
SELECT * FROM users WHERE active=1
AND ((first_name = 'Bill Gates' OR last_name = 'Bill Gates' OR work_place = 'Bill Gates')
OR ((first_name = 'Bill' OR last_name = 'Bill' OR work_place = 'Bill')
AND (first_name = 'Gates' OR last_name = 'Gates' OR work_place = 'Gates')) );
The composite index is not enough. You are going to need the following indexes:
ALTER TABLE users ADD INDEX (first_name,work_place);
ALTER TABLE users ADD INDEX (last_name,work_place);
After making these indexes
You could
1) go back and try your UNION queries and see...
or
2) you could try the following
SELECT B.* FROM
(
SELECT user_id FROM users WHERE first_name='Bill' and last_name='Gates' UNION
SELECT user_id FROM users WHERE last_name='Bill' and first_name='Gates' UNION
SELECT user_id FROM users WHERE first_name='Bill' and work_place='Gates' UNION
SELECT user_id FROM users WHERE work_place='Bill' and first_name='Gates' UNION
SELECT user_id FROM users WHERE last_name='Bill' and work_place='Gates' UNION
SELECT user_id FROM users WHERE work_place='Bill' and last_name='Gates'
) A INNER JOIN users B USING (user_id);
first_name
would be allowed to accept what look to be both first and last names?