Normalize Query

Hi All,
The following query is running slow, how can I fix it and make it run faster. Thanks
 SELECT * FROM tbl_bass_inject_output a JOIN tbl_studentid_tracking b ON a.studentid = b.student_id WHERE a.RecordID = ? AND b.trashed=? AND (b.baylor_name IS NULL OR b.baylor_name <> ?);
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
First, don't use "*"

Second, make sure any field referenced in either your join or where clause is indexed.

Without knowing more about your database schema there's not much more advice I can give.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Agree with @Russ

Is StudentID or RecordID  the Primay Key of the table ?
If neither are I would suggest creating a NON-CLUSTERED index one each of those fields
If either of them are the Pimary keys then create the NON-Clustered index on the other as the Primary key should be the Clustered index
Agree with @Russ and @Plusone

Creating a foreign key between the columns tbl_bass_inject_output.studentid and tbl_studentid_tracking.student_id will also help. Some database engine query optimizers can produce different execution plans for cases where there is a foreign key. A foreign implies referential integrity and this means certain scans/checks are unnecessary at query execution time.

An index on the "trashed" column will also help speed things up. I have a database where we use a flag to indicate data which was "deleted". It's just a BIT column, and the index helps speed things up a great deal.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.