简体   繁体   中英

My MySQL query is producing a very slow processing time. How do I speed it up?

My query is producing a very slow result. This is obviously unacceptable and I must be doing something wrong here.

I have 4 tables in the query. The tr_id refers to a single transaction which may contain applications from several people who want to go to several different countries. I want to display the information for each person and each country on a separate row like:

1298 2011-08-04 2011-09-01 Joe  Bloggs Platinum_Level China
1298 2011-08-04 2011-09-01 Jane Bloggs Platinum_Level China
1298 2011-08-04 2011-09-14 Joe  Bloggs Platinum_Level Japan
1298 2011-08-04 2011-09-14 Jane Bloggs Platinum_Level Japan

My query that I'm using is:

SELECT tr_id, tr_datecreated, tr_depart, trp_name, trp_lname, trv_processlevel,
visa_destcountry FROM transactions, people, trvisas, visas 
WHERE tr_datecreated >= "2009-09-02 00:00:00" AND tr_datecreated <= "2009-09-04 23:59:59" 
AND tr_id = trp_trid 
AND tr_id = trv_trid
AND trv_visaid = visa_code

There is a lot of data in the database, and I will only want to increase to complexity of the query as I add filters to narrow the results. This will mean accessing more tables and making more comparisons etc. I'm sure this is not too compicated though and it can be done within a reasonable time.

Grateful for any advice.

EDIT:

In fact, having benchmarked my code, the actual MySQL query is taking 53 seconds.

Check out the MySQL advice on optimisation:

http://dev.mysql.com/doc/refman/5.0/en/optimization.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM