1

I have a users table with 450,000 records. The table structure is as so:

  UserID Int(11) Primary Key
  Firstname (50) Varchar
  Lastname  (50) Varchar

I also need to check two other table to see if the UserID is in those tables. (the structure is a bit different, but the table have the same UserID) I am running this sub query below, and is running very slow. Appreciate a second set of eyes to offer up a fresh perspective to help run a bit faster...

SELECT 
`Users`.`Firstname`, 
`Users`.`Lastname`, 
`Users`.`UserID` 
FROM `Users`
WHERE `Users`.`UserID` IN  (SELECT `admin`.`UserID` FROM `admin` WHERE `admin`.`UserID`=`User`.`UserID`)
AND `Users`.`UserID` IN  (SELECT `elite`.`UserID` FROM `elite` WHERE `elite`.`UserID`=`Users`.`UserID`)
AND `Users`.`Lastname` LIKE '%smith%'
1
  • What indexes do you have on the tables? Post SHOW CREATE TABLE ... for each table, and also post EXPLAIN SELECT ... output. Commented Apr 9, 2012 at 13:58

3 Answers 3

2

i guess instead of sub queries you should go with inner joins...because right now you are fetching the user id from the admin table, elite table and checking the same must be in user table...so go ahead with join and see the difference.

Sign up to request clarification or add additional context in comments.

Comments

1

The absolutely most important thing when optimizing queries in MySQL is to ensure you have the appropriate indexes. Check what indexes you have (using SHOW INDEX), and what indexes the query is using (using EXPLAIN SELECT). Indexes are absolutely vital for good performance once your tables get large.

In particular you should make sure that you have an index on the UserID column in all three tables. I'd also suggest adding a foreign key contraint if that is appropriate in your situation.

However if you have already checked you have the correct indexes and you still aren't getting appropriate performance, you can try using joins instead instead of subselects:

SELECT 
    Users.Firstname, 
    Users.Lastname, 
    Users.UserID 
FROM Users
JOIN admin USING (UserID)
JOIN elite USING (UserID)
WHERE Users.Lastname LIKE '%smith%'

Note that LIKE '%smith%' is unable to use an index efficiently. You should avoid LIKE expressions that start with a wildcard if possible.

1 Comment

It's worth mentioning that you can't put an index on '%smith%'.
1

Start with joins;

SELECT 
`Users`.`Firstname`, 
`Users`.`Lastname`, 
`Users`.`UserID` 
FROM `Users`
INNER JOIN `admin` ON (`Users`.`UserID` = `admin`.`Users`)
INNER JOIN `elite` ON (`Users`.`UserID` = `elite`.`UserID`)
WHERE `Users`.`Lastname` LIKE '%smith%'

Then check your indices.

You should also be away that the your last name search will be slow, because it is not anchored at the start or the end of the Lastname string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.